2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
6 getconf configfile /etc/mysql/debian.cnf
11 getconf dbhost localhost
17 # If vservers are configured, decide if the handler should
18 # use them or if it should just operate on the host
20 if [ "$VSERVERS" = "yes" ]
24 info "Using vserver '$vsname'"
27 info "No vserver name specified, actions will be performed on the host"
31 # Check to make sure that the specified vserver exists
34 vroot="$VROOTDIR/$vsname"
35 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
38 # create backup dirs, the vroot variable will be empty if no vsname was specified
39 # and will proceed to operate on the host
40 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
41 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
42 hotdir="$backupdir/hotcopy"
43 dumpdir="$backupdir/sqldump"
47 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
48 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
50 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
51 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
55 # only if dbusername and dbpassword specified.
56 # we create a tmp file because we don't want to have to
57 # specify the password on the command line.
60 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
61 mycnf="/tmp/backupninja.$$.my.cnf"
62 if [ -f $mycnf ]; then
69 # auto generated backupninja mysql conf
83 defaultsfile="--defaults-file $mycnf"
85 # otherwise, use $configfile
86 defaultsfile="--defaults-file $configfile"
91 if [ "$hotcopy" == "yes" ]; then
92 if [ "$databases" == "all" ]; then
95 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY $defaultsfile --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
97 execstr="$MYSQLHOTCOPY $defaultsfile --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
99 debug "su $user -c '$execstr'"
101 output=`su $user -c "$execstr" 2>&1`
103 if [ "$code" == "0" ]; then
105 info "Successfully finished hotcopy of all mysql databases"
108 warning "Failed to hotcopy all mysql databases"
112 for db in $databases; do
115 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY $defaultsfile --allowold $db $hotdir"
117 execstr="$MYSQLHOTCOPY $defaultsfile --allowold $db $hotdir"
119 debug "su $user -c '$execstr'"
121 output=`su $user -c "$execstr" 2>&1`
123 if [ "$code" == "0" ]; then
125 info "Successfully finished hotcopy of mysql database $db"
128 warning "Failed to hotcopy mysql database $db"
137 if [ "$sqldump" == "yes" ]; then
138 if [ "$databases" == "all" ]; then
141 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
143 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
147 for db in $databases; do
150 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
152 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
154 debug "su $user -c '$execstr'"
156 output=`su $user -c "$execstr" 2>&1`
158 if [ "$code" == "0" ]; then
160 info "Successfully finished dump of mysql database $db"
163 warning "Failed to dump mysql databases $db"
168 if [ "$compress" == "yes" ]; then
169 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
174 # clean up tmp config file
175 if [ -f "$mycnf" ]; then