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