--- automysqlbackup.sh.2.5	2006-02-15 09:19:13.000000000 -0500
+++ automysqlbackup.sh.2.6.2.my	2008-06-23 09:37:26.000000000 -0400
@@ -74,7 +74,10 @@
 DOWEEKLY=6
 
 # Choose Compression type. (gzip or bzip2)
-COMP=gzip
+COMP=bzip2
+
+# Use pipe compress
+PIPECOMP=yes
 
 # Compress communications between backup server and MySQL server?
 COMMCOMP=no
@@ -88,6 +91,9 @@
 #  For connections to localhost. Sometimes the Unix socket file must be specified.
 SOCKET=
 
+# Backup databases per table work if SEPDIR set to `yes'
+PERTABLE=yes
+
 # Command to run before backups (uncomment to use)
 #PREBACKUP="/etc/mysql-backup-pre"
 
@@ -342,7 +348,7 @@
 DOM=`date +%d`							# Date of the Month e.g. 27
 M=`date +%B`							# Month e.g January
 W=`date +%V`							# Week Number e.g 37
-VER=2.5									# Version Number
+VER=2.6.2									# Version Number
 LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log		# Logfile Name
 LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log		# Logfile Name
 BACKUPFILES=""
@@ -408,6 +414,46 @@
 mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 > $2
 return 0
 }
+dbdump_table () {
+mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 > $3
+return 0
+}
+# Database dump function with pipe compression plus latest copy
+dbdump_comp () {
+if [ "$COMP" = "gzip" ]; then
+    echo
+    echo Backup Information for "$1.gz"
+    SUFFIX=".gz"
+	mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 | gzip > $2$SUFFIX
+    gzip -l "$1.gz"
+elif [ "$COMP" = "bzip2" ]; then
+    echo Compression information for "$1.bz2"
+    SUFFIX=".bz2"
+	mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 | bzip2 -v 2>&1 > $2$SUFFIX
+else
+    echo "No compression option set, check advanced settings"
+fi
+
+
+
+return 0
+}
+dbdump_table_comp () {
+if [ "$COMP" = "gzip" ]; then
+	echo
+	echo Backup	Information	for "$3.gz"
+	SUFFIX=".gz"
+	mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 | gzip > $3$SUFFIX
+	gzip -l "$3.gz"
+elif [ "$COMP" = "bzip2" ]; then
+	echo Compression information for "$3.bz2"
+	SUFFIX=".bz2"
+	mysqldump --user=$USERNAME --password=$PASSWORD --host=$DBHOST $OPT $1 $2 | bzip2 -v 2>&1 > $3$SUFFIX 
+else
+	echo "No compression option set, check advanced settings"
+fi
+return 0
+}
 
 # Compression function plus latest copy
 SUFFIX=""
@@ -415,7 +461,7 @@
 if [ "$COMP" = "gzip" ]; then
 	gzip -f "$1"
 	echo
-	echo Backup Information for "$1"
+	echo Backup Information for "$1.gz"
 	gzip -l "$1.gz"
 	SUFFIX=".gz"
 elif [ "$COMP" = "bzip2" ]; then
@@ -448,6 +494,8 @@
 if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
 	if [ "$CREATE_DATABASE" = "no" ]; then
 		OPT="$OPT --no-create-db"
+	elif [ "$PERTABLE" = "yes" ]; then
+		OPT="$OPT --no-create-db"
 	else
 		OPT="$OPT --databases"
 	fi
@@ -497,15 +545,43 @@
 			 # Prepare $DB for using
 		        MDB="`echo $MDB | sed 's/%/ /g'`"
 
