Skip to content

Instantly share code, notes, and snippets.

@bkenny
Created June 1, 2011 10:43
Show Gist options
  • Save bkenny/1002100 to your computer and use it in GitHub Desktop.
Save bkenny/1002100 to your computer and use it in GitHub Desktop.
MySQL Backup Script
#!/bin/bash
BACKUP="/backups/db"
DATE=`date +"%Y-%m-%d"`
WEEKAGO=`date --date='week ago' +"%Y-%m-%d"`
DAY=`date +"%a"`
XWEEKSAGO=`date --date='5 weeks ago' +"%Y-%m-%d"`
# Day on which the weekly backup is kept. Should be the 3 char abbreviation
WEEKLY="Sun";
DIR="${BACKUP}/${DATE}/"
WEEKLYDIR="${BACKUP}/weekly/"
if [ ! -d $DIR ];
then
mkdir -p $DIR
fi
if [ ! -d $WEEKLYDIR ]
then
mkdir -p $WEEKLYDIR
fi
for DATABASE in `echo "SHOW DATABASES" | mysql -p$password -u $user | grep -v ^Database`
do
echo -n Backing up $DATABASE .....
mysqldump -p$password -u $user $DATABASE | gzip -c > ${DIR}/${DATABASE}.sql.gz 2>/dev/null
echo Done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment