Script Dumpster: Keep last X amount of files or Y amount of days
Ever had the need to remove all but the last X amount of copies of a back-up?
And I’m not talking about the copies from the last 2 days, because this can cause issues if the files aren’t created daily [in case of a weekend], in case a back-up has failed in the meantime or if multiple back-up copies have been made in one day.
While at first I was always playing around with the simple solution by checking file age, I ran into a more elegant solution I’d like to share with you.
NOTE
Both solutions are essentially one liners after the defining of variables, but I’ve split up the code a bit for readability. Also both codes still have the
|
|
switch added, so that you don’t accidentally run this in production and remove the incorrect files. Please change accordingly!
Keep Y amount of days
This example is used to keep the files from the last 4 days in order to make sure I keep the Friday and Monday file available [no separate back-ups are made over the weekend]. This solution writes the deleted file to a separate log file so you can see which files were [perhaps accidentally] deleted. Big downsides to this solution are:
- While I still have the files for Friday and Monday, it means during weekdays I’ll have 4 copies of the back-up file, which can flood your disk.
- If I make multiple back-ups on 1 day, it will still retain those files, as they simple need to have a maximum age before they’re removed.
|
|
Keep X amount of files
Personally this is my preferred and more elegant solution in order to keep only the last X amount of back-up files.
|
|
While this can create an issue when you make multiple back-ups on a single day, it’s would be the preferred solution in most cases.
Happy Scripting! 🙂