3b7423f21fa32ae72ea8f07e8a27a24ac0b54c4f
[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         home=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
94     else
95         home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
96     fi
97
98     [ -d $home ] || fatal "Can't find root's home directory ($home)."
99     
100     mycnf="$home/.my.cnf"
101
102     if [ $usevserver = yes ]
103     then
104       workcnf="$vroot$mycnf"
105     else
106       workcnf="$mycnf"
107     fi
108
109     if [ -f $workcnf ]
110     then
111       # rename temporarily
112       tmpcnf="$workcnf.disable"
113       debug "mv $workcnf $tmpcnf"
114       mv $workcnf $tmpcnf
115     fi
116     
117     oldmask=`umask`
118     umask 077
119     cat > $workcnf <<EOF
120 # auto generated backupninja mysql conf
121 [mysql]
122 host=$dbhost
123 user=$dbusername
124 password="$dbpassword"
125
126 [mysqldump]
127 host=$dbhost
128 user=$dbusername
129 password="$dbpassword"
130
131 [mysqlhotcopy]
132 host=$dbhost
133 user=$dbusername
134 password="$dbpassword"
135
136 [mysqladmin]
137 host=$dbhost
138 user=$dbusername
139 password="$dbpassword"
140 EOF
141         umask $oldmask
142         defaultsfile="--defaults-extra-file=$mycnf"
143 fi
144
145 # if a user is not set, use $configfile, otherwise use $mycnf
146 if [ "$user" == "" ]; then
147    user=root;
148    defaultsfile="--defaults-extra-file=$configfile"
149 else
150         userset=true;
151         if [ $usevserver = yes ]
152         then
153             userhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
154             if [ $? -eq 2 ]
155             then
156                 fatal "User $user not found in /etc/passwd"
157             fi
158             debug "User home set to: $vroot$userhome"
159             [ -f $vroot$userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
160         else
161             userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
162             if [ $? -eq 2 ]
163             then
164                 fatal "User $user not found in /etc/passwd"
165             fi
166             debug "User home set to: $userhome"
167             [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
168         fi
169         
170         defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
171         debug "using $defaultsfile"
172 fi
173
174 #######################################################################
175 ## HOT COPY
176
177 if [ "$hotcopy" == "yes" ]
178 then
179    info "Initializing hotcopy method"
180    if [ "$databases" == "all" ]
181    then
182       if [ $usevserver = yes ]
183       then
184          info "dbhost: $dbhost"
185          execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
186       else
187          execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
188       fi
189       debug "su $user -c \"$execstr\""
190       if [ ! $test ]
191       then
192          output=`su $user -c "$execstr" 2>&1`
193          code=$?
194          if [ "$code" == "0" ]
195          then
196             debug $output
197             info "Successfully finished hotcopy of all mysql databases"
198          else
199             warning $output
200             warning "Failed to hotcopy all mysql databases"
201          fi
202       fi
203    else
204       for db in $databases
205       do
206          if [ $usevserver = yes ]
207          then
208             execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
209          else
210             execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
211          fi
212          debug 'su $user -c \"$execstr\"'
213          if [ ! $test ]
214          then
215             output=`su $user -c "$execstr" 2>&1`
216             code=$?
217             if [ "$code" == "0" ]
218             then
219                debug $output
220                info "Successfully finished hotcopy of mysql database $db"
221             else
222                warning $output
223                warning "Failed to hotcopy mysql database $db"
224             fi
225          fi
226       done
227    fi
228 fi
229
230 ##########################################################################
231 ## SQL DUMP
232
233 if [ "$sqldump" == "yes" ]
234 then
235    info "Initializing SQL dump method"
236    if [ "$databases" == "all" ]
237    then
238       if [ $usevserver = yes ]
239       then
240          debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
241          databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
242          if [ $? -ne 0 ]
243          then
244             fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
245          fi
246       else
247          databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
248          if [ $? -ne 0 ]
249          then
250             fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
251          fi
252       fi
253    fi
254
255    for db in $databases
256    do
257       DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
258
259       # Dumping structure and data
260       DUMP="$DUMP_BASE $ignore $db"
261
262       # If requested, dump only the table structure for this database
263       if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
264       then
265          # Get the structure of the tables, without data
266          DUMP_STRUCT="$DUMP_BASE --no-data $db"
267          for qualified_table in $nodata
268          do
269             table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
270             DUMP_STRUCT="$DUMP_STRUCT $table"
271          done
272          DUMP="( $DUMP; $DUMP_STRUCT )"
273       fi
274       if [ $usevserver = yes ]
275       then
276          # Test to make sure mysqld is running, if it is not sqldump will not work
277          $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping 2>&1 >/dev/null"
278          if [ $? -ne 0 ]; then
279             fatal "mysqld doesn't appear to be running!"
280          fi
281          if [ "$compress" == "yes" ]; then
282             execstr="$VSERVER $vsname exec $DUMP | $GZIP --rsyncable > '$vroot$dumpdir/${db}.sql.gz'"
283          else
284             execstr="$VSERVER $vsname exec $DUMP -r '$vroot$dumpdir/${db}.sql'"
285          fi
286       else
287          # Test to make sure mysqld is running, if it is not sqldump will not work
288          su $user -c "$MYSQLADMIN $defaultsfile ping 2>&1 >/dev/null"
289          if [ $? -ne 0 ]; then
290             fatal "mysqld doesn't appear to be running!"
291          fi
292          if [ "$compress" == "yes" ]; then
293             execstr="$DUMP | $GZIP --rsyncable > '$dumpdir/${db}.sql.gz'"
294          else
295             execstr="$DUMP -r '$dumpdir/${db}.sql'"
296          fi
297       fi
298       debug "su $user -c \"$execstr\""
299       if [ ! $test ]
300       then
301          output=`su $user -c "$execstr" 2>&1`
302          code=$?
303          if [ "$code" == "0" ]
304          then
305             debug $output
306             info "Successfully finished dump of mysql database $db"
307          else
308             warning $output
309             warning "Failed to dump mysql databases $db"
310          fi
311       fi
312    done
313 fi
314
315 # clean up tmp config file
316 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
317 then
318         ## clean up tmp config file
319         debug "rm $workcnf"
320         rm $workcnf
321         if [ -f "$tmpcnf" ]
322         then
323                 debug "mv $tmpcnf $workcnf"
324                 mv $tmpcnf $workcnf
325         fi
326 fi
327
328 return 0