The other day I needed a shell script that could backup files and folders to a remote server using rsync as well as rotating the backups.
I wrote a small script that does just this, it requires rsync and sshfs (available on Ubuntu as packages). I wrote the scipt to works as follows:
The machine which contains the data I wish to make a backup of runs this script at certain intervals using a Cron-job, one job for backing up the data and one for rotating the backups. The data is copied to a server which supports rsync over ssh and rotating the backup folders is done by mounting the remote filesystem using sshfs and then renaming folders.The script can be run with two arguments
- dailybackup: uses rsync transfer the selected folders to the backup server. This is run daily at 01:00.
- rotate: this rotates the backup folders named ‘weekly0’, ‘weekly1′,’weekly2′,’weekly3’. This is run every Sunday at 00:30.
This way the folders weekly0 – weekly3 will always contain the following:
- Weekly0 – Backup from the current day, taken at 01:00, this is updated daily.
- Weekly1 – Backup from Sunday last week.
- Weekly2 – Backup from Sunday 2 weeks ago.
- Weekly3 – Backup from Sunday 3 weeks ago.
Except of-course in the beginning where less than 4 weeks have passed.
And last but not least, the actual script can be downloaded here.
UPDATE: The rotate part of the script is probably better off running locally on your backup host, since moving via. sshfs can take quite some time.