How to export all databases in MYSQL via SSH command line dump

The following is a script you can use to dump all of your databases that the user, in this case “backup” has access to. You can save this as a script file and then run it using a CRON job in your control panel.

Note – this command is not doing cleanup of the archives it creates so you will want to rotate the backups or other use it for specific purposes – such as offsite backups where another script performs cleanup.

#! /bin/bash

TIMESTAMP=$(date +”%F”)
BACKUP_DIR=”/backup/$TIMESTAMP”
MYSQL_USER=”backup”
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD=”password”
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

Want to discuss a project?

  • This field is for validation purposes and should be left unchanged.