Remove all files from a month but last one
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 ea开发者_如何学编程sy since I have current day. But how I can keep the last of each month?
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.
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 yyyyMMdd.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.
精彩评论