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