How to Back Up/Export All MySQL Databases in Separate Files with Mysqldump?

If we plan to back up /export only one MySQL databases in a file, we can use the following method:

mysqldump -u root -p mydatabase > /home/myuser/database-dump.sql

If we want to back up several databases only in a file, we can use:

mysqldump -u root -p --databases database_name_a database_name_b > databases_a_b.sql

If we plan to export all databases in a file, we can use:

mysqldump -u root -p --all-databases > all_databases.sql

If we need to back up /exports all MySQL databases in separate files, we can use the Linux shell scripts:

#! /bin/bash
 
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="www.glassescanada.com"  # enter mysql database user name here!
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="glasses canada"  # enter user name password here!
MYSQLDUMP=/usr/bin/mysqldump
 
mkdir -p "$BACKUP_DIR/mysql"
 
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
 
for db in $databases; do
  $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done

Create a Compressed MySQL Database Backup:

mysqldump database_name | gzip > database_name.sql.gz

Create a Backup with Timestamp

mysqldump  database_name > database_name-$(date +%Y%m%d).sql