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 defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
165 debug "using $defaultsfile"
168 #######################################################################
171 if [ "$hotcopy" == "yes" ]
173 info "Initializing hotcopy method"
174 if [ "$databases" == "all" ]
176 if [ $usevserver = yes ]
178 info "dbhost: $dbhost"
179 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
181 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
183 debug "su $user -c \"$execstr\""
186 output=`su $user -c "$execstr" 2>&1`
188 if [ "$code" == "0" ]
191 info "Successfully finished hotcopy of all mysql databases"
194 warning "Failed to hotcopy all mysql databases"
200 if [ $usevserver = yes ]
202 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
204 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
206 debug 'su $user -c \"$execstr\"'
209 output=`su $user -c "$execstr" 2>&1`
211 if [ "$code" == "0" ]
214 info "Successfully finished hotcopy of mysql database $db"
217 warning "Failed to hotcopy mysql database $db"
224 ##########################################################################
227 if [ "$sqldump" == "yes" ]
229 info "Initializing SQL dump method"
230 if [ "$databases" == "all" ]
232 if [ $usevserver = yes ]
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`
238 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
241 databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
244 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
251 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
253 # Dumping structure and data
254 DUMP="$DUMP_BASE $ignore $db"
256 # If requested, dump only the table structure for this database
257 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
259 # Get the structure of the tables, without data
260 DUMP_STRUCT="$DUMP_BASE --no-data $db"
261 for qualified_table in $nodata
263 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
264 DUMP_STRUCT="$DUMP_STRUCT $table"
266 DUMP="( $DUMP; $DUMP_STRUCT )"
268 if [ $usevserver = yes ]
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!"
275 if [ "$compress" == "yes" ]; then
276 execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
278 execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
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!"
286 if [ "$compress" == "yes" ]; then
287 execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
289 execstr="$DUMP -r $dumpdir/${db}.sql"
292 debug "su $user -c \"$execstr\""
295 output=`su $user -c "$execstr" 2>&1`
297 if [ "$code" == "0" ]
300 info "Successfully finished dump of mysql database $db"
303 warning "Failed to dump mysql databases $db"
309 # clean up tmp config file
310 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
312 ## clean up tmp config file
317 debug "mv $tmpcnf $mycnf"