Search code examples
mysqlbashshellrsyncmysql-python

Create a script rotating backup on remote disk


Firstly, I'm completely lost when it comes to shell scripting so I'm expecting a fairly easy solution to this. I just don't know where to find it.

The situation is this: We are running an application from which we take backups (duh!). The application comes with it's own backup script which when run creates a folder with the current date and time as name. Within these are a database dump and some config files like such:

# cd backup
# ll
drwxr-xr-x 2 root root 4096 Mar  6 02:30 2012-03-06_02-15 
# ll 2012-03-06_02-15
-rw-r--r-- 1 root root   26706579 Mar  6 02:16 Application.tar.gz
-rw-r--r-- 1 root root      26060 Mar  6 02:15 Config.tar.gz
-rw-r--r-- 1 root root 5503574530 Mar  6 02:21 DatabaseBackup.sql.gz

The backup folder is then rsync'd to a network drive.

The script also allows an argument for the number of days backup to keep. Which is great. But now we're starting to see concerns with disk space on the production server.

Replicating the scripts functionallity to dump the database and compress the configs isn't a problem.

But where I get lost is here: I would ideally like to keep the latest backup on the production server, and keep X backups on the network drive.

I'm thinking this shouldn't be too hard using bash (or maybe expect?)


Solution

  • The following will delete everything except the most recent item in a directory:

    ls -lrt | grep -v total | awk '{print $9}' | head -n -1 | xargs rm 
    

    If you require X number of remaining items you can alter head -n -1 to head -n -X