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"
22 getconf configfile /etc/mysql/debian.cnf
25 # Decide if the handler should operate on a vserver or on the host.
26 # In the former case, check that $vsname exists and is running.
30 if [ -n "$vsname" ]; then
31 if [ -z "$vsnames" ]; then
32 warning "Using deprecated 'vsname' configuration variable"
35 warning "Ignoring (deprecated) vsname configuration variable, since vsnames is given"
39 if [ $vservers_are_available = yes -a -n "$vsnames" ]; then
40 if [ "$vsnames" = all ]; then
41 vsnames="$found_vservers"
43 if ! vservers_exist "$vsnames" ; then
44 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
47 info "Using vservers '$vsnames'"
50 [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
53 ## Prepare ignore part of the command
54 ## This only works for mysqldump at the moment
57 for i in $ignores $nodata; do
58 ignore="$ignore --ignore-table=$i"
61 function make_backup() {
63 if [ -z "$vsname" ]; then
64 info "Running on host"
68 if ! vservers_running "$vsname"; then
69 error "vserver $vsname is not running!"
72 info "Running on vserver $vsname"
73 vroot="$VROOTDIR/$vsname"
74 vexec="$VSERVER $vsname exec"
77 interpolated=`interpolate "$backupdir" "$vsname"`
79 # create backup dirs, $vroot will be empty if no vsname was specified
80 # and we will instead proceed to operate on the host
81 [ -d $vroot$interpolated ] || mkdir -p $vroot$interpolated
82 [ -d $vroot$interpolated ] || fatal "Backup directory '$vroot$backupdir'"
84 hotdir="$interpolated/hotcopy"
85 dumpdir="$interpolated/sqldump"
87 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || mkdir -p $vroot$dumpdir
88 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || mkdir -p $vroot$hotdir
90 #######################################################################
94 # one of three authentication methods:
95 # 1. setting the user, so that /home/user/.my.cnf is used.
96 # 2. specifying the user and password in the handler config,
97 # which generates a temporary .my.cnf in /root/.my.cnf
98 # 3. specify the config file with --defaults-extra-file
99 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
103 # only if dbusername and dbpassword specified.
104 # we create a tmp file because we don't want to
105 # specify the password on the command line.
109 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
111 vhome=`$vexec getent passwd "root" | @AWK@ -F: '{print $6}'`
114 [ -d $home ] || fatal "Can't find root's home directory ($home)."
116 mycnf="$home/.my.cnf"
121 tmpcnf="$home/my.cnf.disable"
122 debug "mv $mycnf $tmpcnf"
129 # auto generated backupninja mysql conf
133 password="$dbpassword"
138 password="$dbpassword"
143 password="$dbpassword"
146 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
149 # if a user is not set, use $configfile, otherwise use $mycnf
150 if [ "$user" == "" ]; then
152 defaultsfile="--defaults-extra-file=$configfile"
155 if [ $usevserver = yes ]
157 vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'`
160 fatal "User $user not found in /etc/passwd"
162 userhome="$vroot$vuserhome"
165 debug "User home set to: $userhome"
166 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
167 defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
168 debug "using $defaultsfile"
171 #######################################################################
174 if [ "$hotcopy" == "yes" ]
176 info "Initializing hotcopy method"
177 if [ "$databases" == "all" ]
179 info "dbhost: $dbhost"
180 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
182 debug "su $user -c \"$execstr\""
185 output=`su $user -c "$execstr" 2>&1`
187 if [ "$code" == "0" ]
190 info "Successfully finished hotcopy of all mysql databases"
193 warning "Failed to hotcopy all mysql databases"
199 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
201 debug 'su $user -c \"$execstr\"'
204 output=`su $user -c "$execstr" 2>&1`
206 if [ "$code" == "0" ]
209 info "Successfully finished hotcopy of mysql database $db"
212 warning "Failed to hotcopy mysql database $db"
219 ##########################################################################
222 if [ "$sqldump" == "yes" ]
224 info "Initializing SQL dump method"
225 if [ "$databases" == "all" ]
227 debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'"
228 # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure.
229 databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'`
232 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
238 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
240 # Dumping structure and data
241 DUMP="$DUMP_BASE $ignore $db"
243 # If requested, dump only the table structure for this database
244 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
246 # Get the structure of the tables, without data
247 DUMP_STRUCT="$DUMP_BASE --no-data $db"
248 for qualified_table in $nodata
250 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
251 DUMP_STRUCT="$DUMP_STRUCT $table"
253 DUMP="( $DUMP; $DUMP_STRUCT )"
255 # Test to make sure mysqld is running, if it is not sqldump will not work
256 $vexec su $user -c "$MYSQLADMIN $defaultsfile ping"
257 if [ $? -ne 0 ]; then
258 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
260 if [ "$compress" == "yes" ]; then
261 execstr="$vexec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
263 execstr="$vexec $DUMP -r $vroot$dumpdir/${db}.sql"
266 debug "su $user -c \"$execstr\""
269 output=`su $user -c "$execstr" 2>&1`
271 if [ "$code" == "0" ]
274 info "Successfully finished dump of mysql database $db"
277 warning "Failed to dump mysql databases $db"
283 # clean up tmp config file
284 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
286 ## clean up tmp config file
291 debug "mv $tmpcnf $mycnf"
297 if [ $usevserver = yes ]; then
298 for vserver in $vsnames; do
299 make_backup "$vserver"