typo in backupninja.1
[matthijs/upstream/backupninja.git] / handlers / mysql
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # mysql handler script for backupninja
4 #
5
6 getconf backupdir /var/backups/mysql
7 getconf databases all
8 getconf ignores
9 getconf dbhost localhost
10 getconf hotcopy no
11 getconf sqldump no
12 getconf compress yes
13 getconf vsname
14
15 # authentication:
16 getconf user
17 getconf dbusername
18 getconf dbpassword
19 getconf configfile /etc/mysql/debian.cnf
20
21
22 # Decide if the handler should operate on a vserver or on the host.
23 # In the former case, check that $vsname exists and is running.
24 local usevserver=no
25 local vroot
26 if [ $vservers_are_available = yes ]; then
27    if [ -n "$vsname" ]; then
28       # does it exist ?
29       if ! vservers_exist "$vsname" ; then
30          fatal "The vserver given in vsname ($vsname) does not exist."
31       fi
32       # is it running ?
33       $VSERVERINFO -q $vsname RUNNING
34       if [ $? -ne 0 ]; then
35          fatal "The vserver $vsname is not running."
36       fi
37       # everything ok
38       info "Using vserver '$vsname'."
39       usevserver=yes
40       vroot="$VROOTDIR/$vsname"
41    else
42       info "No vserver name specified, actions will be performed on the host."
43    fi
44 fi
45
46 ## Prepare ignore part of the command
47 ## This only works for mysqldump at the moment
48
49 ignore=''
50 for i in $ignores; do
51        ignore="$ignore --ignore-table=$i"
52 done
53         
54 # create backup dirs, $vroot will be empty if no vsname was specified
55 # and we will instead proceed to operate on the host
56 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
57 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
58 hotdir="$backupdir/hotcopy"
59 dumpdir="$backupdir/sqldump"
60
61 if [ $usevserver = yes ]
62 then
63         [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
64         [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
65 else
66         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
67         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
68 fi
69
70 #######################################################################
71 ## AUTHENTICATION
72
73 #
74 # one of three authentication methods:
75 # 1. setting the user, so that /home/user/.my.cnf is used.
76 # 2. specifying the user and password in the handler config,
77 #    which generates a temporary .my.cnf in /root/.my.cnf
78 # 3. specify the config file with --defaults-extra-file
79 #    (this option DOESN'T WORK WITH MYSQLHOTCOPY)
80 #
81
82 # create .my.cnf
83 # only if dbusername and dbpassword specified.
84 # we create a tmp file because we don't want to
85 # specify the password on the command line.
86
87 defaultsfile=""
88
89 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
90 then
91     if [ $usevserver = yes ]
92     then
93         vhome=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
94         home="$vroot$vhome"
95     else
96         home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
97     fi
98
99     [ -d $home ] || fatal "Can't find root's home directory ($home)."
100     
101     mycnf="$home/.my.cnf"
102     
103     if [ -f $mycnf ]
104     then
105         # rename temporarily
106         tmpcnf="$home/my.cnf.disable"
107         debug "mv $mycnf $tmpcnf"
108         mv $mycnf $tmpcnf
109     fi
110     
111     oldmask=`umask`
112     umask 077
113     cat > $mycnf <<EOF
114 # auto generated backupninja mysql conf
115 [mysql]
116 host=$dbhost
117 user=$dbusername
118 password="$dbpassword"
119
120 [mysqldump]
121 host=$dbhost
122 user=$dbusername
123 password="$dbpassword"
124
125 [mysqlhotcopy]
126 host=$dbhost
127 user=$dbusername
128 password="$dbpassword"
129 EOF
130         umask $oldmask
131         if [ $usevserver = yes ] 
132         then
133             defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
134         else
135             defaultsfile="--defaults-extra-file=$mycnf"
136         fi
137 fi
138
139 # if a user is not set, use $configfile, otherwise use $mycnf
140 if [ "$user" == "" ]; then
141         user=root;
142         defaultsfile="--defaults-extra-file=$configfile"
143 else
144         userset=true;
145         if [ $usevserver = yes ]
146         then
147             vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
148             if [ $? -eq 2 ]
149             then
150                 fatal "User $user not found in /etc/passwd"
151             fi
152             userhome="$vroot$vuserhome"
153         else
154             userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
155             if [ $? -eq 2 ]
156             then
157                 fatal "User $user not found in /etc/passwd"
158             fi
159         fi
160         
161         debug "User home set to: $userhome"
162         [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
163         defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
164         debug "using $defaultsfile"
165 fi
166
167 #######################################################################
168 ## HOT COPY
169
170 if [ "$hotcopy" == "yes" ]
171 then
172     info "Initializing hotcopy method"
173     if [ "$databases" == "all" ]
174     then
175         if [ $usevserver = yes ]
176         then
177                 info "dbhost: $dbhost"
178                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
179         else
180                 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
181         fi
182         debug "su $user -c \"$execstr\""
183         if [ ! $test ]
184         then
185                 output=`su $user -c "$execstr" 2>&1`
186                 code=$?
187                 if [ "$code" == "0" ]
188                 then
189                         debug $output
190                         info "Successfully finished hotcopy of all mysql databases"
191                 else
192                         warning $output
193                         warning "Failed to hotcopy all mysql databases"
194                 fi
195         fi
196     else        
197         for db in $databases
198         do
199                 if [ $usevserver = yes ]
200                 then
201                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
202                 else
203                         execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
204                 fi
205                 debug 'su $user -c \"$execstr\"'
206                 if [ ! $test ]
207                 then
208                         output=`su $user -c "$execstr" 2>&1`
209                         code=$?
210                         if [ "$code" == "0" ]
211                         then
212                                 debug $output
213                                 info "Successfully finished hotcopy of mysql database $db"
214                         else
215                                 warning $output
216                                 warning "Failed to hotcopy mysql database $db"
217                         fi
218                 fi
219         done
220      fi
221 fi
222
223 ##########################################################################
224 ## SQL DUMP
225
226 if [ "$sqldump" == "yes" ]
227 then
228     info "Initializing SQL dump method"
229     if [ "$databases" == "all" ]
230     then
231         if [ $usevserver = yes ]
232         then
233             debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
234             databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
235             if [ $? -ne 0 ]
236             then
237                 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
238             fi
239         else
240                 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
241                 if [ $? -ne 0 ]
242                 then
243                     fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
244                 fi
245         fi
246 fi
247
248         for db in $databases
249         do
250                 if [ $usevserver = yes ]
251                 then
252                    # Test to make sure mysqld is running, if it is not sqldump will not work
253                    $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping"
254                    if [ $? -ne 0 ]; then
255                       fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
256                    fi
257                    if [ "$compress" == "yes" ]; then
258                       execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $vroot$dumpdir/${db}.sql.gz"
259                    else
260                       execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -r $vroot$dumpdir/${db}.sql"
261                    fi
262                 else
263                    # Test to make sure mysqld is running, if it is not sqldump will not work
264                    su $user -c "$MYSQLADMIN $defaultsfile ping"
265                    if [ $? -ne 0 ]; then
266                       fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
267                    fi
268                    if [ "$compress" == "yes" ]; then
269                       execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $dumpdir/${db}.sql.gz"
270                    else
271                       execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -r $dumpdir/${db}.sql"
272                    fi
273                 fi
274                 debug "su $user -c \"$execstr\""
275                 if [ ! $test ]
276                 then
277                         output=`su $user -c "$execstr" 2>&1`
278                         code=$?
279                         if [ "$code" == "0" ]
280                         then
281                                 debug $output
282                                 info "Successfully finished dump of mysql database $db"
283                         else
284                                 warning $output
285                                 warning "Failed to dump mysql databases $db"
286                         fi
287                 fi
288         done
289 fi
290
291 # clean up tmp config file
292 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
293 then
294         ## clean up tmp config file
295         debug "rm $mycnf"
296         rm $mycnf
297         if [ -f "$tmpcnf" ]
298         then
299                 debug "mv $tmpcnf $mycnf"
300                 mv $tmpcnf $mycnf
301         fi
302 fi
303
304 return 0