Skip to content

Instantly share code, notes, and snippets.

@aamnah
Created June 18, 2016 08:19
Show Gist options
  • Select an option

  • Save aamnah/8b5275fbd318f15ee8e4cbdd96b807c7 to your computer and use it in GitHub Desktop.

Select an option

Save aamnah/8b5275fbd318f15ee8e4cbdd96b807c7 to your computer and use it in GitHub Desktop.
A simple script that dumps compressed mysql databases individually with a timestap
#!/bin/bash
USER='BACKUPUSER'
PASSWORD='PASSWORD'
DIR='/backups/mysql' # don't use the trailing slash as it is used in the command
# see `date --help` for format controls
# %b = Jan
# $H = hour (00..23)
# %M = minute (00..59)
# %T = time; same as %H:%M:%S
TIMESTAMP=`date +%Y%b%d.%T`
ExcludeDatabases='Database|information_schema|performance_schema|mysql|phpmyadmin'
databases=`mysql -u ${USER} -p${PASSWORD} -Bse "SHOW DATABASES;" | egrep -v $ExcludeDatabases`
for db in ${databases}; do
echo "Dumping database: $db"
mysqldump -u ${USER} -p${PASSWORD} --databases $db | gzip -9 > ${DIR}/${TIMESTAMP}.$db.sql.gz
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment