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."
44 [ -z "$vsname" ] || warning 'vservers support disabled in backupninja.conf, vsname configuration line will be ignored'
47 ## Prepare ignore part of the command
48 ## This only works for mysqldump at the moment
51 for i in $ignores $nodata; do
52 ignore="$ignore --ignore-table=$i"
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"
62 if [ $usevserver = yes ]
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
67 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
68 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
71 #######################################################################
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)
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.
90 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
92 if [ $usevserver = yes ]
94 vhome=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
97 home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
100 [ -d $home ] || fatal "Can't find root's home directory ($home)."
102 mycnf="$home/.my.cnf"
107 tmpcnf="$home/my.cnf.disable"
108 debug "mv $mycnf $tmpcnf"
115 # auto generated backupninja mysql conf
119 password="$dbpassword"
124 password="$dbpassword"
129 password="$dbpassword"
132 if [ $usevserver = yes ]
134 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
136 defaultsfile="--defaults-extra-file=$mycnf"
140 # if a user is not set, use $configfile, otherwise use $mycnf
141 if [ "$user" == "" ]; then
143 defaultsfile="--defaults-extra-file=$configfile"
146 if [ $usevserver = yes ]
148 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
151 fatal "User $user not found in /etc/passwd"
153 userhome="$vroot$vuserhome"
155 userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
158 fatal "User $user not found in /etc/passwd"
162 debug "User home set to: $userhome"
163 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
164 if [ $usevserver = yes ]; then
165 defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
167 defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
169 debug "using $defaultsfile"
172 #######################################################################
175 if [ "$hotcopy" == "yes" ]
177 info "Initializing hotcopy method"
178 if [ "$databases" == "all" ]
180 if [ $usevserver = yes ]
182 info "dbhost: $dbhost"
183 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
185 execstr="$MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
187 debug "su $user -c \"$execstr\""
190 output=`su $user -c "$execstr" 2>&1`
192 if [ "$code" == "0" ]
195 info "Successfully finished hotcopy of all mysql databases"
198 warning "Failed to hotcopy all mysql databases"
204 if [ $usevserver = yes ]
206 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
208 execstr="$MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
210 debug 'su $user -c \"$execstr\"'
213 output=`su $user -c "$execstr" 2>&1`
215 if [ "$code" == "0" ]
218 info "Successfully finished hotcopy of mysql database $db"
221 warning "Failed to hotcopy mysql database $db"
228 ##########################################################################
231 if [ "$sqldump" == "yes" ]
233 info "Initializing SQL dump method"
234 if [ "$databases" == "all" ]
236 if [ $usevserver = yes ]
238 debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
239 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
242 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
245 databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
248 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
255 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
257 # Dumping structure and data
258 DUMP="$DUMP_BASE $ignore $db"
260 # If requested, dump only the table structure for this database
261 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
263 # Get the structure of the tables, without data
264 DUMP_STRUCT="$DUMP_BASE --no-data $db"
265 for qualified_table in $nodata
267 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
268 DUMP_STRUCT="$DUMP_STRUCT $table"
270 DUMP="( $DUMP; $DUMP_STRUCT )"
272 if [ $usevserver = yes ]
274 # Test to make sure mysqld is running, if it is not sqldump will not work
275 $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping"
276 if [ $? -ne 0 ]; then
277 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
279 if [ "$compress" == "yes" ]; then
280 execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
282 execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
285 # Test to make sure mysqld is running, if it is not sqldump will not work
286 su $user -c "$MYSQLADMIN $defaultsfile ping"
287 if [ $? -ne 0 ]; then
288 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
290 if [ "$compress" == "yes" ]; then
291 execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
293 execstr="$DUMP -r $dumpdir/${db}.sql"
296 debug "su $user -c \"$execstr\""
299 output=`su $user -c "$execstr" 2>&1`
301 if [ "$code" == "0" ]
304 info "Successfully finished dump of mysql database $db"
307 warning "Failed to dump mysql databases $db"
313 # clean up tmp config file
314 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
316 ## clean up tmp config file
321 debug "mv $tmpcnf $mycnf"