added support for --defaults-file
[matthijs/upstream/backupninja.git] / handlers / mysql
1 #
2 # mysql handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/mysql
6 getconf configfile /etc/mysql/debian.cnf
7 getconf databases all
8 getconf compress yes
9 getconf dbusername
10 getconf dbpassword
11 getconf dbhost localhost
12 getconf hotcopy yes
13 getconf sqldump no
14 getconf user root
15 getconf vsname
16
17 # If vservers are configured, decide if the handler should
18 # use them or if it should just operate on the host
19
20 if [ "$VSERVERS" = "yes" ]
21 then
22         if [ ! -z $vsname ]
23         then            
24                 info "Using vserver '$vsname'"
25                 usevserver=1
26         else
27                 info "No vserver name specified, actions will be performed on the host"
28         fi
29 fi
30
31 # Check to make sure that the specified vserver exists
32 if [ $usevserver ]
33 then
34         vroot="$VROOTDIR/$vsname"
35         [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
36 fi
37         
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"
44
45 if [ $usevserver ]
46 then
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
49 else
50         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
51         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
52 fi
53
54 # create .my.cnf
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.
58
59 defaultsfile=""
60 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
61         mycnf="/tmp/backupninja.$$.my.cnf"
62         if [ -f $mycnf ]; then
63                 debug "rm $mycnf"
64                 rm $mycnf
65         fi
66         oldmask=`umask`
67         umask 077
68         cat > $mycnf <<EOF
69 # auto generated backupninja mysql conf
70 [mysql]
71 user=$dbusername
72 password=$dbpassword
73
74 [mysqldump]
75 user=$dbusername
76 password=$dbpassword
77
78 [mysqlhotcopy]
79 user=$dbusername
80 password=$dbpassword
81 EOF
82         umask $oldmask
83         defaultsfile="--defaults-file $mycnf"
84 else
85         # otherwise, use $configfile
86         defaultsfile="--defaults-file $configfile"
87 fi
88
89 ## HOT COPY
90
91 if [ "$hotcopy" == "yes" ]; then 
92         if [ "$databases" == "all" ]; then
93                 if [ $usevserver ]
94                 then
95                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY $defaultsfile --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
96                 else
97                         execstr="$MYSQLHOTCOPY $defaultsfile --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
98                 fi
99                 debug "su $user -c '$execstr'"
100                 if [ ! $test ]; then
101                         output=`su $user -c "$execstr" 2>&1`
102                         code=$?
103                         if [ "$code" == "0" ]; then
104                                 debug $output
105                                 info "Successfully finished hotcopy of all mysql databases"
106                         else
107                                 warning $output
108                                 warning "Failed to hotcopy all mysql databases"
109                         fi
110                 fi
111         else    
112                 for db in $databases; do
113                         if [ $usevserver ]
114                         then
115                                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY $defaultsfile --allowold $db $hotdir"
116                         else
117                                 execstr="$MYSQLHOTCOPY $defaultsfile --allowold $db $hotdir"
118                         fi
119                         debug "su $user -c '$execstr'"
120                         if [ ! $test ]; then
121                                 output=`su $user -c "$execstr" 2>&1`
122                                 code=$?
123                                 if [ "$code" == "0" ]; then
124                                         debug $output
125                                         info "Successfully finished hotcopy of mysql database $db"
126                                 else
127                                         warning $output
128                                         warning "Failed to hotcopy mysql database $db"
129                                 fi
130                         fi
131                 done
132         fi
133 fi
134         
135 ## SQL DUMP
136
137 if [ "$sqldump" == "yes" ]; then
138         if [ "$databases" == "all" ]; then
139                 if [ $usevserver ]
140                 then
141                         databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
142                 else
143                         databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
144                 fi
145         fi
146
147         for db in $databases; do
148                 if [ $usevserver ]
149                 then
150                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
151                 else
152                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
153                 fi
154                 debug "su $user -c '$execstr'"
155                 if [ ! $test ]; then
156                         output=`su $user -c "$execstr" 2>&1`
157                         code=$?
158                         if [ "$code" == "0" ]; then
159                                 debug $output
160                                 info "Successfully finished dump of mysql database $db"
161                         else
162                                 warning $output
163                                 warning "Failed to dump mysql databases $db"
164                         fi
165                 fi
166         done
167         
168         if [ "$compress" == "yes" ]; then
169                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
170                 debug $output
171         fi
172 fi
173
174 # clean up tmp config file
175 if [ -f "$mycnf" ]; then
176         debug "rm $mycnf"
177         rm $mycnf
178 fi
179
180 return 0