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