Package: backupninja
Version: 0.9.4-6
Severity: wishlist
Tags: patch


A long time ago I submitted a patch (that was accepted) to support the 'ignores'
option for the mysql plugin. That option completely omits certain tables from
the dump.

Here is a new patch that provides another option for mysql: 'nodata'. Qualified tables listed in the nodata section will only have their structure dumped. This is very useful to backup databases that have cache data you don't want to backup, but you still need the strcture of table to exist on restore.

I took the opportunity to refactor the dump command, which was duplicated at
four places.

I had no opportunity to test the vserver mode.


Cheers,

Daniel
--- /usr/share/backupninja/mysql.orig	2006-12-15 07:12:34.000000000 +0100
+++ /usr/share/backupninja/mysql	2007-01-14 12:34:33.000000000 +0100
@@ -6,6 +6,7 @@
 getconf backupdir /var/backups/mysql
 getconf databases all
 getconf ignores
+getconf nodata
 getconf dbhost localhost
 getconf hotcopy no
 getconf sqldump no
@@ -47,7 +48,7 @@
 ## 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
 	
@@ -243,10 +244,28 @@
 		    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 --lock-tables --complete-insert --add-drop-table --quick --quote-names"
+
+		# Dumping structure and data
+		DUMP="$DUMP_BASE $ignore $db"
+
+		# Dumping structure only if needed 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 +274,9 @@
                       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 +285,9 @@
                       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\""

Reply via email to