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