2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
10 getconf dbhost localhost
17 [ -d $backupdir ] || mkdir -p $backupdir
18 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
19 hotdir="$backupdir/hotcopy"
20 dumpdir="$backupdir/sqldump"
21 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
22 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
25 # (we do this because we don't want to have to specify the password on the command line
26 # because then anyone would be able to see it with a 'ps aux'. instead, we create a
27 # temporary ~/.my.cnf in root's home directory).
29 if [ "$dbusername" != "" ]; then
30 home=`grep '^root' /etc/passwd | awk -F: '{print $6}'`
31 [ -d $home ] || fatal "Can't find root's home directory ($home)."
33 if [ -f $mycnf ]; then
35 tmpcnf="$home/my.cnf.disable"
36 debug 0 "mv $mycnf $tmpcnf"
42 # auto generated backupninja mysql conf
60 if [ "$hotcopy" == "yes" ]; then
61 if [ "$databases" == "all" ]; then
62 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
63 debug 0 "su $user -c '$execstr'"
65 output=`su $user -c "$execstr" 2>&1`
67 if [ "$code" == "0" ]; then
69 debug 1 "Successfully finished hotcopy of all mysql databases"
72 debug 2 "Failed to hotcopy all mysql databases"
76 for db in $databases; do
77 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
78 debug 0 "su $user -c '$execstr'"
80 output=`su $user -c "$execstr" 2>&1`
82 if [ "$code" == "0" ]; then
84 debug 1 "Successfully finished hotcopy of mysql database $db"
87 debug 2 "Failed to hotcopy mysql database $db"
96 if [ "$sqldump" == "yes" ]; then
97 if [ "$databases" == "all" ]; then
98 databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
101 for db in $databases; do
102 execstr="$MYSQLDUMP --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
103 debug 0 "su $user -c '$execstr'"
105 output=`su $user -c "$execstr" 2>&1`
107 if [ "$code" == "0" ]; then
109 debug 1 "Successfully finished dump of mysql database $db"
112 debug 2 "Failed to dump mysql databases $db"
117 if [ "$compress" == "yes" ]; then
118 output=`$GZIP -f $dumpdir/*.sql 2>&1`
123 if [ "$dbusername" != "" ]; then
124 ## clean up tmp config file
127 if [ -f "$tmpcnf" ]; then
128 debug 0 "mv $tmpcnf $mycnf"