2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
10 getconf dbhost localhost
16 # If vservers are configured, decide if the handler should
17 # use them or if it should just operate on the host
18 if [ "$VSERVERS" = "yes" ]
22 info "Using vserver '$vsname'"
25 info "No vserver name specified, actions will be performed on the host"
29 # Check to make sure that the specified vserver exists
32 vroot="$VROOTDIR/$vsname"
33 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
36 # create backup dirs, the vroot variable will be empty if no vsname was specified
37 # and will proceed to operate on the host
38 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
39 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
40 hotdir="$backupdir/hotcopy"
41 dumpdir="$backupdir/sqldump"
45 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
46 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
48 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
49 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
53 # (we do this because we don't want to have to specify the password on the command line
54 # because then anyone would be able to see it with a 'ps aux'. instead, we create a
55 # temporary ~/.my.cnf in root's home directory).
57 if [ "$dbusername" != "" ]; then
58 home=`grep '^root:' $vroot/etc/passwd | awk -F: '{print $6}'`
59 [ -d $home ] || fatal "Can't find root's home directory ($home)."
60 mycnf="$vroot$home/.my.cnf"
61 if [ -f $mycnf ]; then
63 tmpcnf="$home/my.cnf.disable"
64 debug "mv $mycnf $tmpcnf"
70 # auto generated backupninja mysql conf
88 if [ "$hotcopy" == "yes" ]; then
89 if [ "$databases" == "all" ]; then
92 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
94 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
96 debug "su $user -c '$execstr'"
98 output=`su $user -c "$execstr" 2>&1`
100 if [ "$code" == "0" ]; then
102 info "Successfully finished hotcopy of all mysql databases"
105 warning "Failed to hotcopy all mysql databases"
109 for db in $databases; do
112 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
114 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
116 debug "su $user -c '$execstr'"
118 output=`su $user -c "$execstr" 2>&1`
120 if [ "$code" == "0" ]; then
122 info "Successfully finished hotcopy of mysql database $db"
125 warning "Failed to hotcopy mysql database $db"
134 if [ "$sqldump" == "yes" ]; then
135 if [ "$databases" == "all" ]; then
138 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL" | grep -v Database`
140 databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
144 for db in $databases; do
147 execstr="$VSERVER $vsname exec $MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
149 execstr="$MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
151 debug "su $user -c '$execstr'"
153 output=`su $user -c "$execstr" 2>&1`
155 if [ "$code" == "0" ]; then
157 info "Successfully finished dump of mysql database $db"
160 warning "Failed to dump mysql databases $db"
165 if [ "$compress" == "yes" ]; then
166 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
171 if [ "$dbusername" != "" ]; then
172 ## clean up tmp config file
175 if [ -f "$tmpcnf" ]; then
176 debug "mv $tmpcnf $mycnf"