How to Backup Multiple MySQL Databases Automatically! [SCRIPT]

Tired of making MYSQL database backups one by one? If you are migrating to another server or want to do a monthly MYSQL database backup of all databases in your server, you can do so by using a script that will backup all of your databases at once.

Of course, you will want to save each database separately in case you just want to restore one at a time plus it will be easier when restoring.

Here’s a script you can use, replace password with your root password.

#!/bin/bash
# webserverhacks.com

USER="root"
PASSWORD="password"
OUTPUTDIR="."
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"

# clean up any old backups - save space
rm "$OUTPUTDIR/*sql" > /dev/null 2>&1

# get a list of databases
databases=`$MYSQL -u$USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|
" | grep -v Database`

# dump each database in turn
for db in $databases; do
   echo $db
   $MYSQLDUMP --force --opt -u$USER -p$PASSWORD $db > "$OUTPUTDIR/$db.sql"
done
Share

Recommended MySQL



This entry was posted in MySQL and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Related News and Resources