There is no ‘one size fits all’ solution for automating the process of deleting expired backup files. Depending on the backup procedure (do you do weekly full and daily incrementals or daily full?) and file naming convention (does your backup file have the date as part of the file name or use the same file name every time?) of your resulting backup files, the applicable method differs.
We are going to point out several simple solutions which fit the most common backup scenarios, so odds are one will be right for you.
Deleting by File Age or Date
When to use: Daily full backups.
Perhaps the simplest and most logical way to purge expired backups is to base the deletion process on the date of the backup file. We have previously covered this topic using log files as our target files, however it works just as well with backup files.
For example, to delete any files in the specified folder not modified within the past week, run this command:
Note the keyword above: modified. The ForFiles command is only capable of evaluating the modified file date as opposed to the created date which would be more applicable. Typically, however, you are probably not modifying a backup file after it is created so this will most likely not be an issue.
Alternately, if your backup file has some sort of numeric date pattern specified in the file name (i.e. Backup_2010-01-13.zip, BackupSet_100113_Full.zip, etc.), you can use the DeleteByDatePattern script we provided in the linked article to remove expired backups.
For example, to delete files older than 2 weeks matching a file name pattern like the following: “Backup_YYYY-MM-DD_(Full | Incremental).zip”, you would use the command:
Or if your file naming pattern is: “BackupSet_YYMMDD.zip”, you would use:
Of course, adjust as needed but either of the methods above could easily be added to the start or end of your backup process to keep the number of backups stored manageable.
Folder Rolling
When to use: Periodic full backups (weekly, bi-weekly, etc.) with daily incremental backups in between.
The idea behind “folder rolling” is that you store all of your current backup set (full backup + respective incrementals) in a single folder and then have several archive folders where your old backup sets are kept. Prior to a new backup set being created, you delete the folder contents containing oldest backup set and “roll” the contents of each folder down one.
For example, suppose we have a current backup folder with two archive folders. The batch script commands to perform the folder roll for this would be:
You can add as many archive folders as needed. Just delete the contents of the lowest archive folder add a move command for each of the other archive folders.
Again, this works best for situations where you create a periodic full backup and the a number of incremental backups up until your next full backup. Simply drop all your related backup files into a single folder and the run the folder roll script right before you create a new backup set.
Backup9
When to use: Daily full backups or individual file backups.
Backup9 is a free command line utility developed by Gammadyne. Similar to the folder rolling process above, the idea behind this utility is simple in that when it is run, a copy of the target file is created with a number appended to the end. Additionally, you specify a cut-off of the number of copies to keep with the default being 9 (hence the name).
An example will best explain this process. Using the following command would produce the output below:
If this command were run again, the following would happen:
The number of files to keep (7 in our example) is evaluated and if there are currently that many copies, the last one is dropped. BackupFile. zip. bk7 is deleted. BackupFile. zip. bk6 is renamed to BackupFile. zip. bk7 BackupFile. zip. bk[#] is renamed to BackupFile. zip. bk[#+1] BackupFile. zip. bk1 is renamed to BackupFile. zip. bk2 BackupFile. zip is copied and named BackupFile. zip. bk1
With the ability to keep up to 999 copies, this utility works very well if you have a file with a static name. You simply add the Backup9 command to the beginning or end of your backup process it takes care of keeping up with the appropriate number of archive copies.
Belvedere Automated File Manager
When to use: Daily full backups.
Belvedere automated file manager is a utility which runs in the background monitoring file system active and performs configured actions when specified conditions are met. Among its many uses are cleaning up expired backup files.
The configuration of the rules are pretty straightforward. For example, to create a rule to delete backup files using a file name pattern such as “BackupSet_Jan13.zip” which are older than 2 weeks, you could use the following:
While the basic function we are performing can easily be done with command line tools described above, the obvious difference is that Belvedere provides an easy to use graphical interface for those more comfortable with pointing and clicking.
Belvedere is designed as a desktop user application which runs from the system tray, however you can run Belvedere as a service and use it on servers to perform this and other file monitoring operations.
Conclusion
While there are a myriad number of ways you can manage your backup expiration process, the methods we have described above are both flexible and easy to implement. With a bit of experimentation, find what works for you and go with it so you can just set it and forget it.
Links
Download Backup9 from Gammadyne.com
Download Belvedere from Lifehacker.com