Created
December 3, 2014 14:29
-
-
Save matteomattei/a02348252d2d47aa5913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DB_USER="root" | |
DB_PASSWORD="yourdbrootpassword" | |
MAIL_NOTIFICATION="[email protected]" | |
BACKUP_FOLDER="/root/mysql_backup" | |
NUM_COPIES=7 | |
for db in $(mysql -u${DB_USER} -p${DB_PASSWORD} -e 'show databases;' | grep -Ev "^(Database|mysql|information_schema)$") | |
do | |
echo "processing ${db}" | |
[ -d "${BACKUP_FOLDER}/${db}" ] || mkdir -p "${BACKUP_FOLDER}/${db}" | |
cd "${BACKUP_FOLDER}/${db}" | |
mysqldump --opt -u${DB_USER} -p${DB_PASSWORD} "${db}" | gzip > dump_$(date +%F_%T).sql.gz || mail -s "error backing up database ${db}" ${MAIL_NOTIFICATION} | |
while [ `ls dump_* | wc -l` -gt ${NUM_COPIES} ] | |
do | |
FIRST=`ls -t dump_* | tail -n1` | |
rm -f "${FIRST}" | |
done | |
done | |
mkdir -p ${BACKUP_FOLDER}/ALL_DATABASES | |
cd ${BACKUP_FOLDER}/ALL_DATABASES | |
mysqldump --opt -u${DB_USER} -p${DB_PASSWORD} --events --ignore-table=mysql.event --all-databases | gzip > all_$(date +%F_%T).sql.gz || mail -s "error backing up all databases" ${MAIL_NOTIFICATION} | |
while [ `ls all_* | wc -l` -gt ${NUM_COPIES} ] | |
do | |
FIRST=`ls -t all_* | tail -n1` | |
rm -f "${FIRST}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment