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