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
20 getconf configfile /etc/mysql/debian.cnf
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.
27 if [ $vservers_are_available = yes ]; then
28 if [ -n "$vsname" ]; then
30 if ! vservers_exist "$vsname" ; then
31 fatal "The vserver given in vsname ($vsname) does not exist."
34 vservers_running $vsname || fatal "The vserver $vsname is not running."
36 info "Using vserver '$vsname'."
38 vroot="$VROOTDIR/$vsname"
40 info "No vserver name specified, actions will be performed on the host."
44 ## Prepare ignore part of the command
45 ## This only works for mysqldump at the moment
48 for i in $ignores $nodata; do
49 ignore="$ignore --ignore-table=$i"
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"
59 if [ $usevserver = yes ]
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
64 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
65 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
68 #######################################################################
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)
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.
87 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
89 if [ $usevserver = yes ]
91 vhome=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
94 home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
97 [ -d $home ] || fatal "Can't find root's home directory ($home)."
104 tmpcnf="$home/my.cnf.disable"
105 debug "mv $mycnf $tmpcnf"
112 # auto generated backupninja mysql conf
116 password="$dbpassword"
121 password="$dbpassword"
126 password="$dbpassword"
129 if [ $usevserver = yes ]
131 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
133 defaultsfile="--defaults-extra-file=$mycnf"
137 # if a user is not set, use $configfile, otherwise use $mycnf
138 if [ "$user" == "" ]; then
140 defaultsfile="--defaults-extra-file=$configfile"
143 if [ $usevserver = yes ]
145 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
148 fatal "User $user not found in /etc/passwd"
150 userhome="$vroot$vuserhome"
152 userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
155 fatal "User $user not found in /etc/passwd"
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"
165 #######################################################################
168 if [ "$hotcopy" == "yes" ]
170 info "Initializing hotcopy method"
171 if [ "$databases" == "all" ]
173 if [ $usevserver = yes ]
175 info "dbhost: $dbhost"
176 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
178 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
180 debug "su $user -c \"$execstr\""
183 output=`su $user -c "$execstr" 2>&1`
185 if [ "$code" == "0" ]
188 info "Successfully finished hotcopy of all mysql databases"
191 warning "Failed to hotcopy all mysql databases"
197 if [ $usevserver = yes ]
199 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
201 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
203 debug 'su $user -c \"$execstr\"'
206 output=`su $user -c "$execstr" 2>&1`
208 if [ "$code" == "0" ]
211 info "Successfully finished hotcopy of mysql database $db"
214 warning "Failed to hotcopy mysql database $db"
221 ##########################################################################
224 if [ "$sqldump" == "yes" ]
226 info "Initializing SQL dump method"
227 if [ "$databases" == "all" ]
229 if [ $usevserver = yes ]
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`
235 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
238 databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
241 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
248 DUMP_BASE="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names"
250 # Dumping structure and data
251 DUMP="$DUMP_BASE $ignore $db"
253 # If requested, dump only the table structure for this database
254 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
256 # Get the structure of the tables, without data
257 DUMP_STRUCT="$DUMP_BASE --no-data $db"
258 for qualified_table in $nodata
260 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
261 DUMP_STRUCT="$DUMP_STRUCT $table"
263 DUMP="( $DUMP; $DUMP_STRUCT )"
265 if [ $usevserver = yes ]
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!"
272 if [ "$compress" == "yes" ]; then
273 execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
275 execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
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!"
283 if [ "$compress" == "yes" ]; then
284 execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
286 execstr="$DUMP -r $dumpdir/${db}.sql"
289 debug "su $user -c \"$execstr\""
292 output=`su $user -c "$execstr" 2>&1`
294 if [ "$code" == "0" ]
297 info "Successfully finished dump of mysql database $db"
300 warning "Failed to dump mysql databases $db"
306 # clean up tmp config file
307 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
309 ## clean up tmp config file
314 debug "mv $tmpcnf $mycnf"