Subscribe to the RSS Feed

Paradox - a statement or proposition that seems self-contradictory or absurd but in reality expresses a possible truth.

:: BINARY PARADOX ::

Stepping stone to the /dev/null in the sky

Powershell Backup Script

I’ve been playing a bit with Windows Powershell (codenamed Monad). Not a bad piece of software all things considered! Certainly a world better than standard Windows XP cmd.exe.

My problem task was that I needed a basic backup script that I could add as an ‘at’ job for a Windows Server 2003 box at work.

It is a very small time operation so all of our backups are stored on two external harddrives. At the end of each week we swap the currently plugged in drive and cycle it out with the second drive. This way we can have one backup drive stored offsite and in a worst case scenario, we’ve only lost a week of data.

It is hardly a perfect solution, but it is incredibly cheap and easy for non-technical folks to manage since I’m only available parttime.

The aforementioned script is required to determine which drive is plugged in (because you can’t just have one standard Windows Backup Tool job because the target drive letter will change, one drive mounts as Z:\ the other as X:\). This script also needs to determine how much free space is left on the backup device and then remove older backups if there isn’t adequate space for a new backup. Pretty simple cycling task.

Several improvements can be made over my beta product. It would be beneficial to keep a backup catalog on the system to make it easier to find specific backups later. The script is also not signed. This means to run it from the powershell you will have to change the default security settings. Not the best way of doing things… Additionally, due to my lack of .net knowledge, and my newbieness with powershell, I was forced to perform a few hacks because they “just work” and I don’t know of a better way.

The most obvious of these hacks is when I query the system for some information and aren’t sure whether I’ll be getting 0, 1, or more results. The problem that I encountered is this: If you query the loaded ‘removable’ type drives from the system with the following command:

$removeableDrives = get-wmiobject -class Win32_LogicalDisk -filter "Drivetype=2"

And only 1 drive is returned, you access it with “$removeableDrives.name”. However if multiple drives are returned, and you wish to access the first drive of those returned results you need to access it with no "$removeableDrives[0].name" . I wasn’t able to find a way to handle this gracefully and it resulted in some hacks.

Edit: By casting the result as an array I was able to take care of this problem and treat all results as an array, even if just one result was returned.

It is certainly beta software, but you are welcome to take my script, modify it, use it, abuse it and so forth.

Use at your own risk though! It is certainly not a bulletproof backup solution.

Edit: The script additionally formats the size of the folder to backup, and the free space of the backup device a little nicer.

Edit: The script can now send status messages via SMTP to an e-mail address of your choice

file: powershell_backup_version1_0_2.ps1 [4.74KB]
Category: scripts
download: 98


Transmissions:

Begin Transmission:

Add your thoughts, preview and then submit

Please note, your comment will not appear until after it has been moderated & approved. Sorry!


Hidden


Textile Help

You may also be interested in...