Last active
January 19, 2017 20:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To Use
ExcludeDatabases
inexport-mysql.sh
to exclude any additional databases.cd
into the folder.chmod +x export-mysql.sh & chmod +x import-mysql.sh
../export-mysql.sh
or./import-mysql.sh