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'."
40 info "No vserver name specified, actions will be performed on the host."
43 [ -z "$vsname" ] || warning 'vservers support disabled in backupninja.conf, vsname configuration line will be ignored'
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 if [ $usevserver = yes ]; then
55 vroot="$VROOTDIR/$vsname"
56 vexec="$VSERVER $vsname exec"
63 interpolated=`interpolate "$backupdir" "$vsname"`
65 # create backup dirs, $vroot will be empty if no vsname was specified
66 # and we will instead proceed to operate on the host
67 [ -d $vroot$interpolated ] || mkdir -p $vroot$interpolated
68 [ -d $vroot$interpolated ] || fatal "Backup directory '$vroot$backupdir'"
70 hotdir="$interpolated/hotcopy"
71 dumpdir="$interpolated/sqldump"
73 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || mkdir -p $vroot$dumpdir
74 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || mkdir -p $vroot$hotdir
76 #######################################################################
80 # one of three authentication methods:
81 # 1. setting the user, so that /home/user/.my.cnf is used.
82 # 2. specifying the user and password in the handler config,
83 # which generates a temporary .my.cnf in /root/.my.cnf
84 # 3. specify the config file with --defaults-extra-file
85 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
89 # only if dbusername and dbpassword specified.
90 # we create a tmp file because we don't want to
91 # specify the password on the command line.
95 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
97 vhome=`$vexec 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 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
135 # if a user is not set, use $configfile, otherwise use $mycnf
136 if [ "$user" == "" ]; then
138 defaultsfile="--defaults-extra-file=$configfile"
141 if [ $usevserver = yes ]
143 vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'`
146 fatal "User $user not found in /etc/passwd"
148 userhome="$vroot$vuserhome"
151 debug "User home set to: $userhome"
152 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
153 defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
154 debug "using $defaultsfile"
157 #######################################################################
160 if [ "$hotcopy" == "yes" ]
162 info "Initializing hotcopy method"
163 if [ "$databases" == "all" ]
165 info "dbhost: $dbhost"
166 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
168 debug "su $user -c \"$execstr\""
171 output=`su $user -c "$execstr" 2>&1`
173 if [ "$code" == "0" ]
176 info "Successfully finished hotcopy of all mysql databases"
179 warning "Failed to hotcopy all mysql databases"
185 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $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 mysql database $db"
198 warning "Failed to hotcopy mysql database $db"
205 ##########################################################################
208 if [ "$sqldump" == "yes" ]
210 info "Initializing SQL dump method"
211 if [ "$databases" == "all" ]
213 debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'"
214 # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure.
215 databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'`
218 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
224 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
226 # Dumping structure and data
227 DUMP="$DUMP_BASE $ignore $db"
229 # If requested, dump only the table structure for this database
230 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
232 # Get the structure of the tables, without data
233 DUMP_STRUCT="$DUMP_BASE --no-data $db"
234 for qualified_table in $nodata
236 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
237 DUMP_STRUCT="$DUMP_STRUCT $table"
239 DUMP="( $DUMP; $DUMP_STRUCT )"
241 # Test to make sure mysqld is running, if it is not sqldump will not work
242 $vexec su $user -c "$MYSQLADMIN $defaultsfile ping"
243 if [ $? -ne 0 ]; then
244 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
246 if [ "$compress" == "yes" ]; then
247 execstr="$vexec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
249 execstr="$vexec $DUMP -r $vroot$dumpdir/${db}.sql"
252 debug "su $user -c \"$execstr\""
255 output=`su $user -c "$execstr" 2>&1`
257 if [ "$code" == "0" ]
260 info "Successfully finished dump of mysql database $db"
263 warning "Failed to dump mysql databases $db"
269 # clean up tmp config file
270 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
272 ## clean up tmp config file
277 debug "mv $tmpcnf $mycnf"