-			if [ ! -e "$BACKUPDIR/monthly/$MDB" ]		# Check Monthly DB Directory exists.
-			then
-				mkdir -p "$BACKUPDIR/monthly/$MDB"
+			if [ "$PERTABLE" = "yes"  ];  # Check backup per table
+			then  # Start Monthly DB backup per table
+				echo Monthly Backup of $MDB per table...
+					TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $MDB| sed 's/ /%/g'`"
+					for TABLE in $TABLES
+					do
+						if [ ! -e "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB" ] # Check Monthly DB per table Directory exists.
+						then
+							mkdir -p "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB"
+						fi
+						if [ "$PIPECOMP" = "yes" ]; then
+							dbdump_table_comp "$MDB" "$TABLE" "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql"
+							BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql$SUFFIX"
+						else
+							dbdump_table "$MDB" "$TABLE" "$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql"
+							compression	"$BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql"
+							BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/${MDB}_pertable/${MDB}_pertable_$DATE.$M.$MDB/$TABLE.sql$SUFFIX"
+						fi
+					done
+				echo ----------------------------------------------------------------------
+			else	# Start Monthly DB full
+				if [ ! -e "$BACKUPDIR/monthly/$MDB" ]		# Check Monthly DB Directory exists.
+				then
+					mkdir -p "$BACKUPDIR/monthly/$MDB"
+				fi
+
+				echo Monthly Backup of $MDB...
+					if [ "$PIPECOMP" = "yes" ]; then
+						dbdump_comp "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
+						BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"
+					else
+						dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
+						compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
+						BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"
+					fi
+				echo  ----------------------------------------------------------------------
 			fi
-			echo Monthly Backup of $MDB...
-				dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
-				compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
-				BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"
-			echo ----------------------------------------------------------------------
 		done
 	fi
 
@@ -515,44 +591,124 @@
 	DB="`echo $DB | sed 's/%/ /g'`"
 	
 	# Create Seperate directory for each DB
-	if [ ! -e "$BACKUPDIR/daily/$DB" ]		# Check Daily DB Directory exists.
-		then
-		mkdir -p "$BACKUPDIR/daily/$DB"
-	fi
-	
-	if [ ! -e "$BACKUPDIR/weekly/$DB" ]		# Check Weekly DB Directory exists.
-		then
-		mkdir -p "$BACKUPDIR/weekly/$DB"
+
+	if [ "$PERTABLE" = "yes"  ];
+	then
+		if [ ! -e "$BACKUPDIR/daily/${DB}_pertable" ]		# Check Daily DB per table Directory exists.
+			then
+			mkdir -p "$BACKUPDIR/daily/${DB}_pertable"
+		fi
+		
+		if [ ! -e "$BACKUPDIR/weekly/${DB}_pertable" ]		# Check Weekly DB per table Directory exists.
+			then
+			mkdir -p "$BACKUPDIR/weekly/${DB}_pertableE"
+		fi
+	else
+		if [ ! -e "$BACKUPDIR/daily/$DB" ]		# Check Daily DB Directory exists.
+			then
+			mkdir -p "$BACKUPDIR/daily/$DB"
+		fi
+		
+		if [ ! -e "$BACKUPDIR/weekly/$DB" ]		# Check Weekly DB Directory exists.
+			then
+			mkdir -p "$BACKUPDIR/weekly/$DB"
+		fi
 	fi
 	
 	# Weekly Backup
 	if [ $DNOW = $DOWEEKLY ]; then
 		echo Weekly Backup of Database \( $DB \)
-		echo Rotating 5 weeks Backups...
-			if [ "$W" -le 05 ];then
-				REMW=`expr 48 + $W`
-			elif [ "$W" -lt 15 ];then
-				REMW=0`expr $W - 5`
-			else
-				REMW=`expr $W - 5`
+		if [ "$PERTABLE" = "yes"  ];
+		then
+		
+			if [ ! -e "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE" ]		# Check Weekly DB per table Directory exists.
+				then
+				mkdir -p "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE"
 			fi
-		eval rm -fv "$BACKUPDIR/weekly/$DB_week.$REMW.*" 
-		echo
-			dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
-			compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
-			BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"
-		echo ----------------------------------------------------------------------
+			
+			echo Rotating 5 weeks Backups...
+				if [ "$W" -le 05 ];then
+					REMW=`expr 48 + $W`
+				elif [ "$W" -lt 15 ];then
+					REMW=0`expr $W - 5`
+				else
+					REMW=`expr $W - 5`
+				fi
+			eval rm -rfv "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$REMW.*" 
+			echo
+			TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $DB| sed 's/ /%/g'`"
+			for TABLE in $TABLES
+			do
+				if [ "$PIPECOMP" = "yes" ]; then
+					dbdump_table_comp "$DB" "$TABLE" "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql$SUFFIX"
+				else
+					dbdump_table "$DB" "$TABLE" "$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql"
+					compression	"$BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/${DB}_pertable/${DB}_pertable.week.$W.$DATE/$TABLE.sql$SUFFIX"
+				fi
+			done
+			echo ----------------------------------------------------------------------
+		else
+			echo Rotating 5 weeks Backups...
+				if [ "$W" -le 05 ];then
+					REMW=`expr 48 + $W`
+				elif [ "$W" -lt 15 ];then
+					REMW=0`expr $W - 5`
+				else
+					REMW=`expr $W - 5`
+				fi
+			eval rm -fv "$BACKUPDIR/weekly/$DB/${DB}_week.$REMW.*" 
+			echo
+				if [ "$PIPECOMP" = "yes" ]; then
+					dbdump_comp "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"
+				else
+					dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
+					compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"
+				fi
+			echo ----------------------------------------------------------------------
+		fi
 	
 	# Daily Backup
 	else
 		echo Daily Backup of Database \( $DB \)
