PoshCode Logo PowerShell Code Repository

Farm Backup by shilezi 21 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3998"></script>download | new post

Site Collection by Site Collection backup of SharePoint 2010 Farm. Script is as is and use at your discretion. I take no credit for the script, other than modifying it for my purposes. I took bits of scripts from 2 sites http://gallery.technet.microsoft.com/scriptcenter/Backup-all-collections-in-726514cb and the other i cannot remember.

  1. ##################################
  2. #This errors out in ISE, so use cmd window.
  3. #The script below fixes the error just below.
  4. #Cannot access the local farm. Verify that the local farm is
  5. #properly configured, currently available, and that you have
  6. #the appropriate permissions to access the database before
  7. #trying again.
  8.  
  9. #Enable-PSRemoting -Force
  10. #Enable-WSManCredSSP -Role Server -Force
  11.  
  12. ##################################
  13.  
  14.  
  15. Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue  
  16.  
  17.  try
  18.  {
  19.  
  20.    
  21. # specify here your backup folder
  22. $backupRoot = "\\SERVER\SPFarmBackUP\"
  23. $logPath = Join-Path $backupRoot "_logs"
  24. $tmpPath = Join-Path $backupRoot "_tmp"
  25. # removes all the old backup files on the target folder (valid values: 0 = do not remove; 1 = remove files
  26. $clearUpOldFiles = 0  
  27. # specifies the days for the backups that should be persisted
  28. $removeFilesOlderThanDays = -1
  29. # specifies the backupfolder based on the current weekday (Monday... etc.)
  30. $todaysBackupFolder = Join-Path $backupRoot ((Get-Date).DayOfWeek.toString())  
  31.  
  32. # generate all necessary folders if they are missing
  33. if (-not (Test-Path $logPath)) {  
  34.   New-Item $logPath -type directory
  35. }  
  36.  
  37. if (-not (Test-Path $tmpPath)) {  
  38.   New-Item $tmpPath -type directory
  39. }  
  40.  
  41. if (-not (Test-Path $todaysBackupFolder)) {  
  42.   New-Item $todaysBackupFolder -type directory
  43. }  
  44.  
  45. # creates a log file  
  46. Start-Transcript -Path (Join-Path $logPath ((Get-Date).ToString('MMddyyyy_hhmmss') + ".log"))
  47.  
  48. $TXTFile = Join-Path $logPath ((Get-Date).ToString('MMddyyyy_hhmmss') + ".log")
  49.  
  50. # loop over all web applications (specify filter criteria here if you want to filter them out)
  51. foreach ($webApplication in Get-SPWebApplication) {    
  52.   Write-Host    
  53.   Write-Host    
  54.   Write-Host "Processing $webApplication"    
  55.   Write-Host "*******************************"          
  56.  
  57.   foreach ($site in $webApplication.Sites) {                
  58.     # we have to replace some characters from the url name        
  59.     $name = $site.Url.Replace("http://", "").Replace("https://", "").Replace("/", "_").Replace(".", "_");        
  60.     # replace all special characters from url with underscores        
  61.     [System.Text.RegularExpressions.Regex]::Replace($name,"[^1-9a-zA-Z_]","_");                  
  62.     # define the backup name        
  63.     $backupPath = Join-Path $tmpPath ($name + (Get-Date).ToString('MMddyyyy_hhmmss') + ".bak")                  
  64.  
  65.     Write-Host "Backing up $site to $backupPath"        
  66.     Write-Host                  
  67.      
  68.     # backup the site        
  69.     Backup-SPSite -Identity $site.Url -Path $backupPath    
  70.   }          
  71.  
  72.   Write-Host "*******************************"    
  73.   Write-Host "*******************************"
  74. }  
  75.  
  76. Write-Host
  77. Write-Host  
  78.  
  79. # remove the old backup files in the todays folder if specified
  80. if ($clearUpOldFiles -eq 1) {  
  81.   Write-Host "Cleaning up the folder $todaysBackupFolder"  
  82.   Remove-Item ($todaysBackupFolder + "\*")  
  83. }  
  84.  
  85. # move all backup files from the tmp folder to the target folder
  86. Write-Host "Moving backups from $tmpPath to $todaysBackupFolder"
  87. Move-Item -Path ($tmpPath + "\*") -Destination $todaysBackupFolder
  88. # you can specify an additial parameter that removes filders older than the days you specified
  89. if ($removeFilesOlderThanDays -gt 0) {  
  90.   Write-Host "Checking removal policy on $todaysBackupFolder"  
  91.   $toRemove = (Get-Date).AddDays(-$removeFilesOlderThanDays)  
  92.   $filesToRemove = Get-ChildItem $todaysBackupFolder | Where {$_.LastWriteTime -le$toRemove}      
  93.  
  94.   foreach ($fileToRemove in $filesToRemove)  {    
  95.     Write-Host "Removing the file $fileToRemove because it is older than $removeFilesOlderThanDays days"      
  96.     Remove-Item (Join-Path $todaysBackupFolder $fileToRemove) | out-null  
  97.   }
  98. }
  99.  
  100. Stop-Transcript
  101.  
  102.    
  103.   $today = (Get-Date -Format MM-dd-yyyy)
  104.  # Edit the From Address as per your environment.
  105.   $emailFrom = "admin@domain.com"
  106.  # Edit the mail address to which the Notification should be sent.
  107.   $emailTo = "admin@domain.com"
  108.  # Subject for the notification email. The + “$today” part will add the date in the subject.
  109.   $subject = "Site collections on Farm Backup Report for this day "+"$today"
  110.  # Body or the notification email. The + “$today” part will add the date in the subject.
  111.   $body = "The SharePoint backup of all Site collections in the farm has been run and was successful this day "+"$today"
  112.   # IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server
  113.   $smtpServer = "smtp.domain.com"
  114.   $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  115.   Send-MailMessage -SmtpServer $smtpServer -From $emailFrom -To $emailTo -Subject $subject -Body $body -Attachment $TXTFile
  116.  
  117.  
  118. }
  119.  
  120. catch
  121. {
  122.   [System.Exception]
  123.   $ErrorMessage = $_.Exception.Message
  124.   # Configure the below parameters as per the above.
  125.   $emailFrom = "admin@domain.com"
  126.   $emailTo = "admin@domain.com"
  127.   $subject = "Site collections on Farm Backup Report for this day "+"$today"
  128.   $body = "The SharePoint backup of all Site collections in the farm has been run and the Job failed today, the "+"$today because... $ErrorMessage."
  129.   $smtpServer = "smtp.domain.com"
  130.   $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  131.   Send-MailMessage -SmtpServer $smtpServer -From $emailFrom -To $emailTo -Subject $subject -Body $body
  132. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me