mysql: Pass -h $dbhost to mysqlhotcopy consistently.
[matthijs/upstream/backupninja.git] / handlers / mysql.in
index de4e4c3d2f920d8117c2e960e9c1f5e1619ccf22..39c1fb9c2729819b5d80f86873d910f0f808917c 100644 (file)
@@ -6,9 +6,11 @@
 getconf backupdir /var/backups/mysql
 getconf databases all
 getconf ignores
+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
 
@@ -30,10 +32,7 @@ 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
@@ -41,13 +40,15 @@ if [ $vservers_are_available = yes ]; then
    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
 ## This only works for mysqldump at the moment
 
 ignore=''
-for i in $ignores; do
+for i in $ignores $nodata; do
        ignore="$ignore --ignore-table=$i"
 done
        
@@ -160,7 +161,11 @@ else
        
        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"
+        if [ $usevserver = yes ]; then
+            defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf"
+        else
+            defaultsfile="--defaults-extra-file=$userhome/.my.cnf"
+        fi
        debug "using $defaultsfile"
 fi
 
@@ -177,7 +182,7 @@ then
                info "dbhost: $dbhost"
                execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
        else
-               execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
+               execstr="$MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
        fi
        debug "su $user -c \"$execstr\""
        if [ ! $test ]
@@ -198,9 +203,9 @@ then
        do
                if [ $usevserver = yes ]
                then
-                       execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
+                       execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
                else
-                       execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
+                       execstr="$MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir"
                fi
                debug 'su $user -c \"$execstr\"'
                if [ ! $test ]
@@ -237,16 +242,33 @@ then
                fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?"
            fi
        else
-               databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
+               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
-fi
+    fi
 
        for db in $databases
        do
+           DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions"
+
+           # Dumping structure and data
+           DUMP="$DUMP_BASE $ignore $db"
+
+           # If requested, dump only the table structure for this database
+           if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null
+           then
+              # Get the structure of the tables, without data
+              DUMP_STRUCT="$DUMP_BASE --no-data $db"
+              for qualified_table in $nodata
+              do
+                 table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" )
+                 DUMP_STRUCT="$DUMP_STRUCT $table"
+              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
@@ -255,9 +277,9 @@ fi
                       fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
                    fi
                    if [ "$compress" == "yes" ]; then
-                      execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $vroot$dumpdir/${db}.sql.gz"
+                      execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz"
                    else
-                      execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -r $vroot$dumpdir/${db}.sql"
+                      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
@@ -266,9 +288,9 @@ fi
                       fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!"
                    fi
                    if [ "$compress" == "yes" ]; then
-                      execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $dumpdir/${db}.sql.gz"
+                      execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz"
                    else
-                      execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -r $dumpdir/${db}.sql"
+                      execstr="$DUMP -r $dumpdir/${db}.sql"
                    fi
                fi
                debug "su $user -c \"$execstr\""