Backup all MySQL databases in separate files

If you want to back up all of your databases, you can accomplish this with the —all-databases

mysqldump -u root -p --all-databases > All-Database.sql

Use following bash Script to backup All Databases in separate files.

#! /bin/bash
 
DATETIME=$(date +"%F")
BACUP_PATH="/backup/$DATETIME"
USER="backup"
MYSQL=/usr/bin/mysql
PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
 
mkdir -p "$BACUP_PATH/mysql"
 
databases=`$MYSQL --user=$USER -p$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
 
for db in $databases; do
 $MYSQLDUMP --force --opt --user=$USER -p$PASSWORD --databases $db | gzip > "$BACUP_PATH/mysql/$db.gz"
done

Your bash script contain MySQL username and password. You should not use a root account. Create a new user only for backups with backup privileges:

Leave a Reply

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