locked
Question about the farm backup RRS feed

  • Question

  • Using the following command:

    Backup-SPFarm -Directory "D:\Backupfarm" -BackupMethod full

    I can see the folder spbr0002 created, within that there are several .bak files created. What is the difference between the above script and below script which creates.dat file.

    #
    # SharePoint 2010 Unleashed - PowerShell SharePoint backup script
    # http://www.amazon.com/Microsoft-SharePoint-2010-Unleashed-Michael/dp/0672333252
    # Copyright: Toni Frankola
    # Version: 1.1.0, Jul 2011.
    #
    # Source: http://www.sharepointusecases.com/?p=1597
    # Licensed under the MIT License:
    # http://www.opensource.org/licenses/mit-license.php
    #
    cls
    if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    	Add-PSSnapin Microsoft.SharePoint.PowerShell;
    }
    Function Backup-SPSiteCollections ()
    {
    	param(
    		[Parameter(
    			Position=0,
    			Mandatory=$true
    		)]
    		[Guid]$SPSiteID,
    		[Parameter(
    			Position=0,
    			Mandatory=$true
    		)]
    		[string]$BackupFolder,
    		[Parameter(
    			Position=0,
    			Mandatory=$true
    		)]
    		[string]$SiteName,
    		[Parameter(
    			Position=0,
    			Mandatory=$true
    		)]
    		[int]$BackupFilesLimit,
    		[Parameter(
    			Position=0,
    			Mandatory=$false
    		)]
    		[string]$Email = "",
    		[Parameter(
    			Position=0,
    			Mandatory=$false
    		)]
    		[string]$SmtpServer = ""
    	)
    	$siteNameSlug = $SiteName -replace "https://", ""
    	$siteNameSlug = $siteNameSlug -replace "http://", ""
    	$siteNameSlug = $siteNameSlug -replace ":", "-"
    	$siteNameSlug = $siteNameSlug -replace " ", "-"
    	$siteNameSlug = $siteNameSlug -replace "/", "-"
    	$siteNameSlug = $siteNameSlug -replace "\.", "-"
    	# Test if backup folder exists
    	if (Test-Path $BackupFolder)
    	{
    		# Retrive previous backup files , sorted by last write time (last modified)
        	$files = Get-Childitem $BackupFolder | where {$_.Name -like ("*" + $siteNameSlug + "*.dat")} | Sort $_.LastWriteTime
        	$filesCount = @($files).Count
    		# If there are more files in directory than backupFilesLimit
        	if($filesCount -ge $BackupFilesLimit)
        	{
    			# Delete all older files
            	for ( $i=0; $i -lt $filesCount-$BackupFilesLimit+1; $i++)
            	{
                	Remove-Item ($BackupFolder + $files[$i].Name)
            	}
        	}
    	}
    	# If backup folder does not exist it will be created
    	else
    	{
    		New-Item $BackupFolder -type directory
    	}
        $backupFileName = ("" + $siteNameSlug + "_" + (Get-Date -Format yyyy-MM-ddThh-mm-ss) + ".dat")
        $backupFilePath = $BackupFolder + $backupFileName
    	$startTime = Get-Date
        Backup-SPSite -identity $_.ID -path ($backupFilePath) -force
        $endTime = Get-Date
    	# Checking if Email and SmtpServer values have been defined
    	if($Email -ne "" -and $SmtpServer -ne "")
    	{
    		$subject = "SharePoint Site Collection Backup Completed!"
    		$body = "The following site collection was backuped: " + $SiteName + "`n"
    		$body += "Site collection was backuped to: " + $backupFileName + "`n"
    		$body += "Backup started on: " + $startTime + ", and ended on: " + $endTime + "`n`n"
    		# Retrieving Site Collection size
    		$SiteCollectionSize = Get-SPSite "http://team.devwin.imsa" | Select @{Label="Size"; Expression={$_.Usage.Storage/1MB}} | Select Size
    		# Retrieving backup file size
    		$backupFileSize = Get-ChildItem $backupFilePath
    		$backupFileSize = [Math]::Round($backupFileSize.length/1MB, 2)
    		$body += "Site collection size on SharePoint system is: " + [Math]::Round($SiteCollectionSize.Size, 2) + " MB`n"
    		$body += "Backup file size: " + $backupFileSize + " MB"
    		$smtp = new-object Net.Mail.SmtpClient($SmtpServer)
    		# Sending email
    		$smtp.Send($Email, $Email, $subject, $body)
    	}
    }
    # Backup all site collections in your farm
    Get-SPSite -Limit All | ForEach-Object {Backup-SPSiteCollections -SPSiteID $_.ID -BackupFolder "D:\Backupfarm\" -SiteName $_.Url -BackupFilesLimit 5 -Email "csaha@imsa.edu" -SmtpServer "mail.imsa.edu"}
    #Backup a site collection whose URL equals http://intranet.contoso.com
    Get-SPSite | Where {$_.Url -eq "http://team.devwin.imsa"} | ForEach-Object {Backup-SPSiteCollections -SPSiteID $_.ID -BackupFolder "D:\Backupfarm\" -SiteName $_.Url -BackupFilesLimit 5 -Email "csaha@imsa.edu" -SmtpServer "x.imsa.edu"}
    #Backup all site collections whose URL is not equal to http://no-backup.contoso.com, no emails will be sent
    Get-SPSite | where {$_.Url -ne "http://no-backup.contoso.com"} | ForEach-Object {Backup-SPSiteCollections -SPSiteID $_.ID -BackupFolder "D:\Backupfarm\" -SiteName $_.Url -BackupFilesLimit 5}
    # Backup site
    Backup-SPSite http://team.devwin.imsa/ -path "D:\Backupfarm\team.bak" -force


    • Edited by ChitraSaha Wednesday, July 18, 2012 8:37 PM
    Wednesday, July 18, 2012 8:36 PM

Answers

  • It's a difference in what the stock Backup-SPFarm cmdlet covers versus what Toni's script is doing via the Backup-SPSite cmdlet.

    Backup-SPFarm literally does exactly that: it backs up your entire farm. All of it's web applications, content databases, service applications (and their associated databases) such as Search and User Profiles, solutions deployed to the farm, its configuration database, and a bit more beyond just that.

    Backup-SPSite on its own backs up a single site collection. The thing to understand here is that site collections reside inside content databases, which are associated with web applications. Backup-SPSite does not cover any of the service applications that the site collection consumes, nor does it capture any information about the overall farm that the site collection is a part of.

    From a cursory read of it, it looks like Toni's script is set up to iterate through all of the content web applications in your farm, and then iterate through each site collection in each of those web apps and perform an individual backup using Backup-SPSite of each of those site collections. It looks like it also does some nifty trimming of older backup files if certain thresholds are crossed, which is definitely something that you won't see Backup-SPSite or Backup-SPFarm do on their own.

    If you want to see a PowerShell script that calls Backup-SPFarm rather than Backup-SPSite and does some similar kinds of storage trimming, I've got one in the TechNet Gallery you can take a look at as well: http://gallery.technet.microsoft.com/9b99c435-8831-4c9e-a70b-1f13158ef22a.

    John


    MCITP and MCTS: SharePoint, Virtualization, Project Server 2007
    My books on Amazon: The SharePoint 2010 Disaster Recovery Guide and The SharePoint 2007 Disaster Recovery Guide.
    My blog: My Central Admin.

    Wednesday, July 18, 2012 9:37 PM

All replies

  • It's a difference in what the stock Backup-SPFarm cmdlet covers versus what Toni's script is doing via the Backup-SPSite cmdlet.

    Backup-SPFarm literally does exactly that: it backs up your entire farm. All of it's web applications, content databases, service applications (and their associated databases) such as Search and User Profiles, solutions deployed to the farm, its configuration database, and a bit more beyond just that.

    Backup-SPSite on its own backs up a single site collection. The thing to understand here is that site collections reside inside content databases, which are associated with web applications. Backup-SPSite does not cover any of the service applications that the site collection consumes, nor does it capture any information about the overall farm that the site collection is a part of.

    From a cursory read of it, it looks like Toni's script is set up to iterate through all of the content web applications in your farm, and then iterate through each site collection in each of those web apps and perform an individual backup using Backup-SPSite of each of those site collections. It looks like it also does some nifty trimming of older backup files if certain thresholds are crossed, which is definitely something that you won't see Backup-SPSite or Backup-SPFarm do on their own.

    If you want to see a PowerShell script that calls Backup-SPFarm rather than Backup-SPSite and does some similar kinds of storage trimming, I've got one in the TechNet Gallery you can take a look at as well: http://gallery.technet.microsoft.com/9b99c435-8831-4c9e-a70b-1f13158ef22a.

    John


    MCITP and MCTS: SharePoint, Virtualization, Project Server 2007
    My books on Amazon: The SharePoint 2010 Disaster Recovery Guide and The SharePoint 2007 Disaster Recovery Guide.
    My blog: My Central Admin.

    Wednesday, July 18, 2012 9:37 PM
  • Hi John,

    I am working on your script, however can you get me started:

    I have the script in D:/Script/fullsharepointfarmbackupwithfilerotation.ps1. The backup folder is in D:/Backupfarm. In the following statement, are we trying to do as below. Do I have to make any more changes to the #requires -version 2  script.

    param([string]$drive = $(Throw "D:\"), `
          [string]$backupDir = $(Throw "D:\Backupfarm"), `
         [string]$fullBackupDay = $(Throw "Sunday"), `
          #[string]$logStorage, `
          [string]$configDBInstance, `
          [string]$configDBName, `
      [string]$nbrDaysRetained = $(Throw "14"), `
          #[string]$sharePointVersion = $(throw "2010"), `
          [string]$computer)
    # END Parameters

    Thursday, July 19, 2012 5:40 PM
  • I don't think we need to change anything from the original script. What we need to do is call the script with parameter. For example:

    .\BackupSharePoint.ps1 -drive "C:" -backupDir "\\server\backups" 
        -fullBackupDay "Sunday" -configDBInstance sql\sql01 -configDBName ConfigDB 
        -nbrDaysRetained 14 


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)

    Friday, July 20, 2012 9:38 AM