hankknight
asked on
rsync: Daily, Weekly and Monthly backups
I want to use rsync to store daily, weekly and monthly backups of my /home/ directory on my production server to a backup server.
I want seven daily backups to be kept. So every day, the oldest daily backup should be deleted and a new backup added.
I want five weekly backups to be kept. So every week, the oldest weekly backup should be deleted and the newest daily backup should be kept as a weekly backup.
I want twelve monthly backups to be kept. So every month, the oldest monthly backup should be deleted and the newest daily backup should be kept as a weekly monthly backup.
I want seven daily backups to be kept. So every day, the oldest daily backup should be deleted and a new backup added.
I want five weekly backups to be kept. So every week, the oldest weekly backup should be deleted and the newest daily backup should be kept as a weekly backup.
I want twelve monthly backups to be kept. So every month, the oldest monthly backup should be deleted and the newest daily backup should be kept as a weekly monthly backup.
there are a number of linux backup tools available are you sure you want to use rsync
ASKER
I am using an online service that only supports rsync.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
just to expand on robthewolf and IanTh's suggestion, the cron daemon would have to look something like this:
########################## ########## ########## ######
# Daily backups: This will run rsync every night at #
# 1:00 a.m. (you can change the time to whatever you want) #
########################## ########## ########## ######
#Sunday
0 1 * * 0 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/SUND AY/
#Monday
0 1 * * 1 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/MOND AY/
#Tuesday
0 1 * * 2 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/TUES DAY/
#Wednesday
0 1 * * 3 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/WEDN ESDAY/
#Thursday
0 1 * * 4 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/THUR SDAY/
#Friday
0 1 * * 5 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/FRID AY/
#Saturday
0 1 * * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/SATU RDAY/
########################## ####
# Keep 4 WEEKLY "snapshots" #
# runs at 2 am Saturdays if the day #
# of the month falls on the given #
# day range #
########################## ####
#Week 1
0 2 1-7 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/WEEK _1/
#Week 2
0 2 8-14 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/WEEK _2/
#Week 3
0 2 15-21 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/WEEK _3/
#Week 4
0 2 22-31 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/WEEK _4/
#################
# Monthly backups #
#################
#January
0 3 * 1 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Janu ary/
#February
0 3 * 2 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Febr uary/
#March
0 3 * 3 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Marc h/
#April
0 3 * 4 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Apri l/
#May
0 3 * 5 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/May/
#June
0 3 * 6 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/June /
#July
0 3 * 7 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/July /
#August
0 3 * 8 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Augu st/
#September
0 3 * 9 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Sept ember/
#October
0 3 * 10 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Octo ber/
#November
0 3 * 11 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Nove mber/
#December
0 3 * 12 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE CTORY/Dece mber/
##################
# End of Crontab file #
##################
**NOTE** you can use the --dry-run option to ensure it is doing what you want to do without actually copying anything over.
**NOTE** just for clarity, the * * * * * in front of the rsync command tells cron when to run rsync. Each * stands for MINUTES, HOUR, DAY_OF_MONTH, MONTH, DAY_OF_WEEK.
**NOTE** I am not to sure if the weekly scripts will work. Technicaly, the Daily scropts we created is the way to do weekly cron jobs, but since you are wanting to keep a weekly backup, that is the only way that i know how to do it short of actually creating a shell script.
The Daily and Monthly scripts will work though. Notice that I added the "-u" and "--delete" commands. The -u, or update, switch saves time on your backups after the first one has been created. The --delete will delete any files off the remote directory that are no longer existent on the local filesystem.
I know that was a lot to dump on you. If you have any questions please feel free to ask. The above written code should be a "plug and play" minus a few alterations to the actually directory and the ssh connection to your backup site.
##########################
# Daily backups: This will run rsync every night at #
# 1:00 a.m. (you can change the time to whatever you want) #
##########################
#Sunday
0 1 * * 0 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Monday
0 1 * * 1 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Tuesday
0 1 * * 2 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Wednesday
0 1 * * 3 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Thursday
0 1 * * 4 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Friday
0 1 * * 5 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Saturday
0 1 * * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
##########################
# Keep 4 WEEKLY "snapshots" #
# runs at 2 am Saturdays if the day #
# of the month falls on the given #
# day range #
##########################
#Week 1
0 2 1-7 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Week 2
0 2 8-14 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Week 3
0 2 15-21 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#Week 4
0 2 22-31 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#################
# Monthly backups #
#################
#January
0 3 * 1 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#February
0 3 * 2 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#March
0 3 * 3 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#April
0 3 * 4 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#May
0 3 * 5 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#June
0 3 * 6 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#July
0 3 * 7 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#August
0 3 * 8 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#September
0 3 * 9 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#October
0 3 * 10 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#November
0 3 * 11 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
#December
0 3 * 12 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRE
##################
# End of Crontab file #
##################
**NOTE** you can use the --dry-run option to ensure it is doing what you want to do without actually copying anything over.
**NOTE** just for clarity, the * * * * * in front of the rsync command tells cron when to run rsync. Each * stands for MINUTES, HOUR, DAY_OF_MONTH, MONTH, DAY_OF_WEEK.
**NOTE** I am not to sure if the weekly scripts will work. Technicaly, the Daily scropts we created is the way to do weekly cron jobs, but since you are wanting to keep a weekly backup, that is the only way that i know how to do it short of actually creating a shell script.
The Daily and Monthly scripts will work though. Notice that I added the "-u" and "--delete" commands. The -u, or update, switch saves time on your backups after the first one has been created. The --delete will delete any files off the remote directory that are no longer existent on the local filesystem.
I know that was a lot to dump on you. If you have any questions please feel free to ask. The above written code should be a "plug and play" minus a few alterations to the actually directory and the ssh connection to your backup site.