mysql: Interpolate the backupdir config variable.
[matthijs/upstream/backupninja.git] / handlers / mysql.in
index defb72aa8aeb98713d807024a5a488bcbca43cdd..a6cd023ae7b422675c342775d9e3c1f3c4557464 100644 (file)
@@ -10,6 +10,7 @@ getconf nodata
 getconf dbhost localhost
 getconf hotcopy no
 getconf sqldump no
+getconf sqldumpoptions "--lock-tables --complete-insert --add-drop-table --quick --quote-names"
 getconf compress yes
 getconf vsname
 
@@ -31,17 +32,15 @@ if [ $vservers_are_available = yes ]; then
          fatal "The vserver given in vsname ($vsname) does not exist."
       fi
       # is it running ?
-      $VSERVERINFO -q $vsname RUNNING
-      if [ $? -ne 0 ]; then
-         fatal "The vserver $vsname is not running."
-      fi
+      vservers_running $vsname || fatal "The vserver $vsname is not running."
       # everything ok
       info "Using vserver '$vsname'."
       usevserver=yes
-      vroot="$VROOTDIR/$vsname"
    else
       info "No vserver name specified, actions will be performed on the host."
    fi
+else
+   [ -z "$vsname" ] || warning 'vservers support disabled in backupninja.conf, vsname configuration line will be ignored'
 fi
 
 ## Prepare ignore part of the command
@@ -51,23 +50,29 @@ ignore=''
 for i in $ignores $nodata; do
        ignore="$ignore --ignore-table=$i"
 done
-       
-# create backup dirs, $vroot will be empty if no vsname was specified
-# and we will instead proceed to operate on the host
-[ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
-[ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
-hotdir="$backupdir/hotcopy"
-dumpdir="$backupdir/sqldump"
 
-if [ $usevserver = yes ]
-then
-       [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
-       [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
+if [ $usevserver = yes ]; then
+   vroot="$VROOTDIR/$vsname"
+   vexec="$VSERVER $vsname exec"
 else
-       [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
-       [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
+   vroot=""
+   vexec=""
+   vsname=""
 fi
 
+interpolated=`interpolate "$backupdir" "$vsname"`
+
+# create backup dirs, $vroot will be empty if no vsname was specified
+# and we will instead proceed to operate on the host
+[ -d $vroot$interpolated ] || mkdir -p $vroot$interpolated
+[ -d $vroot$interpolated ] || fatal "Backup directory '$vroot$backupdir'"
+
+hotdir="$interpolated/hotcopy"
+dumpdir="$interpolated/sqldump"
+
+[ "$sqldump" == "no" -o -d $vroot$dumpdir ] || mkdir -p $vroot$dumpdir
+[ "$hotcopy" == "no" -o -d $vroot$hotdir ] || mkdir -p $vroot$hotdir
+
 #######################################################################
 ## AUTHENTICATION
 
@@ -89,13 +94,8 @@ defaultsfile=""
 
 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
 then
-    if [ $usevserver = yes ]
-    then
-       vhome=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'`
-       home="$vroot$vhome"
-    else
-       home=`getent passwd "root" | @AWK@ -F: '{print $6}'`
-    fi
+    vhome=`$vexec getent passwd "root" | @AWK@ -F: '{print $6}'`
+    home=$vroot$vhome
 
     [ -d $home ] || fatal "Can't find root's home directory ($home)."
     
@@ -129,12 +129,7 @@ user=$dbusername
 password="$dbpassword"
 EOF
        umask $oldmask
-       if [ $usevserver = yes ] 
-       then
-           defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
-       else
-           defaultsfile="--defaults-extra-file=$mycnf"
-       fi
+        defaultsfile="--defaults-extra-file=$vhome/.my.cnf"
 fi
 
 # if a user is not set, use $configfile, otherwise use $mycnf
@@ -145,23 +140,17 @@ else
        userset=true;
        if [ $usevserver = yes ]
        then
-           vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'`
+           vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'`
            if [ $? -eq 2 ]
            then
                fatal "User $user not found in /etc/passwd"
            fi
            userhome="$vroot$vuserhome"
-       else
-           userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'`
-           if [ $? -eq 2 ]
-           then
-               fatal "User $user not found in /etc/passwd"
-           fi
                fi
        
        debug "User home set to: $userhome"
        [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
-       defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
+        defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
        debug "using $defaultsfile"
 fi
 
@@ -173,13 +162,9 @@ then
     info "Initializing hotcopy method"
     if [ "$databases" == "all" ]
     then
-       if [ $usevserver = yes ]
-       then
-               info "dbhost: $dbhost"
-               execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
-       else
-               execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
-       fi
+        info "dbhost: $dbhost"
+        execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
+
        debug "su $user -c \"$execstr\""
        if [ ! $test ]
        then
@@ -197,12 +182,8 @@ then
     else       
        for db in $databases
        do
-               if [ $usevserver = yes ]
-               then
-                       execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
-               else
-                       execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
-               fi
+                execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
+
                debug 'su $user -c \"$execstr\"'
                if [ ! $test ]
                then
@@ -229,26 +210,18 @@ then
     info "Initializing SQL dump method"
     if [ "$databases" == "all" ]
     then
-       if [ $usevserver = yes ]
-       then
-           debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
-           databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
-           if [ $? -ne 0 ]
-           then
-               fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
-           fi
-       else
-               databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d')
-               if [ $? -ne 0 ]
-               then
-                   fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
-               fi
-       fi
+        debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'"
+        # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure.
+        databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'`
+        if [ $? -ne 0 ]
+        then
+            fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
+        fi
     fi
 
        for db in $databases
        do
-           DUMP_BASE="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names"
+           DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
 
            # Dumping structure and data
            DUMP="$DUMP_BASE $ignore $db"
@@ -265,30 +238,17 @@ then
               done
               DUMP="( $DUMP; $DUMP_STRUCT )"
            fi
-               if [ $usevserver = yes ]
-               then
-                   # Test to make sure mysqld is running, if it is not sqldump will not work
-                   $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping"
-                   if [ $? -ne 0 ]; then
-                      fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
-                   fi
-                   if [ "$compress" == "yes" ]; then
-                      execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
-                   else
-                      execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql"
-                   fi
-               else
-                   # Test to make sure mysqld is running, if it is not sqldump will not work
-                   su $user -c "$MYSQLADMIN $defaultsfile ping"
-                   if [ $? -ne 0 ]; then
-                      fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
-                   fi
-                   if [ "$compress" == "yes" ]; then
-                      execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
-                   else
-                      execstr="$DUMP -r $dumpdir/${db}.sql"
-                   fi
-               fi
+                # Test to make sure mysqld is running, if it is not sqldump will not work
+                $vexec su $user -c "$MYSQLADMIN $defaultsfile ping"
+                if [ $? -ne 0 ]; then
+                   fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
+                fi
+                if [ "$compress" == "yes" ]; then
+                   execstr="$vexec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
+                else
+                   execstr="$vexec $DUMP -r $vroot$dumpdir/${db}.sql"
+                fi
+
                debug "su $user -c \"$execstr\""
                if [ ! $test ]
                then