1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 # mysql handler script for backupninja
7 getconf backupdir /var/backups/mysql
11 getconf dbhost localhost
14 getconf sqldumpoptions "--lock-tables --complete-insert --add-drop-table --quick --quote-names"
22 getconf configfile /etc/mysql/debian.cnf
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.
29 if [ $vservers_are_available = yes ]; then
30 if [ -n "$vsname" ]; then
32 if ! vservers_exist "$vsname" ; then
33 fatal "The vserver given in vsname ($vsname) does not exist."
36 vservers_running $vsname || fatal "The vserver $vsname is not running."
38 info "Using vserver '$vsname'."
40 vroot="$VROOTDIR/$vsname"
42 info "No vserver name specified, actions will be performed on the host."
46 ## Prepare ignore part of the command
47 ## This only works for mysqldump at the moment
50 for i in $ignores $nodata; do
51 ignore="$ignore --ignore-table=$i"
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"
61 if [ $usevserver = yes ]
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
66 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
67 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
70 #######################################################################
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)
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.
89 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
91 if [ $usevserver = yes ]
93 home=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
95 home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
98 [ -d $home ] || fatal "Can't find root's home directory ($home)."
100 mycnf="$home/.my.cnf"
102 if [ $usevserver = yes ]
104 workcnf="$vroot$mycnf"
112 tmpcnf="$workcnf.disable"
113 debug "mv $workcnf $tmpcnf"
120 # auto generated backupninja mysql conf
124 password="$dbpassword"
129 password="$dbpassword"
134 password="$dbpassword"
139 password="$dbpassword"
142 defaultsfile="--defaults-extra-file=$mycnf"
145 # if a user is not set, use $configfile, otherwise use $mycnf
146 if [ "$user" == "" ]; then
148 defaultsfile="--defaults-extra-file=$configfile"
151 if [ $usevserver = yes ]
153 userhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
156 fatal "User $user not found in /etc/passwd"
158 debug "User home set to: $vroot$userhome"
159 [ -f $vroot$userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
161 userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
164 fatal "User $user not found in /etc/passwd"
166 debug "User home set to: $userhome"
167 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
170 defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
171 debug "using $defaultsfile"
174 #######################################################################
177 if [ "$hotcopy" == "yes" ]
179 info "Initializing hotcopy method"
180 if [ "$databases" == "all" ]
182 if [ $usevserver = yes ]
184 info "dbhost: $dbhost"
185 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
189 debug "su $user -c \"$execstr\""
192 output=`su $user -c "$execstr" 2>&1`
194 if [ "$code" == "0" ]
197 info "Successfully finished hotcopy of all mysql databases"
200 warning "Failed to hotcopy all mysql databases"
206 if [ $usevserver = yes ]
208 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
210 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
212 debug 'su $user -c \"$execstr\"'
215 output=`su $user -c "$execstr" 2>&1`
217 if [ "$code" == "0" ]
220 info "Successfully finished hotcopy of mysql database $db"
223 warning "Failed to hotcopy mysql database $db"
230 ##########################################################################
233 if [ "$sqldump" == "yes" ]
235 info "Initializing SQL dump method"
236 if [ "$databases" == "all" ]
238 if [ $usevserver = yes ]
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`
244 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
247 databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
250 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
257 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
259 # Dumping structure and data
260 DUMP="$DUMP_BASE $ignore $db"
262 # If requested, dump only the table structure for this database
263 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
265 # Get the structure of the tables, without data
266 DUMP_STRUCT="$DUMP_BASE --no-data $db"
267 for qualified_table in $nodata
269 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
270 DUMP_STRUCT="$DUMP_STRUCT $table"
272 DUMP="( $DUMP; $DUMP_STRUCT )"
274 if [ $usevserver = yes ]
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!"
281 if [ "$compress" == "yes" ]; then
282 execstr="$VSERVER $vsname exec $DUMP | $GZIP --rsyncable > '$vroot$dumpdir/${db}.sql.gz'"
284 execstr="$VSERVER $vsname exec $DUMP -r '$vroot$dumpdir/${db}.sql'"
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!"
292 if [ "$compress" == "yes" ]; then
293 execstr="$DUMP | $GZIP --rsyncable > '$dumpdir/${db}.sql.gz'"
295 execstr="$DUMP -r '$dumpdir/${db}.sql'"
298 debug "su $user -c \"$execstr\""
301 output=`su $user -c "$execstr" 2>&1`
303 if [ "$code" == "0" ]
306 info "Successfully finished dump of mysql database $db"
309 warning "Failed to dump mysql databases $db"
315 # clean up tmp config file
316 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
318 ## clean up tmp config file
323 debug "mv $tmpcnf $workcnf"