Mysql backup & restore all databases in separate files

Date: 2020-04-01

https://gist.github.com/andsens/3736393

#!/bin/bash
user="backup"
password="s3cr3t"
dir=$(dirname $0)"/databases"
mysqldump=$(which mysqldump)
mysql=$(which mysql)
databases=`$mysql --user=$user --password=$password --batch --skip-column-names -e "SHOW DATABASES;" | grep -v 'mysql$\|information_schema\|performance_schema'`

mkdir -p $dir

for database in $databases; do
    $mysqldump \
        --user=$user --password=$password \
        --force \
        --quote-names --dump-date \
        --opt --single-transaction \
        --events --routines --triggers \
        --databases $database \
        --result-file="$dir/$database.sql"
done

Restore

#!/bin/bash
host=localhost
user="backup"
password="s3cr3t"
mysql=$(which mysql)
files=`ls databases_new/*.sql`

for file in $files; do
        echo "Executing: $file"
        $mysql -h $host --user=$user --password=$password < $file
done
36100cookie-checkMysql backup & restore all databases in separate files