Fixed mysql no user defaults file handling
[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 fi
143
144 # if a user is not set, use $confgfile, otherwise use $mycnf
145 if [ "$user" == "" ]; then
146         user=root;
147         defaultsfile="--defaults-file=$configfile"
148 else
149         userset=true;
150         if [ $usevserver ]
151         then
152             vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
153             if [ $? -eq 2 ]
154             then
155                 fatal "User $user not found in /etc/passwd"
156             fi
157             userhome="$vroot$vuserhome"
158         else
159             userhome=`getent passwd "$user" | awk -F: '{print $6}'`
160             if [ $? -eq 2 ]
161             then
162                 fatal "User $user not found in /etc/passwd"
163             fi
164         fi
165         
166         debug "User home set to: $userhome"
167         [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
168         defaultsfile="--defaults-file=$vuserhome/.my.cnf"
169         debug "using $defaultsfile"
170 fi
171
172 #######################################################################
173 ## HOT COPY
174
175 if [ "$hotcopy" == "yes" ]
176 then
177     info "Initializing hotcopy method"
178     if [ "$databases" == "all" ]
179     then
180         if [ $usevserver ]
181         then
182                 info "dbhost: $dbhost"
183                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
184         else
185                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
186         fi
187         debug "su $user -c '$execstr'"
188         if [ ! $test ]
189         then
190                 output=`su $user -c "$execstr" 2>&1`
191                 code=$?
192                 if [ "$code" == "0" ]
193                 then
194                         debug $output
195                         info "Successfully finished hotcopy of all mysql databases"
196                 else
197                         warning $output
198                         warning "Failed to hotcopy all mysql databases"
199                 fi
200         fi
201     else        
202         for db in $databases
203         do
204                 if [ $usevserver ]
205                 then
206                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
207                 else
208                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
209                 fi
210                 debug "su $user -c '$execstr'"
211                 if [ ! $test ]
212                 then
213                         output=`su $user -c "$execstr" 2>&1`
214                         code=$?
215                         if [ "$code" == "0" ]
216                         then
217                                 debug $output
218                                 info "Successfully finished hotcopy of mysql database $db"
219                         else
220                                 warning $output
221                                 warning "Failed to hotcopy mysql database $db"
222                         fi
223                 fi
224         done
225      fi
226 fi
227
228 ##########################################################################
229 ## SQL DUMP
230
231 if [ "$sqldump" == "yes" ]
232 then
233     info "Initializing SQL dump method"
234     if [ "$databases" == "all" ]
235     then
236         if [ $usevserver ]
237         then
238             debug "echo show databases | $VSERVER $vsname exec su $user -c $MYSQL $defaultsfile | grep -v Database"
239             databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
240             if [ $? -ne 0 ]
241             then
242                 fatal "Authentication problem, maybe user/password is wrong"
243             fi
244         else
245                 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
246                 if [ $? -ne 0 ]
247                 then
248                     fatal "Authentication problem, maybe user/password is wrong"
249                 fi
250         fi
251 fi
252
253         for db in $databases
254         do
255                 if [ $usevserver ]
256                 then
257                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
258                 else
259                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
260                 fi
261                 debug "su $user -c '$execstr'"
262                 if [ ! $test ]
263                 then
264                         output=`su $user -c "$execstr" 2>&1`
265                         code=$?
266                         if [ "$code" == "0" ]
267                         then
268                                 debug $output
269                                 info "Successfully finished dump of mysql database $db"
270                         else
271                                 warning $output
272                                 warning "Failed to dump mysql databases $db"
273                         fi
274                 fi
275         done
276         
277         if [ "$compress" == "yes" ]
278         then
279                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
280                 debug $output
281         fi
282 fi
283
284 # clean up tmp config file
285 if [ "$dbusername" != "" ]
286 then
287         ## clean up tmp config file
288         debug "rm $mycnf"
289         rm $mycnf
290         if [ -f "$tmpcnf" ]
291         then
292                 debug "mv $tmpcnf $mycnf"
293                 mv $tmpcnf $mycnf
294         fi
295 fi
296
297 return 0