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"
144 # if a user is not set, use $confgfile, otherwise use $mycnf
145 if [ "$user" == "" ]; then
147 defaultsfile="--defaults-file=$configfile"
152 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
155 fatal "User $user not found in /etc/passwd"
157 userhome="$vroot$vuserhome"
159 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
162 fatal "User $user not found in /etc/passwd"
166 debug "User home set to: $userhome"
167 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
168 defaultsfile="--defaults-file=$vuserhome/.my.cnf"
169 debug "using $defaultsfile"
172 #######################################################################
175 if [ "$hotcopy" == "yes" ]
177 info "Initializing hotcopy method"
178 if [ "$databases" == "all" ]
182 info "dbhost: $dbhost"
183 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
185 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187 debug "su $user -c '$execstr'"
190 output=`su $user -c "$execstr" 2>&1`
192 if [ "$code" == "0" ]
195 info "Successfully finished hotcopy of all mysql databases"
198 warning "Failed to hotcopy all mysql databases"
206 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
208 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
210 debug "su $user -c '$execstr'"
213 output=`su $user -c "$execstr" 2>&1`
215 if [ "$code" == "0" ]
218 info "Successfully finished hotcopy of mysql database $db"
221 warning "Failed to hotcopy mysql database $db"
228 ##########################################################################
231 if [ "$sqldump" == "yes" ]
233 info "Initializing SQL dump method"
234 if [ "$databases" == "all" ]
238 debug "echo show databases | $VSERVER $vsname exec su $user -c $MYSQL $defaultsfile | grep -v Database"
239 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
242 fatal "Authentication problem, maybe user/password is wrong"
245 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
248 fatal "Authentication problem, maybe user/password is wrong"
257 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
259 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
261 debug "su $user -c '$execstr'"
264 output=`su $user -c "$execstr" 2>&1`
266 if [ "$code" == "0" ]
269 info "Successfully finished dump of mysql database $db"
272 warning "Failed to dump mysql databases $db"
277 if [ "$compress" == "yes" ]
279 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
284 # clean up tmp config file
285 if [ "$dbusername" != "" ]
287 ## clean up tmp config file
292 debug "mv $tmpcnf $mycnf"