Skip to content

Instantly share code, notes, and snippets.

@craigw
Created November 3, 2009 10:06
Show Gist options
  • Save craigw/224927 to your computer and use it in GitHub Desktop.
Save craigw/224927 to your computer and use it in GitHub Desktop.
Back up MySQL databases
#! /bin/bash
RETENTION=14
BACKUP_DIRECTORY='/var/backups/mysql'
NICE='/usr/bin/nice -n 20'
MYSQLDUMP='/usr/bin/mysqldump'
BZIP='/bin/bzip2'
RM='/bin/rm'
PASSWORD=`cat /etc/mysql.passwd` # Make sure it's `chmod go-wrx`!
CWD=`/bin/pwd`
UMASK=`umask`
umask u=r,go-wrx
cd $BACKUP_DIRECTORY
SQLDUMP=`date +%Y%m%d`.sql
$NICE $MYSQLDUMP -u root --password='$PASSWORD' --all-databases > ./$SQLDUMP
$NICE $BZIP ${SQLDUMP}
FALLOFF="`date --date="$RETENTION days ago" +%Y%m%d`.sql"
[ -f $FALLOFF ] && $NICE $RM -f $FALLOFF
cd $CWD
umask $UMASK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment