views:

26

answers:

2

Every day we run an backup job.

This job creates a new file in format yyyyMMdd.7z and now we need to cleanup our backup storage automatically.

Our backup police says that we need to keep files from last 5 days, and last backup of each month. First step is easy since I have current day. But how I can keep the last of each month?

A: 

To keep the last of each month I would go through the folder of files and for each month keep record of the last file by saving the yyyy, mm and dd from yyyy*MMdd*.7z.

Thus as you are reading through the folder if you get a record where the MM matches an mm you already have then you check the dd. (below) If it doesn't match a mm already stored then add it.

If the saved dd is more current then the new one then delete the file you are at. If the current file is more current then you delete the one saved (finding by concatenating yyyy+MM+dd+".7z" that you saved and replace the stored values with the current file.

Kyra
+2  A: 

We always run our backups overnight, so the last backup of the month conveniently runs on the 1st of the next month in the first hours, typically at 3h17 in the morning. Then the pattern is simply *01.7z .

Maybe you could tweak the backup schedule in a similar way?

otherwise pipe a directory listing of YYYYMM*.7z through select-object --last 1. Move it out of the way, rename it (like Archive_YYYYYMMDD.7z). Then they are easily separated.

Peter Tillemans
I would've proposed a similar solution to @Peter - to see if the backup schedule can be changed to preserve the first of every month instead, which is where your pattern led.
Robert Hui
Simplicity is key to reliability. Reliability is key to backups.
Peter Tillemans