Showing posts with label Backup. Show all posts
Showing posts with label Backup. Show all posts

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.

Wednesday, September 22, 2010

RoboCopy vs. SyncToy

While this is not really an in depth comparison, this is more of a quick observation…  If you are looking for a backup solution, please, PLEASE pick one that can actually call itself a backup solution.  Here’s where SyncToy falls short:
  1. Copy files in Restartable mode ~ This allows for an intermittent network connection and will pick up where the copy operation left off.  This is great for those fiesty T1’s that love to chew up more time than they are worth.
  2. Copy Access Rights ~ So if you have intricate permissions set on your files (Who doesn’t ;-P), then you will miss out on all of this glory.
  3. Monitor Source ~ WOW… What a capability.  Robocopy can monitor your files and when there are changes, it’ll automatically copy them.
  4. Bandwidth Throttling ~ While this isn’t a complete solution, it still can free up some bits for other stuff that’s traveling down that slow T1.
  5. Multi-Threaded ~ Why am I still going?  This allows for multiple files to be handled at once.  Now you don’t have to wait on that 6 GB .pst to copy over before your 32 KB files are handled.
  6. Attribute Handling ~ This is not the most impressive, but it’s actually my favorite.  If you know anything about backups, then you know setting the Archive attribute on files can save precious time and space on your backup routine.
  7. Can be easily automated ~ Now before you ping my comment section, I’m very aware that you can schedule a task to run SyncToy.  My main problem with that is I have to setup my pairs via GUI only.  The help files say otherwise, but I’m not convinced.  I’ve spent at least 30 mins trying to prove myself wrong…  This doesn’t work: SyncToy -d(left=e:\,right=c:\Pictures, name=MyPictures,operation=contribute) and this is in the help file!!!
What is SyncToy good for?
Practical uses?  Set it up for your grandma and wait for her to call you when she wants to do a simple backup.  But you then also have to consider what is being backed up.  There are more caveats that you may not know about.  Here’s an excerpt from SyncToy’s Forum:
Q. I have noticed that sometimes SyncToy does not pick up changes to my photos which I had edited using "some popular" photo editing and management software?
A. Several popular photo-editing suites do NOT modify the file time or file size when making certain changes to pictures such as when the user adds or edits tags, description, title, etc on the picture. This is especially true of file formats such as JPEG which use the EXIF header structure. Because of this SyncToy has no way of knowing that the file was modified by the user - if either the last updated timestamp on the file changes or the file size changes, SyncToy will pick up that change and synchronize it over to the other side - otherwise it looks like the same file as before to SyncToy. We're considering improving this behavior in later versions of the Microsoft Sync Framework which is the synchronization technology underneath SyncToy.  For now, the only options users have when synchronizing pictures that may exhibit this behavior is to turn on the Check File Contents option on their folder pair - however, it will make the sync operation quite slow because for this SyncToy has to do a complete scan of every file on each side to compute a signature for the file contents.
RoboCopy is your friend…
This is really a short list that could go on forever.  It may sound a bit biased, but based on what I’ve seen, SyncToy should never be used as for backup.  It may seem like such an easy piece of software to work with, in the end do not be fooled. 
What do you think?
Technorati Tags: ,,

Tuesday, September 21, 2010

The Almighty Content Database

I ran into a major issue on my dev box the other night.  It was such a weird issue, I ran for the hills and waited for it to fix itself.  That almost never works, so I dug in.  What’s a bit of work without getting your hands dirty, eh?

The issue with my box was an update.  I’m almost 99% sure it was an update, but I couldn’t pin it on one and since it’s a box running in Hyper-V, I decided to take the easy route.  I constantly make backups of my content database.  That is key, without these precious backups, I would have lost all of my work.  Since I take snapshots of this server regularly, I figured the most effective time-saver was to use my latest snapshot.  It was only a week old and I know I wasn’t suffering from my problem at that point.

The recovery went well and SharePoint snapped right open.  So far, so good.  I’ve never had to recover a full content database before, so this was new territory for me.  Not to worry, I have good backups.  A quick google search landed me on Stefan Keir Gordon’s blog.  This specific post talks about moving the DB to a new server, it was exactly what I needed.  I tweaked it just a bit by taking the Content DB offline first.  After a reboot, I ran a Recover Database operation.  You just simply point to the .bak from your backups and then pick the Content DB.  This all took about 10 mins (My site is rather small). 

What Have I Learned?

That’s what it’s all about right?  Before this issue, I have been manually running them every week or so.  Dropping off the bits to my file server here which is on a RAID 5.  Plenty enough redundancy for my tiny operation…  Now I’ve realized, the weeks’ worth of work that I lost was a big loss to me.  Because of that, I now have my backups scheduled to run every day.  It’s not talked about enough in my book: We all should review backup strategies every 3 mos. or so and re-evaluate what’s mission critical and what isn’t.  It can literally save lots of time and money.