Spruced up the changelog a bit
[matthijs/upstream/backupninja-vserver.git] / handlers / mysql
1 #
2 # mysql handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/mysql
6 getconf databases all
7 getconf compress yes
8 getconf dbusername
9 getconf dbpassword
10 getconf dbhost localhost
11 getconf hotcopy no
12 getconf sqldump no
13 getconf user root
14 getconf vsname
15
16 # If vservers are configured, decide if the handler should
17 # use them or if it should just operate on the host
18 if [ "$VSERVERS" = "yes" ]
19 then
20         if [ ! -z $vsname ]
21         then            
22                 info "Using vserver '$vsname'"
23                 usevserver=1
24         else
25                 info "No vserver name specified, actions will be performed on the host"
26         fi
27 fi
28
29 # Check to make sure that the specified vserver exists
30 if [ $usevserver ]
31 then
32         vroot="$VROOTDIR/$vsname"
33         [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
34 fi
35         
36 # create backup dirs, the vroot variable will be empty if no vsname was specified
37 # and will proceed to operate on the host
38 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
39 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
40 hotdir="$backupdir/hotcopy"
41 dumpdir="$backupdir/sqldump"
42
43 if [ $usevserver ]
44 then
45         [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
46         [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
47 else
48         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
49         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
50 fi
51
52 # create .my.cnf
53 # (we do this because we don't want to have to specify the password on the command line
54 # because then anyone would be able to see it with a 'ps aux'. instead, we create a 
55 # temporary ~/.my.cnf in root's home directory).
56
57 if [ "$dbusername" != "" ]; then
58         home=`grep '^root:' $vroot/etc/passwd | awk -F: '{print $6}'`
59         [ -d $home ] || fatal "Can't find root's home directory ($home)."
60         mycnf="$vroot$home/.my.cnf"
61         if [ -f $mycnf ]; then
62                 # rename temporarily
63                 tmpcnf="$home/my.cnf.disable"
64                 debug "mv $mycnf $tmpcnf"
65                 mv $mycnf $tmpcnf
66         fi
67         oldmask=`umask`
68         umask 077
69         cat > $mycnf <<EOF
70 # auto generated backupninja mysql conf
71 [mysql]
72 user=$dbusername
73 password=$dbpassword
74
75 [mysqldump]
76 user=$dbusername
77 password=$dbpassword
78
79 [mysqlhotcopy]
80 user=$dbusername
81 password=$dbpassword
82 EOF
83         umask $oldmask
84 fi
85         
86 ## HOT COPY
87
88 if [ "$hotcopy" == "yes" ]; then 
89         if [ "$databases" == "all" ]; then
90                 if [ $usevserver ]
91                 then
92                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
93                 else
94                         execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
95                 fi
96                 debug "su $user -c '$execstr'"
97                 if [ ! $test ]; then
98                         output=`su $user -c "$execstr" 2>&1`
99                         code=$?
100                         if [ "$code" == "0" ]; then
101                                 debug $output
102                                 info "Successfully finished hotcopy of all mysql databases"
103                         else
104                                 warning $output
105                                 warning "Failed to hotcopy all mysql databases"
106                         fi
107                 fi
108         else    
109                 for db in $databases; do
110                         if [ $usevserver ]
111                         then
112                                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
113                         else
114                                 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
115                         fi
116                         debug "su $user -c '$execstr'"
117                         if [ ! $test ]; then
118                                 output=`su $user -c "$execstr" 2>&1`
119                                 code=$?
120                                 if [ "$code" == "0" ]; then
121                                         debug $output
122                                         info "Successfully finished hotcopy of mysql database $db"
123                                 else
124                                         warning $output
125                                         warning "Failed to hotcopy mysql database $db"
126                                 fi
127                         fi
128                 done
129         fi
130 fi
131         
132 ## SQL DUMP
133
134 if [ "$sqldump" == "yes" ]; then
135         if [ "$databases" == "all" ]; then
136                 if [ $usevserver ]
137                 then
138                         databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL" | grep -v Database`
139                 else
140                         databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
141                 fi
142         fi
143
144         for db in $databases; do
145                 if [ $usevserver ]
146                 then
147                         execstr="$VSERVER $vsname exec $MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
148                 else
149                         execstr="$MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
150                 fi
151                 debug "su $user -c '$execstr'"
152                 if [ ! $test ]; then
153                         output=`su $user -c "$execstr" 2>&1`
154                         code=$?
155                         if [ "$code" == "0" ]; then
156                                 debug $output
157                                 info "Successfully finished dump of mysql database $db"
158                         else
159                                 warning $output
160                                 warning "Failed to dump mysql databases $db"
161                         fi
162                 fi
163         done
164         
165         if [ "$compress" == "yes" ]; then
166                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
167                 debug $output
168         fi
169 fi
170
171 if [ "$dbusername" != "" ]; then
172         ## clean up tmp config file
173         debug "rm $mycnf"
174         rm $mycnf
175         if [ -f "$tmpcnf" ]; then
176                 debug "mv $tmpcnf $mycnf"
177                 mv $tmpcnf $mycnf
178         fi
179 fi
180
181 return 0