e1e89b0a29ccff24d5c8b025f4ece10d0462a940
[matthijs/upstream/backupninja.git] / handlers / mysql.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
3 #
4 # mysql handler script for backupninja
5 #
6
7 getconf backupdir /var/backups/mysql
8 getconf databases all
9 getconf ignores
10 getconf nodata
11 getconf dbhost localhost
12 getconf hotcopy no
13 getconf sqldump no
14 getconf sqldumpoptions "--lock-tables --complete-insert --add-drop-table --quick --quote-names"
15 getconf compress yes
16 getconf vsname
17
18 # authentication:
19 getconf user
20 getconf dbusername
21 getconf dbpassword
22 getconf configfile /etc/mysql/debian.cnf
23
24
25 # Decide if the handler should operate on a vserver or on the host.
26 # In the former case, check that $vsname exists and is running.
27 local usevserver=no
28 local vroot
29 if [ $vservers_are_available = yes ]; then
30    if [ -n "$vsname" ]; then
31       # does it exist ?
32       if ! vservers_exist "$vsname" ; then
33          fatal "The vserver given in vsname ($vsname) does not exist."
34       fi
35       # is it running ?
36       vservers_running $vsname || fatal "The vserver $vsname is not running."
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 $nodata; 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=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
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       DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
251
252       # Dumping structure and data
253       DUMP="$DUMP_BASE $ignore $db"
254
255       # If requested, dump only the table structure for this database
256       if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
257       then
258          # Get the structure of the tables, without data
259          DUMP_STRUCT="$DUMP_BASE --no-data $db"
260          for qualified_table in $nodata
261          do
262             table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
263             DUMP_STRUCT="$DUMP_STRUCT $table"
264          done
265          DUMP="( $DUMP; $DUMP_STRUCT )"
266       fi
267       if [ $usevserver = yes ]
268       then
269          # Test to make sure mysqld is running, if it is not sqldump will not work
270          $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping 2>&1 >/dev/null"
271          if [ $? -ne 0 ]; then
272             fatal "mysqld doesn't appear to be running!"
273          fi
274          if [ "$compress" == "yes" ]; then
275             execstr="$VSERVER $vsname exec $DUMP | $GZIP --rsyncable > $vroot$dumpdir/${db}.sql.gz"
276          else
277             execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
278          fi
279       else
280          # Test to make sure mysqld is running, if it is not sqldump will not work
281          su $user -c "$MYSQLADMIN $defaultsfile ping 2>&1 >/dev/null"
282          if [ $? -ne 0 ]; then
283             fatal "mysqld doesn't appear to be running!"
284          fi
285          if [ "$compress" == "yes" ]; then
286             execstr="$DUMP | $GZIP --rsyncable > $dumpdir/${db}.sql.gz"
287          else
288             execstr="$DUMP -r $dumpdir/${db}.sql"
289          fi
290       fi
291       debug "su $user -c \"$execstr\""
292       if [ ! $test ]
293       then
294          output=`su $user -c "$execstr" 2>&1`
295          code=$?
296          if [ "$code" == "0" ]
297          then
298             debug $output
299             info "Successfully finished dump of mysql database $db"
300          else
301             warning $output
302             warning "Failed to dump mysql databases $db"
303          fi
304       fi
305    done
306 fi
307
308 # clean up tmp config file
309 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
310 then
311    ## clean up tmp config file
312    debug "rm $mycnf"
313    rm $mycnf
314    if [ -f "$tmpcnf" ]
315    then
316       debug "mv $tmpcnf $mycnf"
317       mv $tmpcnf $mycnf
318    fi
319 fi
320
321 return 0