r217@um: micah | 2005-12-27 09:57:30 -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         info "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             info "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             info "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 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
119 then
120     if [ $usevserver ]
121     then
122         home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
123         home="$vroot$home"
124         info "Home set to: $home"
125     else
126         home=`getent passwd "root" | awk -F: '{print $6}'`
127         info "Home set to: $home"
128     fi
129     [ -d $home ] || fatal "Can't find root's home directory ($home)."
130     mycnf="$home/.my.cnf"
131     if [ -f $mycnf ]
132     then
133         # rename temporarily
134         tmpcnf="$home/my.cnf.disable"
135         debug "mv $mycnf $tmpcnf"
136         mv $mycnf $tmpcnf
137     fi
138     oldmask=`umask`
139     umask 077
140     cat > $mycnf <<EOF
141 # auto generated backupninja mysql conf
142 [mysql]
143 user=$dbusername
144 password="$dbpassword"
145
146 [mysqldump]
147 user=$dbusername
148 password="$dbpassword"
149
150 [mysqlhotcopy]
151 user=$dbusername
152 password="$dbpassword"
153 EOF
154         umask $oldmask
155         defaultsfile="--defaults-file=$mycnf"
156 elif [ "$userset" == "false" ]; then
157         # if user is set, don't use $mycnf
158         defaultsfile="--defaults-file=$configfile"
159 fi
160
161 #######################################################################
162 ## HOT COPY
163
164 if [ "$hotcopy" == "yes" ]
165 then
166     info "Initializing hotcopy method"
167     if [ "$databases" == "all" ]
168     then
169         if [ $usevserver ]
170         then
171                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
172         else
173                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
174         fi
175         debug "su $user -c '$execstr'"
176         if [ ! $test ]
177         then
178                 output=`su $user -c "$execstr" 2>&1`
179                 code=$?
180                 if [ "$code" == "0" ]
181                 then
182                         debug $output
183                         info "Successfully finished hotcopy of all mysql databases"
184                 else
185                         warning $output
186                         warning "Failed to hotcopy all mysql databases"
187                 fi
188         fi
189     else        
190         for db in $databases
191         do
192                 if [ $usevserver ]
193                 then
194                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
195                 else
196                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
197                 fi
198                 debug "su $user -c '$execstr'"
199                 if [ ! $test ]
200                 then
201                         output=`su $user -c "$execstr" 2>&1`
202                         code=$?
203                         if [ "$code" == "0" ]
204                         then
205                                 debug $output
206                                 info "Successfully finished hotcopy of mysql database $db"
207                         else
208                                 warning $output
209                                 warning "Failed to hotcopy mysql database $db"
210                         fi
211                 fi
212         done
213      fi
214 fi
215
216 ##########################################################################
217 ## SQL DUMP
218
219 if [ "$sqldump" == "yes" ]
220 then
221     info "Initializing SQL dump method"
222     if [ "$databases" == "all" ]
223     then
224         if [ $usevserver ]
225         then
226                 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
227                 if [ $? -ne 0 ]
228                 then
229                     fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
230                 fi
231         else
232                 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
233                 if [ $? -ne 0 ]
234                 then
235                     fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
236                 fi
237         fi
238 fi
239
240         for db in $databases
241         do
242                 if [ $usevserver ]
243                 then
244                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
245                 else
246                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
247                 fi
248                 debug "su $user -c '$execstr'"
249                 if [ ! $test ]
250                 then
251                         output=`su $user -c "$execstr" 2>&1`
252                         code=$?
253                         if [ "$code" == "0" ]
254                         then
255                                 debug $output
256                                 info "Successfully finished dump of mysql database $db"
257                         else
258                                 warning $output
259                                 warning "Failed to dump mysql databases $db"
260                         fi
261                 fi
262         done
263         
264         if [ "$compress" == "yes" ]
265         then
266                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
267                 debug $output
268         fi
269 fi
270
271 # clean up tmp config file
272 if [ "$dbusername" != "" ]
273 then
274         ## clean up tmp config file
275         debug "rm $mycnf"
276         rm $mycnf
277         if [ -f "$tmpcnf" ]
278         then
279                 debug "mv $tmpcnf $mycnf"
280                 mv $tmpcnf $mycnf
281         fi
282 fi
283
284 return 0