Skip to content

Instantly share code, notes, and snippets.

@phillip-boombox
Last active January 19, 2017 20:39
Show Gist options
  • Save phillip-boombox/288ab16c88fa97dc58808371e84dbe9f to your computer and use it in GitHub Desktop.
Save phillip-boombox/288ab16c88fa97dc58808371e84dbe9f to your computer and use it in GitHub Desktop.
A BASH script to export all MySQL databases to individual files and another to import the files back to MySQL. Adapted from http://stackoverflow.com/a/26096339/3299349 and https://gist.github.com/tenold/aa5e107d93c0f54436cb
#!/bin/bash
USER="root"
ExcludeDatabases="Database|information_schema|performance_schema|mysql"
databases=`mysql -u $USER -e "SHOW DATABASES;" | tr -d "| " | egrep -v $ExcludeDatabases`
for db in $databases; do
echo "Dumping database: $db"
mysqldump -u $USER --databases $db > `date +%Y%m%d`.$db.sql
gzip `date +%Y%m%d`.$db.sql
done
#!/bin/bash
USER="root"
FILES='./*.gz ./*.sql'
for f in $FILES
do
if file --mime-type "$f" | grep -q gzip$; then
echo "Processing gzipped: $f"
gzcat $f | mysql -u $USER
else
echo "Processing: $f"
mysql -u $USER < $f
fi
done
@phillip-boombox
Copy link
Author

phillip-boombox commented Jan 19, 2017

To Use

  1. Download these scripts into a folder that will contain backups.
  2. Update ExcludeDatabases in export-mysql.sh to exclude any additional databases.
  3. Open a terminal window and cd into the folder.
  4. Make these scripts executable by typing chmod +x export-mysql.sh & chmod +x import-mysql.sh.
  5. Execute by typing ./export-mysql.sh or ./import-mysql.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment