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