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"
34 info "Prepending '%v' to backupdir to emulate legacy behaviour"
35 backupdir="%v/$backupdir"
37 warning "Ignoring (deprecated) vsname configuration variable, since vsnames is given"
41 if [ $vservers_are_available = yes -a -n "$vsnames" ]; then
42 if [ "$vsnames" = all ]; then
43 vsnames="$found_vservers"
45 if ! vservers_exist "$vsnames" ; then
46 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
49 info "Using vservers '$vsnames'"
52 [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
55 ## Prepare ignore part of the command
56 ## This only works for mysqldump at the moment
59 for i in $ignores $nodata; do
60 ignore="$ignore --ignore-table=$i"
63 function make_backup() {
65 if [ -z "$vsname" ]; then
66 info "Running on host"
70 if ! vservers_running "$vsname"; then
71 error "vserver $vsname is not running!"
74 info "Running on vserver $vsname"
75 vroot="$VROOTDIR/$vsname"
76 vexec="$VSERVER $vsname exec"
79 interpolated=`interpolate "$backupdir" "$vsname"`
82 [ -d $interpolated ] || mkdir -p $interpolated
83 [ -d $interpolated ] || fatal "Failed to create backup directory '$interpolated'"
85 hotdir="$interpolated/hotcopy"
86 dumpdir="$interpolated/sqldump"
88 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
89 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
91 #######################################################################
95 # one of three authentication methods:
96 # 1. setting the user, so that /home/user/.my.cnf is used.
97 # 2. specifying the user and password in the handler config,
98 # which generates a temporary .my.cnf in /root/.my.cnf
99 # 3. specify the config file with --defaults-extra-file
100 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
104 # only if dbusername and dbpassword specified.
105 # we create a tmp file because we don't want to
106 # specify the password on the command line.
110 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
112 vhome=`$vexec getent passwd "root" | @AWK@ -F: '{print $6}'`
115 [ -d $home ] || fatal "Can't find root's home directory ($home)."
117 mycnf="$home/.my.cnf"
122 tmpcnf="$home/my.cnf.disable"
123 debug "mv $mycnf $tmpcnf"
130 # auto generated backupninja mysql conf
134 password="$dbpassword"
139 password="$dbpassword"
144 password="$dbpassword"
147 defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
150 # if a user is not set, use $configfile, otherwise use $mycnf
151 if [ "$user" == "" ]; then
153 defaultsfile="--defaults-extra-file=$configfile"
156 if [ $usevserver = yes ]
158 vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'`
161 fatal "User $user not found in /etc/passwd"
163 userhome="$vroot$vuserhome"
166 debug "User home set to: $userhome"
167 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
168 defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
169 debug "using $defaultsfile"
172 #######################################################################
175 if [ "$hotcopy" == "yes" ]
177 if starts_with "$hotdir" "$vroot"; then
178 info "Initializing hotcopy method"
179 vhotdir=`strip_prefix "$hotdir" "$vroot"`
180 if [ "$databases" == "all" ]
182 info "dbhost: $dbhost"
183 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $vhotdir"
185 debug "su $user -c \"$execstr\""
188 output=`su $user -c "$execstr" 2>&1`
190 if [ "$code" == "0" ]
193 info "Successfully finished hotcopy of all mysql databases"
196 warning "Failed to hotcopy all mysql databases"
202 execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $vhotdir"
204 debug 'su $user -c \"$execstr\"'
207 output=`su $user -c "$execstr" 2>&1`
209 if [ "$code" == "0" ]
212 info "Successfully finished hotcopy of mysql database $db"
215 warning "Failed to hotcopy mysql database $db"
221 error "hotcopy method can only be used when the backupdir ($hotdir) is within the vserver ($vroot)"
225 ##########################################################################
228 if [ "$sqldump" == "yes" ]
230 info "Initializing SQL dump method"
231 if [ "$databases" == "all" ]
233 debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'"
234 # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure.
235 databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'`
238 fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
244 DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
246 # Dumping structure and data
247 DUMP="$DUMP_BASE $ignore $db"
249 # If requested, dump only the table structure for this database
250 if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
252 # Get the structure of the tables, without data
253 DUMP_STRUCT="$DUMP_BASE --no-data $db"
254 for qualified_table in $nodata
256 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
257 DUMP_STRUCT="$DUMP_STRUCT $table"
259 DUMP="( $DUMP; $DUMP_STRUCT )"
261 # Test to make sure mysqld is running, if it is not sqldump will not work
262 $vexec su $user -c "$MYSQLADMIN $defaultsfile ping"
263 if [ $? -ne 0 ]; then
264 fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
266 if [ "$compress" == "yes" ]; then
267 execstr="$vexec $DUMP | $GZIP > $dumpdir/${db}.sql.gz"
269 execstr="$vexec $DUMP -r $dumpdir/${db}.sql"
272 debug "su $user -c \"$execstr\""
275 output=`su $user -c "$execstr" 2>&1`
277 if [ "$code" == "0" ]
280 info "Successfully finished dump of mysql database $db"
283 warning "Failed to dump mysql databases $db"
289 # clean up tmp config file
290 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
292 ## clean up tmp config file
297 debug "mv $tmpcnf $mycnf"
303 if [ $usevserver = yes ]; then
304 for vserver in $vsnames; do
305 make_backup "$vserver"