Skip to content

Instantly share code, notes, and snippets.

@ragusa87
Last active July 5, 2016 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragusa87/18d1de1fd03059aead42449fd01d31d5 to your computer and use it in GitHub Desktop.
Save ragusa87/18d1de1fd03059aead42449fd01d31d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Backup Mysql database staring with "__apps", compress the dump.
# Olds backup are removed if they are older than X days
# LCO 2016-06-05
# Script based on http://www.cyberciti.biz/faq/ubuntu-linux-mysql-nas-ftp-backup-script/
MUSER="root"
MPASS="PASSWORD-GOES-HERE"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/var/backups/mysql"
GZIP="$(which gzip)"
NOW=$(date +"%Y%m%d")
[ ! -d "$BAK" ] && mkdir -p "$BAK"
QUERY="show databases LIKE 'apps__%';"
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse "$QUERY")"
for db in $DBS
do
FILE=$BAK/$db.$NOW-$(date +"%H%M%S").sql.gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
# delete files older than 7 days
find $BAK -atime +7 -name '*.gz' -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment