2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
8 getconf dbhost localhost
18 getconf configfile /etc/mysql/debian.cnf
20 if [ "$user" == "" ]; then
25 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
26 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
29 ## Prepare ignore part of the command
30 ## This only works for mysqldump at the moment
34 ignore="$ignore --ignore-table=$i"
37 # If vservers are configured, decide if the handler should
38 # use them or if it should just operate on the host
40 if [ "$vservers" = "yes" ]
44 info "Using vserver '$vsname'"
47 info "No vserver name specified, actions will be performed on the host"
51 # If needed, make sure that the specified vserver exists and is running.
54 info "Examining vserver '$vsname'"
56 vroot="$VROOTDIR/$vsname"
57 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
59 $VSERVERINFO -q $vsname RUNNING
62 fatal "vserver $vsname is not running."
66 # create backup dirs, vroot variable will be empty if no vsname was specified
67 # and will proceed to operate on the host
68 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
69 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
70 hotdir="$backupdir/hotcopy"
71 dumpdir="$backupdir/sqldump"
75 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
76 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
78 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
79 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
82 #######################################################################
86 # one of three authentication methods:
87 # 1. setting the user, so that /home/user/.my.cnf is used.
88 # 2. specifying the user and password in the handler config,
89 # which generates a temporary .my.cnf in /root/.my.cnf
90 # 3. specify the config file with --defaults-file
91 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
95 # only if dbusername and dbpassword specified.
96 # we create a tmp file because we don't want to
97 # specify the password on the command line.
100 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
104 home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
106 home=`getent passwd "root" | awk -F: '{print $6}'`
108 [ -d $home ] || fatal "Can't find root's home directory ($home)."
109 mycnf="$home/.my.cnf"
113 tmpcnf="$home/my.cnf.disable"
114 debug "mv $mycnf $tmpcnf"
120 # auto generated backupninja mysql conf
123 password="$dbpassword"
127 password="$dbpassword"
131 password="$dbpassword"
134 defaultsfile="--defaults-file=$mycnf"
135 elif [ "$userset" == "false" ]; then
136 # if user is set, don't use $configfile
137 defaultsfile="--defaults-file=$configfile"
140 #######################################################################
143 if [ "$hotcopy" == "yes" ]; then
144 if [ "$databases" == "all" ]; then
147 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
149 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
151 debug "su $user -c '$execstr'"
153 output=`su $user -c "$execstr" 2>&1`
155 if [ "$code" == "0" ]; then
157 info "Successfully finished hotcopy of all mysql databases"
160 warning "Failed to hotcopy all mysql databases"
164 for db in $databases; do
167 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
169 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
171 debug "su $user -c '$execstr'"
173 output=`su $user -c "$execstr" 2>&1`
175 if [ "$code" == "0" ]; then
177 info "Successfully finished hotcopy of mysql database $db"
180 warning "Failed to hotcopy mysql database $db"
187 ##########################################################################
190 if [ "$sqldump" == "yes" ]; then
191 if [ "$databases" == "all" ]; then
194 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
197 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
200 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
203 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
208 for db in $databases; do
211 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
213 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
215 debug "su $user -c '$execstr'"
217 output=`su $user -c "$execstr" 2>&1`
219 if [ "$code" == "0" ]; then
221 info "Successfully finished dump of mysql database $db"
224 warning "Failed to dump mysql databases $db"
229 if [ "$compress" == "yes" ]; then
230 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
235 # clean up tmp config file
236 if [ "$dbusername" != "" ]; then
237 ## clean up tmp config file
240 if [ -f "$tmpcnf" ]; then
241 debug "mv $tmpcnf $mycnf"