Script that groups backups by date then deletes files+folders older than X days
0
votes
0
answers
315
views
We have 30 days of backups = 30 folders, the backup runs hourly so in each of these folders, there are 24 files in each folder. We need to run a script that keeps the five latest dates of files.
For example, if today is June 30 and I run that script, it doesnt matter how many files are in the folders June 25, June 26, June 27, July 28, July 29 it will keep all five folders and only delete the folders from June 24 and older.
Would also be nice to skip folders that have data < 10kb incase a backup task created an empty zip file.
What I have been testing so far:
find /files/ -ctime +5 -printf "%TY-%Tm-%Td\n" | sort -u -r | tail -n+5 | xargs rm -R
-ctime +5 = which files we want to expire created more than 5 days ago,
-printf "%TY-%Tm-%Td\n" outputs the dates of the folders so we can sort them
sort -u -r = sorting the output so we can do tail
tail -n +5 = tail skips the 5 newest filegroups: grouped by date, not just 5 newest files
Is this logic correct? I also need some kind of xarg to execute "rm -R" on the resultant list of "old backups".
Thanks in advance!
Asked by user480014
(1 rep)
Jul 2, 2021, 10:56 PM
Last activity: Jul 5, 2021, 04:11 PM
Last activity: Jul 5, 2021, 04:11 PM