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
# Putting it in /var/backups/mysql seems sensible... on a debian machine that is
# Create a user for the task or simply run it as root
# crontab entry - backup every night at 02:00
# 0 2 * * * /var/backups/mysql/dump.sh
# Create 'backup' mysql user
# CREATE USER 'backup'@'localhost' IDENTIFIED BY 's3cr3t';
# GRANT EVENT, LOCK TABLES, PROCESS, REFERENCES, SELECT, SHOW DATABASES, SHOW VIEW, TRIGGER ON *.* TO 'backup'@'localhost' ;
# Remember to make the script executable
# chmod +x /var/backups/mysql/dump.sh
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