Linux scheduled backups and cleanup.

3 posts in this topic

Here is a couple of scripts that will backup database with date/time stamps and cleanup old backups.

First let's create the backup script.


# Change DATABASE, USER, and PASS to match your
# mysql parameters.
# DBSERVER can be changed IF you have enabled remote
# access to your MySQL server.
# Change DEST to where you would like to store backups.
# No trailing /
FILE=$DEST/exile.`date +"%Y_%m_%d_%H%M"`.sql

mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}

gzip $FILE

Now we create the cleanup script.


# Database Cleanup Script
# Change DEST to your backup directory, no trailing /
# Change +30 to the age in days to delete. (+7 = 7 days +30 = 30 Days)
find $DEST/* -mtime +7 -exec rm {} \;

Use the "crontab -e" command to schedule these scripts. You can use this to help with your schedule.

I add this to the end of my cron to schedule backups every 30 minutes and cleanup once a week. (4am sundays)

You will want to change the locations of your scripts accordingly.

#Exile Database Backup
*/30 * * * * /home/unbound/scripts/dbbackup.sh

#Exile Backup Cleanup
0 4 * * 0 /home/unbound/scripts/dbcleanup.sh

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.