Topics

Use mysqldump to create separate files and directories for databases and tables

The separate directory for every and bz2 for every inside that database.

#!/bin/sh

#edit these
=""
=""
MYSQLDIR="/path/to/backupdir"

MYSQLDUMP="/usr/bin/mysqldump"
="/usr/bin/"

echo - Dumping DB structure "$MYSQLDIR"/all.bz2 
$MYSQLDUMP --user=$USER --=$ -d --all-databases | bzip2 > "$MYSQLDIR"/all.bz2 

echo - Dumping tables for each DB
databases=`$MYSQL --user=$USER --=$ -e " DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases; do
    echo -  "$db" DB
	mkdir $MYSQLDIR/$db
	chmod -R 777 $MYSQLDIR/$db
	for tb in `$MYSQL  --user=$USER --=$ -N -B -e "use $db ;show tables"`
		do 
			echo -- Creating table $tb
			$MYSQLDUMP --opt  --delayed-insert --insert-ignore --user=$USER --=$ $db $tb | bzip2 -c > $MYSQLDIR/$db/$tb..bz2
	done
	echo
done