Skip to content

Instantly share code, notes, and snippets.

@thinsoldier
Last active April 11, 2016 07:46
Show Gist options
  • Save thinsoldier/453e35f00568d2d45af6 to your computer and use it in GitHub Desktop.
Save thinsoldier/453e35f00568d2d45af6 to your computer and use it in GitHub Desktop.
Backup (mysql dump) all your MySQL databases in separate files
#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="./$TIMESTAMP"
MYSQL_USER="root"
MYSQL=mysql
MYSQL_PASSWORD=""
MYSQLDUMP=mysqldump
mkdir -p "$BACKUP_DIR"
databases=`$MYSQL --user=$MYSQL_USER -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --lock-tables=false --user=$MYSQL_USER --databases $db > "$BACKUP_DIR/$db.sql"
done
@thinsoldier
Copy link
Author

Original code

! /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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment