Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created December 14, 2012 23:28
Show Gist options
  • Save jtarleton/4289555 to your computer and use it in GitHub Desktop.
Save jtarleton/4289555 to your computer and use it in GitHub Desktop.
Shell script to dump MySQL as a gzipped file for each table
#!/bin/bash
if [ "$1" == "" ]; then
echo "Usage: $0 <database> [output-path]"
exit 1
fi
TABLES=`mysql -h localhost -u someuser -p somepasswd "select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA='$1'"`
OUT="$2"
if [ "$OUT" == "" ]; then
OUT="."
fi
if [ ! -d $OUT ]; then
echo "$OUT does not exist."
exit 1
fi
for table in $TABLES; do
echo -n "Dumping $1.$table..."
mysqldump -u root -p root $1 $table | gzip > $OUT/$1.$table.sql.gz
echo "Done."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment