Oops, adding ninjahelper to Makefile
[matthijs/upstream/backupninja.git] / handlers / mysql
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # mysql handler script for backupninja
4 #
5
6 getconf backupdir /var/backups/mysql
7 getconf databases all
8 getconf ignores
9 getconf dbhost localhost
10 getconf hotcopy no
11 getconf sqldump no
12 getconf compress yes
13 getconf vsname
14
15 # authentication:
16 getconf user
17 getconf dbusername
18 getconf dbpassword
19 getconf configfile /etc/mysql/debian.cnf
20
21
22 # If vservers are configured, decide if the handler should
23 # use them or if it should just operate on the host
24
25 if [ "$vservers" = "yes" ]
26 then
27         if [ ! -z $vsname ]
28         then            
29                 info "Using vserver '$vsname'"
30                 usevserver=1
31         else
32                 info "No vserver name specified, actions will be performed on the host"
33         fi
34 fi
35
36 # If needed, make sure that the specified vserver exists and is running.
37 if [ $usevserver ]
38 then
39         debug "Examining vserver '$vsname'"
40         # does it exist ?
41         vroot="$VROOTDIR/$vsname"
42         [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
43         # is it running ?
44         $VSERVERINFO -q $vsname RUNNING
45         if [ $? -ne 0 ]
46         then
47                 fatal "vserver $vsname is not running."
48         fi
49 fi
50
51 ## Prepare ignore part of the command
52 ## This only works for mysqldump at the moment
53
54 ignore=''
55 for i in $ignores; do
56        ignore="$ignore --ignore-table=$i"
57 done
58         
59 # create backup dirs, $vroot will be empty if no vsname was specified
60 # and we will instead proceed to operate on the host
61 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
62 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
63 hotdir="$backupdir/hotcopy"
64 dumpdir="$backupdir/sqldump"
65
66 if [ $usevserver ]
67 then
68         [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
69         [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
70 else
71         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
72         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
73 fi
74
75 #######################################################################
76 ## AUTHENTICATION
77
78 #
79 # one of three authentication methods:
80 # 1. setting the user, so that /home/user/.my.cnf is used.
81 # 2. specifying the user and password in the handler config,
82 #    which generates a temporary .my.cnf in /root/.my.cnf
83 # 3. specify the config file with --defaults-file
84 #    (this option DOESN'T WORK WITH MYSQLHOTCOPY)
85 #
86
87 # create .my.cnf
88 # only if dbusername and dbpassword specified.
89 # we create a tmp file because we don't want to
90 # specify the password on the command line.
91
92 defaultsfile=""
93
94 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
95 then
96     if [ $usevserver ]
97     then
98         vhome=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
99         home="$vroot$vhome"
100     else
101         home=`getent passwd "root" | awk -F: '{print $6}'`
102     fi
103
104     [ -d $home ] || fatal "Can't find root's home directory ($home)."
105     
106     mycnf="$home/.my.cnf"
107     
108     if [ -f $mycnf ]
109     then
110         # rename temporarily
111         tmpcnf="$home/my.cnf.disable"
112         debug "mv $mycnf $tmpcnf"
113         mv $mycnf $tmpcnf
114     fi
115     
116     oldmask=`umask`
117     umask 077
118     cat > $mycnf <<EOF
119 # auto generated backupninja mysql conf
120 [mysql]
121 host=$dbhost
122 user=$dbusername
123 password="$dbpassword"
124
125 [mysqldump]
126 host=$dbhost
127 user=$dbusername
128 password="$dbpassword"
129
130 [mysqlhotcopy]
131 host=$dbhost
132 user=$dbusername
133 password="$dbpassword"
134 EOF
135         umask $oldmask
136         if [ $usevserver ] 
137         then
138             defaultsfile="--defaults-file=$vhome/.my.cnf"
139         else
140             defaultsfile="--defaults-file=$mycnf"
141         fi
142         
143 # if user is set, don't use $mycnf      
144 elif [ "$userset" == "false" ]; then
145                 defaultsfile="--defaults-file=$configfile"
146 fi
147
148 if [ "$user" == "" ]; then
149         userset=false;
150         user=root;
151 else
152         userset=true;
153         if [ $usevserver ]
154         then
155             vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
156             if [ $? -eq 2 ]
157             then
158                 fatal "User $user not found in /etc/passwd"
159             fi
160             userhome="$vroot$vuserhome"
161         else
162             userhome=`getent passwd "$user" | awk -F: '{print $6}'`
163             if [ $? -eq 2 ]
164             then
165                 fatal "User $user not found in /etc/passwd"
166             fi
167         fi
168         
169         debug "User home set to: $userhome"
170         [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
171         defaultsfile="--defaults-file=$vuserhome/.my.cnf"
172         debug "using $defaultsfile"
173 fi
174
175 #######################################################################
176 ## HOT COPY
177
178 if [ "$hotcopy" == "yes" ]
179 then
180     info "Initializing hotcopy method"
181     if [ "$databases" == "all" ]
182     then
183         if [ $usevserver ]
184         then
185                 info "dbhost: $dbhost"
186                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187         else
188                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
189         fi
190         debug "su $user -c '$execstr'"
191         if [ ! $test ]
192         then
193                 output=`su $user -c "$execstr" 2>&1`
194                 code=$?
195                 if [ "$code" == "0" ]
196                 then
197                         debug $output
198                         info "Successfully finished hotcopy of all mysql databases"
199                 else
200                         warning $output
201                         warning "Failed to hotcopy all mysql databases"
202                 fi
203         fi
204     else        
205         for db in $databases
206         do
207                 if [ $usevserver ]
208                 then
209                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
210                 else
211                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
212                 fi
213                 debug "su $user -c '$execstr'"
214                 if [ ! $test ]
215                 then
216                         output=`su $user -c "$execstr" 2>&1`
217                         code=$?
218                         if [ "$code" == "0" ]
219                         then
220                                 debug $output
221                                 info "Successfully finished hotcopy of mysql database $db"
222                         else
223                                 warning $output
224                                 warning "Failed to hotcopy mysql database $db"
225                         fi
226                 fi
227         done
228      fi
229 fi
230
231 ##########################################################################
232 ## SQL DUMP
233
234 if [ "$sqldump" == "yes" ]
235 then
236     info "Initializing SQL dump method"
237     if [ "$databases" == "all" ]
238     then
239         if [ $usevserver ]
240         then
241             databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
242             if [ $? -ne 0 ]
243             then
244                 fatal "Authentication problem, maybe user/password is wrong"
245             fi
246         else
247                 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
248                 if [ $? -ne 0 ]
249                 then
250                     fatal "Authentication problem, maybe user/password is wrong"
251                 fi
252         fi
253 fi
254
255         for db in $databases
256         do
257                 if [ $usevserver ]
258                 then
259                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
260                 else
261                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
262                 fi
263                 debug "su $user -c '$execstr'"
264                 if [ ! $test ]
265                 then
266                         output=`su $user -c "$execstr" 2>&1`
267                         code=$?
268                         if [ "$code" == "0" ]
269                         then
270                                 debug $output
271                                 info "Successfully finished dump of mysql database $db"
272                         else
273                                 warning $output
274                                 warning "Failed to dump mysql databases $db"
275                         fi
276                 fi
277         done
278         
279         if [ "$compress" == "yes" ]
280         then
281                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
282                 debug $output
283         fi
284 fi
285
286 # clean up tmp config file
287 if [ "$dbusername" != "" ]
288 then
289         ## clean up tmp config file
290         debug "rm $mycnf"
291         rm $mycnf
292         if [ -f "$tmpcnf" ]
293         then
294                 debug "mv $tmpcnf $mycnf"
295                 mv $tmpcnf $mycnf
296         fi
297 fi
298
299 return 0