This is the structure of my backup:
- Backups are stored to a directory named
cron_hourly_backup
- Inside that directory a directory is created each day which is named with
ddmmyyyy
format. - In each of these directories there are 5-6 db backups which are dumped every hour through a cron-job, and every hour's backup files have unique name by using time stamp (ex:
db1_000000.zip .... db5_000000.zip
uptodb1_230000.zip ... db5_230000.zip
)
Now I want to programmatically delete all backup files older than 1 day (OR, keep today's and yesterday's all backup), But keep one latest db (of all 5 dbs) for each day. How can I achieve this?
Currently I'm doing this:
find . -type f \( -name "*_00*" \
-o -name "*_01*"-o -name "*_02*" \
-o -name "*_03*" -o -name "*_04*" \
-o -name "*_05*" -o -name "*_06*" \
-o -name "*_07*" -o -name "*_08*" \
-o -name "*_09*" -o -name "*_10*" \
-o -name "*_11*" -o -name "*_12*" \
-o -name "*_13*" -o -name "*_14*" \
-o -name "*_14*" -o -name "*_15*" \
-o -name "*_16*" -o -name "*_17*" \
-o -name "*_18*" -o -name "*_19*" \
-o -name "*_20*" -o -name "*_21*" \
-o -name "*_22*" \) -delete
This works great, problem is
- if 23rd hour backup is not available for any day, then I will lose all files of that day.
- It will also delete today's and yesterday's backups.
Any suggestions on how to solve the above 2 issues is much appreciated.
Not sure what "But keep one latest db (of all 5 dbs) for each day" means. If it means "for each day keep only the last (in lexicographic order) file", and if you have the
coreutils
date
utility, a bash script like this could work (not tested):If you want to keep the last of each
dbN_*
with1<=N<=6
, then you can use one more loop level (not tested):