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