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 info "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 if [ "$user" == "" ]; then
55 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
56 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
59 ## Prepare ignore part of the command
60 ## This only works for mysqldump at the moment
64 ignore="$ignore --ignore-table=$i"
67 # create backup dirs, vroot variable will be empty if no vsname was specified
68 # and will proceed to operate on the host
69 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
70 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
71 hotdir="$backupdir/hotcopy"
72 dumpdir="$backupdir/sqldump"
76 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
77 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
79 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
80 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
83 #######################################################################
87 # one of three authentication methods:
88 # 1. setting the user, so that /home/user/.my.cnf is used.
89 # 2. specifying the user and password in the handler config,
90 # which generates a temporary .my.cnf in /root/.my.cnf
91 # 3. specify the config file with --defaults-file
92 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
96 # only if dbusername and dbpassword specified.
97 # we create a tmp file because we don't want to
98 # specify the password on the command line.
101 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
105 home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
107 home=`getent passwd "root" | awk -F: '{print $6}'`
109 [ -d $home ] || fatal "Can't find root's home directory ($home)."
110 mycnf="$home/.my.cnf"
114 tmpcnf="$home/my.cnf.disable"
115 debug "mv $mycnf $tmpcnf"
121 # auto generated backupninja mysql conf
124 password="$dbpassword"
128 password="$dbpassword"
132 password="$dbpassword"
135 defaultsfile="--defaults-file=$mycnf"
136 elif [ "$userset" == "false" ]; then
137 # if user is set, don't use $configfile
138 defaultsfile="--defaults-file=$configfile"
141 #######################################################################
144 if [ "$hotcopy" == "yes" ]; then
145 if [ "$databases" == "all" ]; then
148 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
150 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
152 debug "su $user -c '$execstr'"
154 output=`su $user -c "$execstr" 2>&1`
156 if [ "$code" == "0" ]; then
158 info "Successfully finished hotcopy of all mysql databases"
161 warning "Failed to hotcopy all mysql databases"
165 for db in $databases; do
168 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
170 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
172 debug "su $user -c '$execstr'"
174 output=`su $user -c "$execstr" 2>&1`
176 if [ "$code" == "0" ]; then
178 info "Successfully finished hotcopy of mysql database $db"
181 warning "Failed to hotcopy mysql database $db"
188 ##########################################################################
191 if [ "$sqldump" == "yes" ]; then
192 if [ "$databases" == "all" ]; then
195 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
198 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
201 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
204 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
209 for db in $databases; do
212 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
214 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
216 debug "su $user -c '$execstr'"
218 output=`su $user -c "$execstr" 2>&1`
220 if [ "$code" == "0" ]; then
222 info "Successfully finished dump of mysql database $db"
225 warning "Failed to dump mysql databases $db"
230 if [ "$compress" == "yes" ]; then
231 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
236 # clean up tmp config file
237 if [ "$dbusername" != "" ]; then
238 ## clean up tmp config file
241 if [ -f "$tmpcnf" ]; then
242 debug "mv $tmpcnf $mycnf"