r220@um: micah | 2005-12-27 11:12:31 -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                 info "dbhost: $dbhost"
184                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
185         else
186                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187         fi
188         debug "su $user -c '$execstr'"
189         if [ ! $test ]
190         then
191                 output=`su $user -c "$execstr" 2>&1`
192                 code=$?
193                 if [ "$code" == "0" ]
194                 then
195                         debug $output
196                         info "Successfully finished hotcopy of all mysql databases"
197                 else
198                         warning $output
199                         warning "Failed to hotcopy all mysql databases"
200                 fi
201         fi
202     else        
203         for db in $databases
204         do
205                 if [ $usevserver ]
206                 then
207                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
208                 else
209                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
210                 fi
211                 debug "su $user -c '$execstr'"
212                 if [ ! $test ]
213                 then
214                         output=`su $user -c "$execstr" 2>&1`
215                         code=$?
216                         if [ "$code" == "0" ]
217                         then
218                                 debug $output
219                                 info "Successfully finished hotcopy of mysql database $db"
220                         else
221                                 warning $output
222                                 warning "Failed to hotcopy mysql database $db"
223                         fi
224                 fi
225         done
226      fi
227 fi
228
229 ##########################################################################
230 ## SQL DUMP
231
232 if [ "$sqldump" == "yes" ]
233 then
234     info "Initializing SQL dump method"
235     if [ "$databases" == "all" ]
236     then
237         if [ $usevserver ]
238         then
239             databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
240             if [ $? -ne 0 ]
241             then
242                 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
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 "Something unexpected happened, the defaults file may have gone missing or is corrupt"
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