Lessons Learned

Various postings based on things I learned the hard way.

Simple Sharepoint 2010 farm backup

Simple Sharepoint 2010 farm backup

  • Comments 4
  • Likes

Problem:

I recently setup a new SharePoint environment to host a single site collection. Even though there was not a lot of content on this small farm, the environment needed to be highly available and we needed the ability to lose no more than one hour worth of data in the case of a catastrophic failure. Retaining backups for one week was plenty for our needs. Users of the site were located globally making the site's least utilized time in the evenings (PST) with the biggest window on Saturday.

Based on this and the idea of keeping the process easy and lightweight as possible (i.e. out-of-the-SharePoint-box and running from a single location) I decided to go this route:

  1. Full farm backup on Saturday afternoon
  2. Differential farm backup on Sunday-Friday evenings
  3. Hourly differential backups on content databases
  4. Clean out last weeks Full and Differential backups on early Monday morning

Problem was I didn't know how I was going to accomplish this and than fully automate it. Breaking down this problem I determined I had three steps to make this work:

  1. Run backups via PowerShell
  2. Run these PowerShell scripts from Task Scheduler
  3. Cleanup backups

 

Solution:

  1. Run backups via PowerShell
    • Using the TechNet documentation for reference I used the following one-liner for Full and Differential farm backup:
              Backup-SPFarm -Directory <backupFolder> -BackupMethod {Full | Differential} [-Verbose]

    • This is what I used for the hourly content db backups:
              Backup-SPFarm -Directory <backupFolder> -BackupMethod Differential -Item <contentDBName>

    • I took these commands and placed them into individual .ps1 files (FullFarmBackup.ps1, DiffFarmBackup.ps1 and HourlyContentDBBackup.ps1) and put them into E:\backupScripts.

  2. Run these PowerShell backups from Task Scheduler
        Getting Task Scheduler to run a PowerShell script with the SharePoint snap-in was a lesson in trial and error. Ultimately this is what I did to get it working.
    • In all my .ps1 scripts I need to have the SharePoint snap-in loaded. To do this I added the live "Add-PSSnapin Microsoft.SharePoint.PowerShell" at the top.

          Add-PSSnapin Microsoft.SharePoint.PowerShell
          Backup-SPFarm -Directory <backupFolder> -BackupMethod {Full | Differential} [-Verbose]
    • Under the Action tab of the task I created:
      • Program/Script = powershell
      • Add Argument (optional) = E:\backupScripts\FullFarmBackup.ps1



  3. Automate the cleanup of the above backups
       
    Because I didn't have infinite disk space and didn't need backups greater than 1 week I needed a means to cleanup old backups. Running a quick Bing search I ran across a blog post on 'Imperfect IT' that did just this. Here is a copy of what was posted on that site and to which I placed into Task Scheduler:

        # Location of spbrtoc.xml
        $spbrtoc = "<backupFolder>"

        # Days of backup that will be remaining after backup cleanup.
        $days = 7

        # Import the Sharepoint backup report xml file
        [xml]$sp = gc $spbrtoc

        # Find the old backups in spbrtoc.xml
        $old = $sp.SPBackupRestoreHistory.SPHistoryObject |
        ? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
        if ($old -eq $Null) { write-host "No reports of backups older than $days days found in spbrtoc.xml.'nspbrtoc.xml isn't changed and no files are removed.'n" ; break}

        # Delete the old backups from the Sharepoint backup report xml file
        $old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }

        # Delete the physical folders in which the old backups were located
        $old | % { Remove-Item $_.SPBackupDirectory -Recurse }

        # Save the new Sharepoint backup report xml file
        $sp.Save($spbrtoc)
        Write-host "Backup(s) entries older than $days days are removed from spbrtoc.xml and harddisc."

I now have 4 scheduled tasks running (Full farm, Diff farm, Hourly content db and cleanup) that provide me the ability to fulfill our simple backup strategy.

 

Comments
  • thanks. Great post.

  • When i run the clean.bat, i get the following error:

    Get-Content : Access to the path 'd:\Backup\Farm' is denied.

    At C:\Scripts\CleanFarmBackup.ps1:8 char:12

    + [xml]$sp = gc $spbrtoc

    + ~~~~~~~~~~~

    + CategoryInfo : PermissionDenied: (D:\Backup\Farm:String) [Get-Content], UnauthorizedAccessException

    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

    Any ideas how to solve this?

  • "get-childitem D:\Backup\Farm -include *.xml" is, what you're looking for.

  • I think, there is an logical error ... in the delete script, you delete all (Full and Diff) Backups older 7 days. But if you delete the Full Backup, which still has differential Backups as "Children", a restore isn't possible.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment