1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # mysql handler script for backupninja
6 getconf backupdir /var/backups/mysql
9 getconf dbhost localhost
19 getconf configfile /etc/mysql/debian.cnf
22 # If vservers are configured, decide if the handler should
23 # use them or if it should just operate on the host
25 if [ "$vservers" = "yes" ]
29 info "Using vserver '$vsname'"
32 info "No vserver name specified, actions will be performed on the host"
36 # If needed, make sure that the specified vserver exists and is running.
39 debug "Examining vserver '$vsname'"
41 vroot="$VROOTDIR/$vsname"
42 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
44 $VSERVERINFO -q $vsname RUNNING
47 fatal "vserver $vsname is not running."
51 ## Prepare ignore part of the command
52 ## This only works for mysqldump at the moment
56 ignore="$ignore --ignore-table=$i"
59 # create backup dirs, $vroot will be empty if no vsname was specified
60 # and we will instead proceed to operate on the host
61 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
62 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
63 hotdir="$backupdir/hotcopy"
64 dumpdir="$backupdir/sqldump"
68 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
69 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
71 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
72 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
75 #######################################################################
79 # one of three authentication methods:
80 # 1. setting the user, so that /home/user/.my.cnf is used.
81 # 2. specifying the user and password in the handler config,
82 # which generates a temporary .my.cnf in /root/.my.cnf
83 # 3. specify the config file with --defaults-file
84 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
88 # only if dbusername and dbpassword specified.
89 # we create a tmp file because we don't want to
90 # specify the password on the command line.
94 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
98 vhome=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
101 home=`getent passwd "root" | awk -F: '{print $6}'`
104 [ -d $home ] || fatal "Can't find root's home directory ($home)."
106 mycnf="$home/.my.cnf"
111 tmpcnf="$home/my.cnf.disable"
112 debug "mv $mycnf $tmpcnf"
119 # auto generated backupninja mysql conf
123 password="$dbpassword"
128 password="$dbpassword"
133 password="$dbpassword"
138 defaultsfile="--defaults-file=$vhome/.my.cnf"
140 defaultsfile="--defaults-file=$mycnf"
143 # if user is set, don't use $mycnf
144 elif [ "$userset" == "false" ]; then
145 defaultsfile="--defaults-file=$configfile"
148 if [ "$user" == "" ]; then
155 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
158 fatal "User $user not found in /etc/passwd"
160 userhome="$vroot$vuserhome"
162 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
165 fatal "User $user not found in /etc/passwd"
169 debug "User home set to: $userhome"
170 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
171 defaultsfile="--defaults-file=$vuserhome/.my.cnf"
172 debug "using $defaultsfile"
175 #######################################################################
178 if [ "$hotcopy" == "yes" ]
180 info "Initializing hotcopy method"
181 if [ "$databases" == "all" ]
185 info "dbhost: $dbhost"
186 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
188 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
190 debug "su $user -c '$execstr'"
193 output=`su $user -c "$execstr" 2>&1`
195 if [ "$code" == "0" ]
198 info "Successfully finished hotcopy of all mysql databases"
201 warning "Failed to hotcopy all mysql databases"
209 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
211 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
213 debug "su $user -c '$execstr'"
216 output=`su $user -c "$execstr" 2>&1`
218 if [ "$code" == "0" ]
221 info "Successfully finished hotcopy of mysql database $db"
224 warning "Failed to hotcopy mysql database $db"
231 ##########################################################################
234 if [ "$sqldump" == "yes" ]
236 info "Initializing SQL dump method"
237 if [ "$databases" == "all" ]
241 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
244 fatal "Authentication problem, maybe user/password is wrong"
247 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
250 fatal "Authentication problem, maybe user/password is wrong"
259 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
261 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
263 debug "su $user -c '$execstr'"
266 output=`su $user -c "$execstr" 2>&1`
268 if [ "$code" == "0" ]
271 info "Successfully finished dump of mysql database $db"
274 warning "Failed to dump mysql databases $db"
279 if [ "$compress" == "yes" ]
281 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
286 # clean up tmp config file
287 if [ "$dbusername" != "" ]
289 ## clean up tmp config file
294 debug "mv $tmpcnf $mycnf"