Wednesday, October 6, 2010

PowerShell: Backup Script

I hooked up a quick script to backup important items from a PC.  It’s is a quick and dirty solution that I may revisit sometime in the future with enhancements. 

  1: # http://geekswithblogs.net/Lance/archive/2009/12/29/program-files-environment-variable-in-powershell.aspx
  2: # http://itknowledgeexchange.techtarget.com/powershell/deleting-files-older-than-a-certain-date/
  3: 
  4: $Today = (Get-Date -format "yyyy-MM-dd")
  5: $targetFolder = "\\BackupServer\Share"
  6: $backupDir = "$targetFolder\$env:username\$Today"
  7: 
  8: function Remove-Items
  9: { 
 10:     $testFolderPath = $targetFolder
 11: 
 12:     if (Test-Path -Path $testFolderPath)
 13:     { 
 14:         $fiveDaysAgo = (Get-Date).AddDays(-5)        
 15:         Get-ChildItem -Path $testFolderPath -Recurse | Where-Object { $_.CreationTime -lt $fiveDaysAgo } | Remove-Item -recurse -force
 16:     }
 17:     else 
 18:     {
 19:         Write-Host "$testFolderPath does not exist."
 20:         #Kill Script
 21:         Exit
 22:     }
 23: }
 24: 
 25: function is64bit()
 26: {
 27:   return ([IntPtr]::Size -eq 8)
 28: }
 29: 
 30: function Get-ProgramFilesDir()
 31: {
 32:   if (is64bit -eq $true) {
 33:     (Get-Item "Env:ProgramFiles(x86)").Value
 34:   }
 35:   else {
 36:     (Get-Item "Env:ProgramFiles").Value
 37:   }
 38: }
 39: 
 40: function Create-Folders 
 41: { 
 42:     if (Test-Path -Path $backupDir) 
 43:     { 
 44:         #do nothing. Folders have been setup already
 45:     }
 46:     else 
 47:     {
 48:         md "$backupDir" -force
 49:     }
 50: }
 51: 
 52: # Remove Folders older than $fivedaysago
 53: Remove-Items
 54: 
 55: # Kills Outlook in preparation for backup
 56: if((get-process "OUTLOOK" -ea SilentlyContinue) -eq $Null){ }else{ Stop-Process -processname Outlook }
 57: 
 58: # Start backup operation
 59: Create-Folders
 60: 
 61: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:appdata\Microsoft\Outlook" "$backupDir\OutlookAppdata" /LOG+:"$backupDir\backup.log"
 62: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\AppData\Local\Microsoft\Outlook" "$backupDir\Outlook" /LOG+:"$backupDir\backup.log"
 63: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Documents" "$backupDir\Docs" /LOG+:"$backupDir\backup.log"
 64: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Favorites" "$backupDir\Favorites" /LOG+:"$backupDir\backup.log"
 65: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Desktop" "$backupDir\Desktop" /LOG+:"$backupDir\backup.log"
 66: 
 67: #Start Outlook
 68: $programfilespath = Get-ProgramFilesDir
 69: 
 70: if (Test-Path -Path "$programfilespath\Microsoft Office\Office12\Outlook.exe") 
 71: {
 72:     Start-Process "$programfilespath\Microsoft Office\Office12\Outlook.exe"
 73: }
 74: else
 75: {
 76:     Start-Process "$programfilespath\Microsoft Office\Office11\Outlook.exe"
 77: }





Please note: This script is designed for Vista and above.  It uses Robocopy to backup.  You can get Robocopy on XP by installing the Resource Kit Tools.  I’ve only tested this on Vista and above though.  I’m also using version 2 of PowerShell.

No comments: