This is a password protected post.
As any long-time system admin or tech can tell you, backups and redundancy are vital. They make it possible to sleep without worry.
One thing that makes that easier is Microsoft’s Hyper-V. It’s a tool for creating virtual machines (VMs); these are computers that exist within software. This means you can copy and move them as files. One particularly nice thing that’s built into Hyper-V is the ability to make checkpoints for the VMs. These save the virtualized computer in exactly the state they were in when the checkpoint was created. It’s handy when installing new software or changing configurations as it gives you a point in time you can revert back to. So, if you were to get a virus, delete an important application, or misconfigure a new deployment, you can undo those things by applying the checkpoint.
It’s very useful, so we wanted a way to back up our servers with regularity so that all of our 20+ VMs would have a number of backups available at any time. Sadly, Microsoft’s Hyper-V Manager doesn’t have any way to automate this process, and doing it manually through the Manager takes a long time, especially if we were to do it weekly, which we wanted to do while I was working for Missouri State University.
So, I made a script that does it for me. And because I convinced my superiors and the University’s lawyers to allow me to submit all of the code and tools we wrote to the open-source community, I can share it with the Internet, so anyone can use it. It utilizes a PowerShell script and Microsoft’s built in tools to add redundancy that the Hyper-V manager doesn’t have. You can download it here.
A breakdown of what it does:
I have the script set to run weekly through the task scheduler. When it runs, it makes a checkpoint of all the VMs that are selected. By default, I used the ‘*’ to have it select all of them. It creates a checkpoint named Auto Checkpoint, and then adds the date, using year, month, day, hour, and minute. This allows it to be sortable by name. After all of the checkpoints are created, it will go through a loop for each VM. If the script finds more checkpoints than you specify in the variable $KeptCheckpoints, it will delete the oldest. It also requires that the name matches “Auto Checkpoint …” so any manually created checkpoints are exempt. Ultimately, you’ll end up with a rolling set of checkpoints. I kept 4 weeks’ worth so that I’d always have a month of full machine backups.
A quick note about why I didn’t just sort them by creation date. I initially had a much shorter and elegant script that simply looked at how old the checkpoints were, based off creation date and name. If they were older than X amount of days, it’d delete them. The problem is that I quickly discovered an occasional bug where the checkpoint’s CreationTime attribute would randomly report that it was made on 12/31/1600. When that happened, all the checkpoints would be deleted, regardless of their actual creation. Sorting by name resolved that issue.
This script, in conjunction with utilizing Hyper-V replica to have the VMs on multiple physical servers, and backing up important data with a combination of Windows backup services and Cron-jobs for the linux servers meant that I had much less worry about losing data or having significant downtime. It meant I slept well.