I made this script after I decided it was easier for me to do it myself than find one online. It takes a backup as often as you'd like (I set a cron for doing it hourly). The backups are saved until their modification time is 7 days old. Note, I said modification time not creation time.
This is because there's no way to figure that out in linux (at least not my version). The only line you should have to change is:
FILE_LOCATION=/root/backups/$SERVER/db/;
Make sure after you edit the location, you create the actual directory they're going to be saved into:
mkdir -p /root/backups/test.server.com/db
backup_db.sh
#!/bin/bash if [ ! $1 ] ; then echo 'Missing server.'; echo 'Syntax: ./backup_db.sh <server> <username> <password>'; exit 1; fi if [ ! $2 ] ; then echo 'Missing username.'; echo 'Syntax: ./backup_db.sh <server> <username> <password>'; exit 1; fi if [ ! $3 ] ; then echo 'Missing password.'; echo 'Syntax: ./backup_db.sh <server> <username> <password>'; exit 1; fi SERVER=$1; USERNAME=$2; PASSWORD=$3; DATE=`date '+%Y-%m-%d-%H:%M:%S'` FILE_LOCATION=/root/backups/$SERVER/db/; mkdir -p $FILE_LOCATION; /usr/bin/mysqldump -h $SERVER -u $USERNAME -p$PASSWORD --opt --all-databases --quote-names | bzip2 -c > $FILE_LOCATION$SERVER-$DATE.sql.bz2; # delete old backups find $FILE_LOCATION -type f -mtime 7 | xargs rm -f
Here's an example cron:
40 * * * * /root/scripts/backup/backup_db.sh db-server.example.com backup mypassword
Here's the line you can edit for changing the amount of days until deletion:
find $FILE_LOCATION -type f -mtime 7 | xargs rm -f