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 function make_backup() {
56 if [ -n "$vsname" ]; then
57 vroot="$VROOTDIR/$vsname"
58 vexec="$VSERVER $vsname exec"
64 interpolated=`interpolate "$backupdir" "$vsname"`
66 # create backup dirs, $vroot will be empty if no vsname was specified
67 # and we will instead proceed to operate on the host
68 [ -d $vroot$interpolated ] || mkdir -p $vroot$interpolated
69 [ -d $vroot$interpolated ] || fatal "Backup directory '$vroot$backupdir'"
71 hotdir="$interpolated/hotcopy"
72 dumpdir="$interpolated/sqldump"
74 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || mkdir -p $vroot$dumpdir
75 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || mkdir -p $vroot$hotdir
77 #######################################################################
81 # one of three authentication methods:
82 # 1. setting the user, so that /home/user/.my.cnf is used.
83 # 2. specifying the user and password in the handler config,
84 # which generates a temporary .my.cnf in /root/.my.cnf
85 # 3. specify the config file with --defaults-extra-file
86 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
90 # only if dbusername and dbpassword specified.
91 # we create a tmp file because we don't want to
92 # specify the password on the command line.
96 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
98 vhome=`$vexec getent passwd "root" | @AWK@ -F: '{print $6}'`
101 [ -d $home ] || fatal "Can't find root's home directory ($home)."
103 mycnf="$home/.my.cnf"
108 tmpcnf="$home/my.cnf.disable"
109 debug "mv $mycnf $tmpcnf"
116 # auto generated backupninja mysql conf
120 password="$dbpassword"
125 password="$dbpassword"
130 password="$dbpassword"
133 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
136 # if a user is not set, use $configfile, otherwise use $mycnf
137 if [ "$user" == "" ]; then
139 defaultsfile="--defaults-extra-file=$configfile"
142 if [ $usevserver = yes ]
144 vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'`
147 fatal "User $user not found in /etc/passwd"
149 userhome="$vroot$vuserhome"
152 debug "User home set to: $userhome"
153 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
154 defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
155 debug "using $defaultsfile"
158 #######################################################################
161 if [ "$hotcopy" == "yes" ]
163 info "Initializing hotcopy method"
164 if [ "$databases" == "all" ]
166 info "dbhost: $dbhost"
167 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
169 debug "su $user -c \"$execstr\""
172 output=`su $user -c "$execstr" 2>&1`
174 if [ "$code" == "0" ]
177 info "Successfully finished hotcopy of all mysql databases"
180 warning "Failed to hotcopy all mysql databases"
186 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
188 debug 'su $user -c \"$execstr\"'
191 output=`su $user -c "$execstr" 2>&1`
193 if [ "$code" == "0" ]
196 info "Successfully finished hotcopy of mysql database $db"
199 warning "Failed to hotcopy mysql database $db"
206 ##########################################################################
209 if [ "$sqldump" == "yes" ]
211 info "Initializing SQL dump method"
212 if [ "$databases" == "all" ]
214 debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'"
215 # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure.
216 databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'`
219 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
225 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
227 # Dumping structure and data
228 DUMP="$DUMP_BASE $ignore $db"
230 # If requested, dump only the table structure for this database
231 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
233 # Get the structure of the tables, without data
234 DUMP_STRUCT="$DUMP_BASE --no-data $db"
235 for qualified_table in $nodata
237 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
238 DUMP_STRUCT="$DUMP_STRUCT $table"
240 DUMP="( $DUMP; $DUMP_STRUCT )"
242 # Test to make sure mysqld is running, if it is not sqldump will not work
243 $vexec su $user -c "$MYSQLADMIN $defaultsfile ping"
244 if [ $? -ne 0 ]; then
245 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
247 if [ "$compress" == "yes" ]; then
248 execstr="$vexec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
250 execstr="$vexec $DUMP -r $vroot$dumpdir/${db}.sql"
253 debug "su $user -c \"$execstr\""
256 output=`su $user -c "$execstr" 2>&1`
258 if [ "$code" == "0" ]
261 info "Successfully finished dump of mysql database $db"
264 warning "Failed to dump mysql databases $db"
270 # clean up tmp config file
271 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
273 ## clean up tmp config file
278 debug "mv $tmpcnf $mycnf"
284 if [ $usevserver = yes ]; then
285 make_backup "$vsname"