Skip to content

Instantly share code, notes, and snippets.

@dwinurhadia
Forked from andsens/dump.sh
Created October 24, 2023 07:04
Show Gist options
  • Save dwinurhadia/998a942b560bd2418f94aecba204d32a to your computer and use it in GitHub Desktop.
Save dwinurhadia/998a942b560bd2418f94aecba204d32a to your computer and use it in GitHub Desktop.
Backup all MySQL databases into separate files
#!/bin/sh
# backup each mysql db into a different file, rather than one big file
# as with --all-databases - will make restores easier
USER="backup"
PASSWORD="s3cr3t"
OUTPUTDIR=$(dirname $0)"/databases"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
databases=`$MYSQL --user=$USER --password=$PASSWORD --batch --skip-column-names -e "SHOW DATABASES;" | grep -v 'mysql\|information_schema'`
for database in $databases; do
$MYSQLDUMP \
--user=$USER --password=$PASSWORD \
--force \
--quote-names --dump-date \
--opt --single-transaction \
--events --routines --triggers \
--databases $database \
--result-file="$OUTPUTDIR/$database.sql"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment