added double quotes around password in .my.cnf
[matthijs/upstream/backupninja.git] / handlers / mysql
1 #
2 # mysql handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/mysql
6 getconf databases all
7 getconf dbhost localhost
8 getconf hotcopy no
9 getconf sqldump no
10 getconf compress yes
11 getconf vsname
12
13 # authentication:
14 getconf user
15 getconf dbusername
16 getconf dbpassword
17 getconf configfile /etc/mysql/debian.cnf
18
19 if [ "$user" == "" ]; then
20         userset=false;
21         user=root;
22 else
23         userset=true;
24         userhome=`grep "^$user:" /etc/passwd | awk -F: '{print $6}'`
25         [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
26 fi
27
28 # If vservers are configured, decide if the handler should
29 # use them or if it should just operate on the host
30
31 if [ "$VSERVERS" = "yes" ]
32 then
33         if [ ! -z $vsname ]
34         then            
35                 info "Using vserver '$vsname'"
36                 usevserver=1
37         else
38                 info "No vserver name specified, actions will be performed on the host"
39         fi
40 fi
41
42 # Check to make sure that the specified vserver exists
43 if [ $usevserver ]
44 then
45         vroot="$VROOTDIR/$vsname"
46         [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
47 fi
48         
49 # create backup dirs, the vroot variable will be empty if no vsname was specified
50 # and will proceed to operate on the host
51 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
52 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
53 hotdir="$backupdir/hotcopy"
54 dumpdir="$backupdir/sqldump"
55
56 if [ $usevserver ]
57 then
58         [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
59         [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
60 else
61         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
62         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
63 fi
64
65 #######################################################################
66 ## AUTHENTICATION
67
68 #
69 # one of three authentication methods:
70 # 1. setting the user, so that /home/user/.my.cnf is used.
71 # 2. specifying the user and password in the handler config,
72 #    which generates a temporary .my.cnf in /root/.my.cnf
73 # 3. specify the config file with --defaults-file
74 #    (this option DOESN'T WORK WITH MYSQLHOTCOPY)
75 #
76
77 # create .my.cnf
78 # only if dbusername and dbpassword specified.
79 # we create a tmp file because we don't want to
80 # specify the password on the command line.
81
82 defaultsfile=""
83 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
84         home=`grep '^root:' /etc/passwd | awk -F: '{print $6}'`
85         [ -d $home ] || fatal "Can't find root's home directory ($home)."
86         mycnf="$home/.my.cnf"
87         if [ -f $mycnf ]; then
88                 # rename temporarily
89                 tmpcnf="$home/my.cnf.disable"
90                 debug "mv $mycnf $tmpcnf"
91                 mv $mycnf $tmpcnf
92         fi
93         oldmask=`umask`
94         umask 077
95         cat > $mycnf <<EOF
96 # auto generated backupninja mysql conf
97 [mysql]
98 user=$dbusername
99 password="$dbpassword"
100
101 [mysqldump]
102 user=$dbusername
103 password="$dbpassword"
104
105 [mysqlhotcopy]
106 user=$dbusername
107 password="$dbpassword"
108 EOF
109         umask $oldmask
110         defaultsfile="--defaults-file=$mycnf"
111 elif [ "$userset" == "false" ]; then
112         # if user is set, don't use $configfile
113         defaultsfile="--defaults-file=$configfile"
114 fi
115
116 #######################################################################
117 ## HOT COPY
118
119 if [ "$hotcopy" == "yes" ]; then 
120         if [ "$databases" == "all" ]; then
121                 if [ $usevserver ]
122                 then
123                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
124                 else
125                         execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
126                 fi
127                 debug "su $user -c '$execstr'"
128                 if [ ! $test ]; then
129                         output=`su $user -c "$execstr" 2>&1`
130                         code=$?
131                         if [ "$code" == "0" ]; then
132                                 debug $output
133                                 info "Successfully finished hotcopy of all mysql databases"
134                         else
135                                 warning $output
136                                 warning "Failed to hotcopy all mysql databases"
137                         fi
138                 fi
139         else    
140                 for db in $databases; do
141                         if [ $usevserver ]
142                         then
143                                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
144                         else
145                                 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
146                         fi
147                         debug "su $user -c '$execstr'"
148                         if [ ! $test ]; then
149                                 output=`su $user -c "$execstr" 2>&1`
150                                 code=$?
151                                 if [ "$code" == "0" ]; then
152                                         debug $output
153                                         info "Successfully finished hotcopy of mysql database $db"
154                                 else
155                                         warning $output
156                                         warning "Failed to hotcopy mysql database $db"
157                                 fi
158                         fi
159                 done
160         fi
161 fi
162
163 ##########################################################################
164 ## SQL DUMP
165
166 if [ "$sqldump" == "yes" ]; then
167         if [ "$databases" == "all" ]; then
168                 if [ $usevserver ]
169                 then
170                         databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
171                 else
172                         databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
173                 fi
174         fi
175
176         for db in $databases; do
177                 if [ $usevserver ]
178                 then
179                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $vroot$dumpdir/${db}.sql"
180                 else
181                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $db > $dumpdir/${db}.sql"
182                 fi
183                 debug "su $user -c '$execstr'"
184                 if [ ! $test ]; then
185                         output=`su $user -c "$execstr" 2>&1`
186                         code=$?
187                         if [ "$code" == "0" ]; then
188                                 debug $output
189                                 info "Successfully finished dump of mysql database $db"
190                         else
191                                 warning $output
192                                 warning "Failed to dump mysql databases $db"
193                         fi
194                 fi
195         done
196         
197         if [ "$compress" == "yes" ]; then
198                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
199                 debug $output
200         fi
201 fi
202
203 # clean up tmp config file
204 if [ "$dbusername" != "" ]; then
205         ## clean up tmp config file
206         debug "rm $mycnf"
207         rm $mycnf
208         if [ -f "$tmpcnf" ]; then
209                 debug "mv $tmpcnf $mycnf"
210                 mv $tmpcnf $mycnf
211         fi
212 fi
213
214 return 0