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
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 info "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 info "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 info "Home set to: $home"
126 home=`getent passwd "root" | awk -F: '{print $6}'`
127 info "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" ]; then
165 if [ "$databases" == "all" ]; then
168 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
170 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $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 all mysql databases"
181 warning "Failed to hotcopy all mysql databases"
185 for db in $databases; do
188 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
190 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
192 debug "su $user -c '$execstr'"
194 output=`su $user -c "$execstr" 2>&1`
196 if [ "$code" == "0" ]; then
198 info "Successfully finished hotcopy of mysql database $db"
201 warning "Failed to hotcopy mysql database $db"
208 ##########################################################################
211 if [ "$sqldump" == "yes" ]; then
212 if [ "$databases" == "all" ]; then
215 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
218 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
221 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
224 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
229 for db in $databases; do
232 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
234 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
236 debug "su $user -c '$execstr'"
238 output=`su $user -c "$execstr" 2>&1`
240 if [ "$code" == "0" ]; then
242 info "Successfully finished dump of mysql database $db"
245 warning "Failed to dump mysql databases $db"
250 if [ "$compress" == "yes" ]; then
251 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
256 # clean up tmp config file
257 if [ "$dbusername" != "" ]; then
258 ## clean up tmp config file
261 if [ -f "$tmpcnf" ]; then
262 debug "mv $tmpcnf $mycnf"