Last night at the wmug meeting in Victoria, not long after bragging about how 'big' my server was (8 gig) and getting shot down by Firdious' 24 gig beast, I then did the natural thing that any former programmer would do when surrounded by IT Pros .... "ohh yeah, you well should see my powershell script!"

Download Script Here

What: This script will use 7zip to either create a full or differential backup of a folder for you and place it destination location. After the first backup it will perform incremental backups of individual file changes within the folder using the inbuilt functions of 7zip.

Why: A good way to get up to speed on powershell

Usage: powershell.exe d:\7ZipBackupScript.ps1 -sourcefolder d:\backup -outputfolder z:\output -archivename BackupSet -days 30

Note1: The first thing you'll have to do is edit the script and update the path for 7zip. You'll of course need to have 7zip already installed (and powershell for that matter)

Note2: the -days command dictates how many days until a new full archive is created. It determines if a full archive is due by trying to find the last archive (using the archive name) in the -outputfolder location. If it can't find it or the last one is older than the -days then a full archive will be created, otherwise a diff will be created

Caveats: This script is provided 'as is' complete with bugs. There is no implied warranties or garuntees, use at your own risk. This script is unsigned so you'll either need to sign it or set your execution level to unrestricted. 7zip is also quite a cpu intensive task so ensure you schedule this accordingly.

Known Bugs

  1. Doesn't like spaces in paths
  2. If an archive turns out to be invalid then 7zip throws some sort of super exception that I can't get powershell to capture

 Key Learnings

  1. Functions in Powershell do not work like I expected as they do not return a value. Instead the values your parameters will capture is infact what is written to stdout. Yep that took me a while to work out!
  2. If you want to use functions, the code that calls that function must come after the function itself... another scripting quirk that would seem perfectly natural to scripters but not programmers.
  3. It was my original intention to learn powershell so i could use it in Operations Manager for agent actions. This may actually not turn out to be that useful because you need to firstly insure that the agent has powershell and powershell has significantly high impact upon resources.
  4. Scripts won't just run, you need to sign them or set your execution level to unrestricted set-executionpolicy unrestricted. Signing them is a convoluted process... maybe another blog idea?

Resources

  1. http://www.powergui.org/index.jspa - Powershell gui is an awesome free tool by quest for writing powershell
  2. http://sradack.blogspot.com/2007/05/compressing-sql-server-backups-with.html - Where I got the 7zip compression bits from
  3. http://www.wintellect.com/cs/blogs/jrobbins/archive/2008/01/05/new-year-s-resolution-powershell.aspx - He had a similar idea
  4. http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx - Microsofts powershell site - lots and lots of info.