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 if [ "$user" == "" ]; then
57 userhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
60 fatal "User $user not found in /etc/passwd"
62 userhome="$vroot$userhome"
63 debug "User home set to: $userhome"
64 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
66 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
69 fatal "User $user not found in /etc/passwd"
71 debug "User home set to: $userhome"
72 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
76 ## Prepare ignore part of the command
77 ## This only works for mysqldump at the moment
81 ignore="$ignore --ignore-table=$i"
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"
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
96 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
97 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
100 #######################################################################
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)
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.
118 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
122 home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
124 debug "Home set to: $home"
126 home=`getent passwd "root" | awk -F: '{print $6}'`
127 debug "Home set to: $home"
129 [ -d $home ] || fatal "Can't find root's home directory ($home)."
130 mycnf="$home/.my.cnf"
134 tmpcnf="$home/my.cnf.disable"
135 debug "mv $mycnf $tmpcnf"
141 # auto generated backupninja mysql conf
144 password="$dbpassword"
148 password="$dbpassword"
152 password="$dbpassword"
155 defaultsfile="--defaults-file=$mycnf"
156 elif [ "$userset" == "false" ]; then
157 # if user is set, don't use $mycnf
158 defaultsfile="--defaults-file=$configfile"
161 #######################################################################
164 if [ "$hotcopy" == "yes" ]
166 info "Initializing hotcopy method"
167 if [ "$databases" == "all" ]
171 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
173 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
175 debug "su $user -c '$execstr'"
178 output=`su $user -c "$execstr" 2>&1`
180 if [ "$code" == "0" ]
183 info "Successfully finished hotcopy of all mysql databases"
186 warning "Failed to hotcopy all mysql databases"
194 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
196 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
198 debug "su $user -c '$execstr'"
201 output=`su $user -c "$execstr" 2>&1`
203 if [ "$code" == "0" ]
206 info "Successfully finished hotcopy of mysql database $db"
209 warning "Failed to hotcopy mysql database $db"
216 ##########################################################################
219 if [ "$sqldump" == "yes" ]
221 info "Initializing SQL dump method"
222 if [ "$databases" == "all" ]
226 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
229 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
232 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
235 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
244 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
246 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
248 debug "su $user -c '$execstr'"
251 output=`su $user -c "$execstr" 2>&1`
253 if [ "$code" == "0" ]
256 info "Successfully finished dump of mysql database $db"
259 warning "Failed to dump mysql databases $db"
264 if [ "$compress" == "yes" ]
266 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
271 # clean up tmp config file
272 if [ "$dbusername" != "" ]
274 ## clean up tmp config file
279 debug "mv $tmpcnf $mycnf"