Skip to content

Instantly share code, notes, and snippets.

@th-schwarz
Last active October 21, 2021 08:32
Show Gist options
  • Save th-schwarz/38dd9fa1cdba05506f642a633095bad7 to your computer and use it in GitHub Desktop.
Save th-schwarz/38dd9fa1cdba05506f642a633095bad7 to your computer and use it in GitHub Desktop.
Dumps every mysql database to a single file, all dump files will be zipped and redirected to the stdout.
## Dumps every database to a single file, all dump files will be zipped and redirected to the stdout.
## Usage: backup-db.sh > databases.zip
##
#! /bin/bash
set -o nounset
set -o errexit
trap 'rm -rf "$BACKUP_DIR"' EXIT
BACKUP_DIR=$(mktemp -d)
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
databases=`$MYSQL --defaults-extra-file=/etc/mysql/credentials.cnf --default-character-set=utf8mb4 -e "SHOW DATABASES;" \
| grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --defaults-extra-file=/etc/mysql/credentials.cnf --default-character-set=utf8mb4 --force --opt --databases $db \
> "$BACKUP_DIR/$db.sql"
done
zip -rjq - "$BACKUP_DIR"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment