Backup Multiple MySQL Databases using a BASH script


In this tutorial I will give an example to a BASH scripts that will help you backup multiple MySQL databases at once.

Backup Multiple MySQL Databases using a BASH script

Backup multiple MySQL databases

First we need to tell the script to generate text file with a DB list using ‘mysql’ and ‘mysqldump’ commands:

[code lang=”bash” inline=”yes”]
echo “Generating DB list file”;
mysql -u MYSQLUSERNAME -pMYSQLUSERPASSWORD -Bse ‘show databases’ > /listfile.txt

No we can export the DB from the list into files (all but ‘information_schema’ DB):

[code lang=”bash” inline=”yes”]
echo “Dumping SQL Databases”;
cat /listfile.txt | while read line
if [ $line != “information_schema” ] ;
mysqldump -u MYSQLUSERNAME -pMYSQLUSERPASSWORD $dbname > /$dbname.sql

Don’t forget to replace “MYSQLUSERNAME” “MYSQLUSERPASSWORD” with your User and Password.

You can create a single “tar.gz” file from your exported databases using this command:

[code lang=”bash” inline=”yes”]
tar -cvzf /*.sql > /filename.tar.gz


Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.