-		echo Rotating last weeks Backup...
-		eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*" 
-		echo
-			dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
-			compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
-			BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"
-		echo ----------------------------------------------------------------------
+		if [ "$PERTABLE" = "yes"  ];
+		then
+			echo Rotating last weeks Backup...
+			eval rm -rfv "$BACKUPDIR/daily/${DB}_pertable/*_pertable*.$DOW"
+			if [ ! -e "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW" ]		# Check Daily DB per table Directory exists.
+			then
+				mkdir -p "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW"
+			fi
+			TABLES="`mysql --user=$USERNAME --password=$PASSWORD --host=$DBHOST --batch --skip-column-names -e "show tables" $DB| sed 's/ /%/g'`"
+			for TABLE in $TABLES
+			do
+				if [ "$PIPECOMP" = "yes" ]; then
+					dbdump_table_comp "$DB" "$TABLE" "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql$SUFFIX"
+				else
+					dbdump_table "$DB" "$TABLE" "$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql"
+					compression	"$BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/${DB}_pertable/${DB}_pertable_$DATE.$DOW/$TABLE.sql$SUFFIX"
+				fi
+			done
+			echo ----------------------------------------------------------------------
+		else
+			echo Rotating last weeks Backup...
+			eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*" 
+			echo
+				if [ "$PIPECOMP" = "yes" ]; then
+					dbdump_comp "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"
+				else
+					dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
+					compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
+					BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"
+				fi
+			echo ----------------------------------------------------------------------
+		fi
 	fi
 	done
 echo Backup End `date`
@@ -565,9 +721,14 @@
 	# Monthly Full Backup of all Databases
 	if [ $DOM = "01" ]; then
 		echo Monthly full Backup of \( $MDBNAMES \)...
-			dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
-			compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
-			BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"
+			if [ "$PIPECOMP" = "yes" ]; then
+				dbdump_comp "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"
+			else
+				dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
+				compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"
+			fi
 		echo ----------------------------------------------------------------------
 	fi
 
@@ -585,9 +746,14 @@
 			fi
 		eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*" 
 		echo
-			dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
-			compression "$BACKUPDIR/weekly/week.$W.$DATE.sql"
-			BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"
+			if [ "$PIPECOMP" = "yes" ]; then
+				dbdump_comp	"$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"
+			else
+				dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
+				compression "$BACKUPDIR/weekly/week.$W.$DATE.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"
+			fi
 		echo ----------------------------------------------------------------------
 		
 	# Daily Backup
@@ -597,9 +763,14 @@
 		echo Rotating last weeks Backup...
 		eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*" 
 		echo
-			dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
-			compression "$BACKUPDIR/daily/$DATE.$DOW.sql"
-			BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"
+			if [ "$PIPECOMP" = "yes" ]; then
+				dbdump_comp "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"
+			else
+				dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
+				compression "$BACKUPDIR/daily/$DATE.$DOW.sql"
+				BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"
+			fi
 		echo ----------------------------------------------------------------------
 	fi
 echo Backup End Time `date`
