Forgot to note in the changelog
[matthijs/upstream/backupninja-vserver.git] / handlers / mysql
1 #
2 # mysql handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/mysql
6 getconf databases all
7 getconf compress yes
8 getconf dbusername
9 getconf dbpassword
10 getconf dbhost localhost
11 getconf hotcopy no
12 getconf sqldump no
13 getconf user root
14
15 # create backup dirs
16
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
23
24 # create .my.cnf
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).
28
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)."
32         mycnf="$home/.my.cnf"
33         if [ -f $mycnf ]; then
34                 # rename temporarily
35                 tmpcnf="$home/my.cnf.disable"
36                 debug "mv $mycnf $tmpcnf"
37                 mv $mycnf $tmpcnf
38         fi
39         oldmask=`umask`
40         umask 077
41         cat > $mycnf <<EOF
42 # auto generated backupninja mysql conf
43 [mysql]
44 user=$dbusername
45 password=$dbpassword
46
47 [mysqldump]
48 user=$dbusername
49 password=$dbpassword
50
51 [mysqlhotcopy]
52 user=$dbusername
53 password=$dbpassword
54 EOF
55         umask $oldmask
56 fi
57         
58 ## HOT COPY
59
60 if [ "$hotcopy" == "yes" ]; then 
61         if [ "$databases" == "all" ]; then
62                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
63                 debug "su $user -c '$execstr'"
64                 if [ ! $test ]; then
65                         output=`su $user -c "$execstr" 2>&1`
66                         code=$?
67                         if [ "$code" == "0" ]; then
68                                 debug $output
69                                 info "Successfully finished hotcopy of all mysql databases"
70                         else
71                                 warning $output
72                                 warning "Failed to hotcopy all mysql databases"
73                         fi
74                 fi
75         else    
76                 for db in $databases; do
77                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
78                         debug "su $user -c '$execstr'"
79                         if [ ! $test ]; then
80                                 output=`su $user -c "$execstr" 2>&1`
81                                 code=$?
82                                 if [ "$code" == "0" ]; then
83                                         debug $output
84                                         info "Successfully finished hotcopy of mysql database $db"
85                                 else
86                                         warning $output
87                                         warning "Failed to hotcopy mysql database $db"
88                                 fi
89                         fi
90                 done
91         fi
92 fi
93         
94 ## SQL DUMP
95
96 if [ "$sqldump" == "yes" ]; then
97         if [ "$databases" == "all" ]; then
98                 databases=`echo 'show databases' | su $user -c "$MYSQL" | grep -v Database`
99         fi
100
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 "su $user -c '$execstr'"
104                 if [ ! $test ]; then
105                         output=`su $user -c "$execstr" 2>&1`
106                         code=$?
107                         if [ "$code" == "0" ]; then
108                                 debug $output
109                                 info "Successfully finished dump of mysql database $db"
110                         else
111                                 warning $output
112                                 warning "Failed to dump mysql databases $db"
113                         fi
114                 fi
115         done
116         
117         if [ "$compress" == "yes" ]; then
118                 output=`$GZIP -f $dumpdir/*.sql 2>&1`
119                 debug $output
120         fi
121 fi
122
123 if [ "$dbusername" != "" ]; then
124         ## clean up tmp config file
125         debug "rm $mycnf"
126         rm $mycnf
127         if [ -f "$tmpcnf" ]; then
128                 debug "mv $tmpcnf $mycnf"
129                 mv $tmpcnf $mycnf
130         fi
131 fi
132
133 return 0