First, some background.
Last week I received the final pieces of my backup strategy, a pair of 500 GB hard drives that I will be rotating periodically to some undisclosed location that isn’t my house. Right now, my fileserver makes backups and sticks them on a mirrored RAID array. These drives are simply to hold extra copies of these backup files in the event of something Very Bad(tm) happening to our house.
Since it will be leaving my house, I decided I wanted to make sure the data was wrapped up in a nice blanket of encryption goodness. For ease of use, I have an eSATA docking station that allows the drives to be ‘hot-swapped’ – and for further convenience I’m attaching it to my desktop instead of the server so that it will be harder to forget to rotate the drives out (in the basement = out of sight, out of mind).
So all of this presented an opportunity to learn the relatively-new command line environment, Microsoft PowerShell.
The scripts (feel free to download) do the following:
- Mount my hard drives as encrypted TrueCrypt volumes, using a keyfile. Now anything written to the drives are wrapped in that nice encryption blanket I mentioned earlier. Mmm… scrambled bits and blankets.
- Checks the amount of space available in the target directory and deletes the necessary number of old backups to make sufficient room for the new backup copies.
- Calls on RoboCopy (included in Windows Vista) to do the actual file copying. Note that in my testing, I found RoboCopy to be 3-4 times as fast as xcopy across the network.
- Sends off a completion email
- Dismounts the encrypted volume
Since this was my first use of PowerShell, I’m sure there is a lot of room for improvement and that I’ve only just scratched the surface. That said, I learned enough about PowerShell that I’m going to keep pushing to learn more.
A few things I like:
- It was very easy to use several diffferent MS technologies from the one script. I seemlessly use .NET objects, COM objects, and even pull in some WMI interaction to get the job done.
- Finally having regular expressions available as first-class citizens in scripts – a.la perl and others. No absolute need to explicitly fire up the .NET regex classes all the time, even though that’s definitely possible and useful too.
- Finally not having to rely on essentially screen-scraping command outputs. Yay for objects!
- Writing custom cmdlets in C# is pretty straightforward. I started down this route to make my own Start-Backup cmdlet, but decided against it since I specifically wanted to learn PS scripting and not just write the solution in C#.
A few things I dislike:
- using -lt and -gt (and similar) instead of “<” and “>” for comparisons and equality. It just feels wrong.
- Lack of an official language reference – I couldn’t believe one wasn’t available, and this left me in the dark and learning by trial by error on some key behaviors of powershell (like function return values, for instance). The “documentation pack” has a crappy quick-reference/cheat-sheet sort of thing, but not a real spec.
- How hard it was to track down the PowerShell SDK – note: you have to download the Windows SDK and blindly choose a few installation options to get the SDK installed.
- I made the PowerShell crash a few times. That shouldn’t be able to happen.