I need to backup data and config files on this server, daily. I need to keep:
- daily backups for a week
- weekly backups for a month
- monthly backups for a year
- yearly backups after that
All of this accomplished via a shell script run daily from cron.
This is how the backup files should look after 10 years of running:
blog-20050103.tar.bz2
blog-20060102.tar.bz2
blog-20070101.tar.bz2
blog-20080107.tar.bz2
blog-20090105.tar.bz2
blog-20100104.tar.bz2
blog-20110103.tar.bz2
blog-20120102.tar.bz2
blog-20130107.tar.bz2
blog-20130902.tar.bz2
blog-20131007.tar.bz2
blog-20131104.tar.bz2
blog-20131202.tar.bz2
blog-20140106.tar.bz2
blog-20140203.tar.bz2
blog-20140303.tar.bz2
blog-20140407.tar.bz2
blog-20140505.tar.bz2
blog-20140602.tar.bz2
blog-20140707.tar.bz2
blog-20140728.tar.bz2
blog-20140804.tar.bz2
blog-20140811.tar.bz2
blog-20140816.tar.bz2
blog-20140817.tar.bz2
blog-20140818.tar.bz2
blog-20140819.tar.bz2
blog-20140820.tar.bz2
blog-20140821.tar.bz2
blog-20140822.tar.bz2
You are seriously over-engineering this. Badly.
Here's some pseudocode:
daily
directorydaily
backupsweekly
directoryweekly
backupsmonthly
directorymonthly
backupsyearly
directoryThe amount of logic you have to implement is about the same, eh? KISS.
This looks easier:
Or, by file count instead of age:
If you just want to keep, for example, 8 daily backups and 5 weekly (every sunday) backups, it works like this:
As of today (2014-11-10), this will output:
As an exercise left for you, you just have to delete all backup files whose names do not appear in the
keep
-array.If you want to keep 13 monthly backups (first sunday of every month) and 6 yearly backups (first sunday of every year) as well, things get a little bit more complicated:
As of today (2014-11-10), this will output:
Same as above, just delete all backup files not found in this array.
I recently had the same problem. IMHO, trying to write a shell script to do it is painful, and it is much easier to write some reusable logic using a higher-level language with builtins like sets, dictionaries, etc. The general idea is to take configuration indicating how many files of each period you want to keep, and then decide for each file if it should be kept.
There is a fairly popular python-based script that looks really nice and has some easy-to-understand source. Plus being python-based instead of shell-based gives it a cross-platform advantage: https://github.com/xolox/python-rotate-backups
As mentioned in a comment, it is normally best to delegate the task of managing backups to a backup management software.
But here is the logic in bash for deleting older backups as per you requirement
PHP Code showing which files will remain after running for 3520 days
https://ideone.com/n2ymQy
I've been through the same problem. Basically needing space on a filesystem where daily full backups were running.
I wrote a script that just prints the rm -f commands.
Basic but good enough for me. Easy to adapt/improve.
Benefits:
Limitations:
Script output example: