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