2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
8 getconf dbhost localhost
18 getconf configfile /etc/mysql/debian.cnf
21 # If vservers are configured, decide if the handler should
22 # use them or if it should just operate on the host
24 if [ "$vservers" = "yes" ]
28 info "Using vserver '$vsname'"
31 info "No vserver name specified, actions will be performed on the host"
35 # If needed, make sure that the specified vserver exists and is running.
38 debug "Examining vserver '$vsname'"
40 vroot="$VROOTDIR/$vsname"
41 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
43 $VSERVERINFO -q $vsname RUNNING
46 fatal "vserver $vsname is not running."
50 ## Prepare ignore part of the command
51 ## This only works for mysqldump at the moment
55 ignore="$ignore --ignore-table=$i"
58 # create backup dirs, $vroot will be empty if no vsname was specified
59 # and we will instead proceed to operate on the host
60 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
61 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
62 hotdir="$backupdir/hotcopy"
63 dumpdir="$backupdir/sqldump"
67 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
68 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
70 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
71 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
74 #######################################################################
78 # one of three authentication methods:
79 # 1. setting the user, so that /home/user/.my.cnf is used.
80 # 2. specifying the user and password in the handler config,
81 # which generates a temporary .my.cnf in /root/.my.cnf
82 # 3. specify the config file with --defaults-file
83 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
87 # only if dbusername and dbpassword specified.
88 # we create a tmp file because we don't want to
89 # specify the password on the command line.
93 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
97 vhome=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
100 home=`getent passwd "root" | awk -F: '{print $6}'`
103 [ -d $home ] || fatal "Can't find root's home directory ($home)."
105 mycnf="$home/.my.cnf"
110 tmpcnf="$home/my.cnf.disable"
111 debug "mv $mycnf $tmpcnf"
118 # auto generated backupninja mysql conf
122 password="$dbpassword"
127 password="$dbpassword"
132 password="$dbpassword"
137 defaultsfile="--defaults-file=$vhome/.my.cnf"
139 defaultsfile="--defaults-file=$mycnf"
142 # if user is set, don't use $mycnf
143 elif [ "$userset" == "false" ]; then
144 defaultsfile="--defaults-file=$configfile"
147 if [ "$user" == "" ]; then
154 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
157 fatal "User $user not found in /etc/passwd"
159 userhome="$vroot$vuserhome"
161 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
164 fatal "User $user not found in /etc/passwd"
168 debug "User home set to: $userhome"
169 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
170 defaultsfile="--defaults-file=$vuserhome/.my.cnf"
171 debug "using $defaultsfile"
174 #######################################################################
177 if [ "$hotcopy" == "yes" ]
179 info "Initializing hotcopy method"
180 if [ "$databases" == "all" ]
184 info "dbhost: $dbhost"
185 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
189 debug "su $user -c '$execstr'"
192 output=`su $user -c "$execstr" 2>&1`
194 if [ "$code" == "0" ]
197 info "Successfully finished hotcopy of all mysql databases"
200 warning "Failed to hotcopy all mysql databases"
208 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
210 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
212 debug "su $user -c '$execstr'"
215 output=`su $user -c "$execstr" 2>&1`
217 if [ "$code" == "0" ]
220 info "Successfully finished hotcopy of mysql database $db"
223 warning "Failed to hotcopy mysql database $db"
230 ##########################################################################
233 if [ "$sqldump" == "yes" ]
235 info "Initializing SQL dump method"
236 if [ "$databases" == "all" ]
240 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
243 fatal "Authentication problem, maybe user/password is wrong"
246 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
249 fatal "Authentication problem, maybe user/password is wrong"
258 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
260 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
262 debug "su $user -c '$execstr'"
265 output=`su $user -c "$execstr" 2>&1`
267 if [ "$code" == "0" ]
270 info "Successfully finished dump of mysql database $db"
273 warning "Failed to dump mysql databases $db"
278 if [ "$compress" == "yes" ]
280 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
285 # clean up tmp config file
286 if [ "$dbusername" != "" ]
288 ## clean up tmp config file
293 debug "mv $tmpcnf $mycnf"