Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

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.

Avatar of IanTh
IanTh
Flag of United Kingdom of Great Britain and Northern Ireland image

there are a number of linux backup tools available are you sure you want to use rsync
Avatar of hankknight

ASKER

I am using an online service that only supports rsync.
SOLUTION
Avatar of IanTh
IanTh
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of arober11
arober11
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bman21
bman21

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/DIRECTORY/SUNDAY/

#Monday
0 1 * * 1 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/MONDAY/

#Tuesday
0 1 * * 2 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/TUESDAY/

#Wednesday
0 1 * * 3 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/WEDNESDAY/

#Thursday
0 1 * * 4 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/THURSDAY/

#Friday
0 1 * * 5 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/FRIDAY/

#Saturday
0 1 * * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/SATURDAY/

##############################
# 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/DIRECTORY/WEEK_1/

#Week 2
0 2 8-14 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/WEEK_2/

#Week 3
0 2 15-21 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/WEEK_3/

#Week 4
0 2 22-31 * 6 rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/WEEK_4/

#################
# Monthly backups  #
#################

#January
0 3 * 1 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/January/

#February
0 3 * 2 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/February/

#March
0 3 * 3 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/March/

#April
0 3 * 4 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/April/

#May
0 3 * 5 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/May/

#June
0 3 * 6 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/June/

#July
0 3 * 7 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/July/

#August
0 3 * 8 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/August/

#September
0 3 * 9 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/September/

#October
0 3 * 10 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/October/

#November
0 3 * 11 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/November/

#December
0 3 * 12 * rsync -avu --delete /LOCAL/DIRECTORY/ -e ssh remote_server:/REMOTE/DIRECTORY/December/

##################
# 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.