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