Swash

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.

dbbackup.sh

#!/bin/sh
#----------------------------------------------------
# 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 /
#----------------------------------------------------
DEST=/home/unbound/A3Exile/dbbackup
FILE=$DEST/exile.`date +"%Y_%m_%d_%H%M"`.sql
DBSERVER=127.0.0.1
DATABASE=exile
USER=exile
PASS=password12345

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

gzip $FILE

Now we create the cleanup script.

dbcleanup.sh

#!/bin/bash
#----------------------------------------------------
# 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)
#----------------------------------------------------
DEST=/home/unbound/A3Exile/dbbackup
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
Advertisement

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.