Backup all MySQL Databases of a User

The following script can be used to backup all MySQL-Databases of a user each in it’s own compressed. sql file inside a folder with todays date:

#!/bin/bash

DBASELIST=`mktemp`
mysqlshow -uusername -ppassword | awk '{print $2}' | grep -v Databases | sort >$DBASELIST
cd /backuplocation
mkdir -p `date +%Y%m%d`
cd `date +%Y%m%d`
for x in `cat $DBASELIST`; do
    echo "Backup Database: $x";
    mysqldump --opt -uusername -password $x >$x.sql;
done;
gzip -f *

You just need to substitute username, password and backuplocation. If you use the root-MySQL user all databases of the whole MySQL-server are backed up.

Leave a comment

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