1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # mysql handler script for backupninja
6 getconf backupdir /var/backups/mysql
10 getconf dbhost localhost
13 getconf sqldumpoptions "--lock-tables --complete-insert --add-drop-table --quick --quote-names"
21 getconf configfile /etc/mysql/debian.cnf
24 # Decide if the handler should operate on a vserver or on the host.
25 # In the former case, check that $vsname exists and is running.
28 if [ $vservers_are_available = yes ]; then
29 if [ -n "$vsname" ]; then
31 if ! vservers_exist "$vsname" ; then
32 fatal "The vserver given in vsname ($vsname) does not exist."
35 vservers_running $vsname || fatal "The vserver $vsname is not running."
37 info "Using vserver '$vsname'."
39 vroot="$VROOTDIR/$vsname"
41 info "No vserver name specified, actions will be performed on the host."
45 ## Prepare ignore part of the command
46 ## This only works for mysqldump at the moment
49 for i in $ignores $nodata; do
50 ignore="$ignore --ignore-table=$i"
53 # create backup dirs, $vroot will be empty if no vsname was specified
54 # and we will instead proceed to operate on the host
55 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
56 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
57 hotdir="$backupdir/hotcopy"
58 dumpdir="$backupdir/sqldump"
60 if [ $usevserver = yes ]
62 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
63 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
65 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
66 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
69 #######################################################################
73 # one of three authentication methods:
74 # 1. setting the user, so that /home/user/.my.cnf is used.
75 # 2. specifying the user and password in the handler config,
76 # which generates a temporary .my.cnf in /root/.my.cnf
77 # 3. specify the config file with --defaults-extra-file
78 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
82 # only if dbusername and dbpassword specified.
83 # we create a tmp file because we don't want to
84 # specify the password on the command line.
88 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
90 if [ $usevserver = yes ]
92 vhome=`$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"
105 tmpcnf="$home/my.cnf.disable"
106 debug "mv $mycnf $tmpcnf"
113 # auto generated backupninja mysql conf
117 password="$dbpassword"
122 password="$dbpassword"
127 password="$dbpassword"
130 if [ $usevserver = yes ]
132 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
134 defaultsfile="--defaults-extra-file=$mycnf"
138 # if a user is not set, use $configfile, otherwise use $mycnf
139 if [ "$user" == "" ]; then
141 defaultsfile="--defaults-extra-file=$configfile"
144 if [ $usevserver = yes ]
146 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
149 fatal "User $user not found in /etc/passwd"
151 userhome="$vroot$vuserhome"
153 userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
156 fatal "User $user not found in /etc/passwd"
160 debug "User home set to: $userhome"
161 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
162 defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
163 debug "using $defaultsfile"
166 #######################################################################
169 if [ "$hotcopy" == "yes" ]
171 info "Initializing hotcopy method"
172 if [ "$databases" == "all" ]
174 if [ $usevserver = yes ]
176 info "dbhost: $dbhost"
177 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
179 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
181 debug "su $user -c \"$execstr\""
184 output=`su $user -c "$execstr" 2>&1`
186 if [ "$code" == "0" ]
189 info "Successfully finished hotcopy of all mysql databases"
192 warning "Failed to hotcopy all mysql databases"
198 if [ $usevserver = yes ]
200 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
202 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
204 debug 'su $user -c \"$execstr\"'
207 output=`su $user -c "$execstr" 2>&1`
209 if [ "$code" == "0" ]
212 info "Successfully finished hotcopy of mysql database $db"
215 warning "Failed to hotcopy mysql database $db"
222 ##########################################################################
225 if [ "$sqldump" == "yes" ]
227 info "Initializing SQL dump method"
228 if [ "$databases" == "all" ]
230 if [ $usevserver = yes ]
232 debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
233 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
236 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
239 databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
242 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
249 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
251 # Dumping structure and data
252 DUMP="$DUMP_BASE $ignore $db"
254 # If requested, dump only the table structure for this database
255 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
257 # Get the structure of the tables, without data
258 DUMP_STRUCT="$DUMP_BASE --no-data $db"
259 for qualified_table in $nodata
261 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
262 DUMP_STRUCT="$DUMP_STRUCT $table"
264 DUMP="( $DUMP; $DUMP_STRUCT )"
266 if [ $usevserver = yes ]
268 # Test to make sure mysqld is running, if it is not sqldump will not work
269 $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping"
270 if [ $? -ne 0 ]; then
271 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
273 if [ "$compress" == "yes" ]; then
274 execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
276 execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
279 # Test to make sure mysqld is running, if it is not sqldump will not work
280 su $user -c "$MYSQLADMIN $defaultsfile ping"
281 if [ $? -ne 0 ]; then
282 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
284 if [ "$compress" == "yes" ]; then
285 execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
287 execstr="$DUMP -r $dumpdir/${db}.sql"
290 debug "su $user -c \"$execstr\""
293 output=`su $user -c "$execstr" 2>&1`
295 if [ "$code" == "0" ]
298 info "Successfully finished dump of mysql database $db"
301 warning "Failed to dump mysql databases $db"
307 # clean up tmp config file
308 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
310 ## clean up tmp config file
315 debug "mv $tmpcnf $mycnf"