So, I’m writing a powershell script that:
- Uses volume shadow service to take a backup of a directory
- Compresses the above backup using 7zip / whatever
- Time/Datestamps the compressed backup and copies it off-server to a given location
- Looks at the above location and deletes backup zips older than a given number of days (i.e. retention period), e.g. keep 30 days of backups
- But wait, it won’t delete anything unless it knows that a given number of backups exist from the last x days (retention period). After all, if your backups had been failing for 30 days, it would be pretty dumb to delete everything over 30 days old. So the script allows you to set what you deem as a good total number of backups to exist within your retention period in order to delete everything outside of the retention period. Some contingency built in.
- It connects to your authenticated SMTP server (e.g. Google Mail) to send you an HTML formatted report of exactly what it found in that backup location (filenames alongside their age), whether it detected that recent backups were indeed found, and therefore whether backups older than the retention period were tidied.
- Obvs, it supports reading / writing to network shares.
- It supports multiple backup sets per report, using a set of commands like this:
Backup-Tidy 8 6 28 “c:\dir-to-backup” “d:\dir-to-store-zips”
Backup-Tidy 4 2 7 “c:\another-dir-to-backup” “d:\another-dir-to-store-zips”
The arguments for Backup-Tidy:
Backup-Tidy 8 6 28 “c:\dir-to-backup” “d:\dir-to-store-zips” “ForReal”
read as follows: “If you find 8 files from the last 6 days, then remove all files over 28 days old”
- The “ForReal” is a safety belt. Without this argument, the detailed report email is generated but the backups and deletions don’t get done.
And the pretty HTML report looks like this:
Mat’s Backup Tidy Report – 22/02/2014 04:00:36
\\internal.hazymat.co.uk\root\Backup\Mat’s Files [retention period = 28 days]
Recent backups were NOT found. We needed at least 8 zip files 6 days old or less; there are only 3.
We didn’t touch any files; it wasn’t safe to delete anything.
|Total size: 2.6g|
\\internal.hazymat.co.uk\root\Backup\Big Files [retention period = 7 days]
Recent backups found. We needed at least 5 zip files 7 days old or less; there are 7.
Old files were removed: jolly good.
|Total size: 6.12g|