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