Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Synopsis
- Automated backup of the SharePoint Farm.
- .Description
- Automates the backup of the SharePoint Farm backing up the Farm, Farm Config, Service Applications, and individual Site Collections.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Days
- Global. Number of days to maintain all backups. Over-ridden by ConfigDays, FarmDays, SitesDays, & SADays for their respective backups If they exist. Default: 7.
- .Parameter From ("Sender")
- Global. Sender email address for all email notifications.
- .Parameter To ("Recipients")
- Global. Recipient(s) email address(es) for all email notifications.
- .Parameter SMTPHost ("SMTP")
- Global. SMTP hostname or IP address.
- .Parameter SubjectPrefix ("Subject")
- Global. Subject prefix of the email subject. Default: "[SP2013 Backup] "
- .Parameter SPConfigFolderName ("SPConfigFolder")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
- .Parameter ConfigDays
- Placeholder. This is currently not in use. Number of days to maintain Config backups. Default (from Days): 7.
- .Parameter FarmFolderName ("FarmFolder")
- Folder name of the Farm backups to be appended to the Root Path. Default: "Farm".
- .Parameter FarmDays
- Number of days to maintain Farm backups. Default (from Days): 7.
- .Parameter SPSitesFolderName ("SPSitesFolder")
- Folder name of the Site Collection backups to be appended to the Root Path. Default: "Sites".
- .Parameter SitesDays
- Number of days to maintain Site Collection backups. Default (from Days): 7.
- .Parameter SAFolderName ("SAFolder")
- Folder name of the Service Application backups to be appended to the Root Path. Default: "ServiceApplications".
- .Parameter SADays
- Placeholder. This is currently not in use. Number of days to maintain Service Application backups. Default (from Days): 7.
- .Example
- Backup-SP2013 -Path "\\viningsparks.local\common\APPDEV\Sharepoint\backups"
- -Days 30
- -From "no-reply@viningspars.com"
- -To "tsmith@ivision.com"
- -SMTP "relay.viningsparks.local"
- .Notes
- Name: Backup-SP2013
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Backup-SP2013 {
- [CmdletBinding()]
- param(
- ## GLOBAL PARAMETERS ##
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("RootPath")]
- [System.String]
- $Path,
- # Over-ridden by FarmDays & SitesDays If they exist
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.Collections.ArrayList]
- $From,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Subject")]
- [System.String]
- $SubjectPrefix = "[SP2013 Backup] ",
- ## SPECIFIC PARAMETERS ##
- # Folder Names
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SPConfigFolder")]
- [System.String]
- $SPConfigFolderName = "Config",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $ConfigDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("FarmFolder")]
- [System.String]
- $FarmFolderName = "Farm",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $FarmDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SPSitesFolder")]
- [System.String]
- $SPSitesFolderName = "Sites",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $SitesDays,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SAFolder")]
- [System.String]
- $SAFolderName = "ServiceApplications",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $SADays
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "Root" "Starting" "Beginning backups..."
- # Create the Root Path
- Create-SP2013BackupDirectory $Path
- # Set the Sites/Farm Days Retention
- If (!$ConfigDays) {
- $ConfigDays = $Days
- }
- Write-SP2013Verbose "Root" "Starting" "Setting the Farm Backups Retention to $ConfigDays Days"
- If (!$FarmDays) {
- $FarmDays = $Days
- }
- Write-SP2013Verbose "Root" "Starting" "Setting the Farm Backups Retention to $FarmDays Days"
- If (!$SitesDays) {
- $SitesDays = $Days
- }
- Write-SP2013Verbose "Root" "Starting" "Setting the Site Collection Backups Retention to $FarmDays Days"
- If (!$SADays) {
- $SADays = $Days
- }
- Write-SP2013Verbose "Root" "Starting" "Setting the Service Application Backups Retention to $FarmDays Days"
- # Set Path Global Variables
- Set-Variable -Name "SP2013Path" -Value $Path -Scope Global
- # Set Email Global Variables
- Set-SP2013EmailParameters $From $To $SMTPHost $SubjectPrefix
- }
- PROCESS {
- # Process Config Backup
- Backup-SP2013Config -Path $Path -Name $SPConfigFolderName -Days $ConfigDays -Verbose:$Verbose
- # Process Sites Backup
- Backup-SP2013Sites -Path $Path -Name $SPSitesFolderName -Days $SitesDays -Verbose:$Verbose
- # Process Farm Backup
- Backup-SP2013Farm -Path $FarmFolderName -Name $SPSitesFolderName -Days $FarmDays -Verbose:$Verbose
- # Process Service Applications Backup
- Backup-SP2013ServiceApplications -Path $FarmFolderName -Name $SAFolderName -Days $SADays -Verbose:$Verbose
- }
- END {
- Write-SP2013Verbose "Root" "Ending" "Backups Complete"
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Config".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2013Config -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Backup-SP2013
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Backup-SP2013Config {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Config",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "Config" "Starting" "Beginning SP Config Backup"
- # Set SP Config Backup Directory Path
- If ($Name)
- {
- $ConfigPath = $Path.Trimend('\') + "\$Name"
- }
- else
- {
- $ConfigPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2013BackupProcess -Path $ConfigPath -Name "Config" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter BackupMethod
- Method of Backup. Default: "Full".
- .Parameter OnError
- Error Action over-ride for ErrorActionPreference. Default: "Stop".
- .Parameter Percentage
- Percentage to expect a message. Default: 15.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2013Config -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Invoke-SP2013BackupProcess
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Invoke-SP2013BackupProcess {
- [CmdletBinding()]
- param(
- # Expects Root Path
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Method")]
- [System.String]
- $BackupMethod = "Full",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $OnError = "Stop",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Percent")]
- [System.String]
- $Percentage = 15,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose $Namespace "Processing" "Starting Backup"
- Create-SP2013BackupDirectory $Path
- $BackupPath = Get-SP2013Directory $Path
- Write-SP2013Verbose $Namespace "Processing" "Backing up SP2013 Farm to $BackupPath"
- }
- PROCESS {
- # Perform new backup
- Invoke-SP2013Backup -Path $BackupPath -Name $Namespace -SendEmail:$SendEmail -Verbose:$Verbose
- # Clean up Old Backups
- Remove-SP2013OldBackups -Path $BackupPath -Name $Namespace -Days $Days -Verbose:$Verbose
- # Check for Backup Completion!
- Watch-2013BackupStatus -Path $Path -Name $Namespace -Verbose:$Verbose
- Write-SP2013Verbose $Namespace "Processing" "Monitoring Complete!" -ForegroundColor "Green"
- }
- END {
- If ($SendEmail -and $SP2013From -and $SP2013To -and $SP2013SMTPHost)
- {
- # The backup status is saved in the last 4 log lines from the file. Save that information off
- $eBody += (Get-SP2013BackupLogFile $Path -Contents)[-2 .. -4]
- # Send Email Status
- Write-SP2013Verbose $Namespace "Emailing" "Sending the email!"
- Send-SP2013BackupEmail -From $SP2013From -To $SP2013To -Subject $eSubject -Body $eBody -SMTP $SP2013SMTPHost -AttachmentName (Get-SP2013BackupLogFile $Path) -Versbose:$Verbose
- }
- Else
- {
- Write-SP2013Verbose $Namespace "Emailing" "Cannot send email. Please run Set-SP2013EmailParameters." -Error
- }
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Farm.
- .Description
- Automates the backup of the SharePoint Farm.
- .Parameter Path ("RootPath")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2013Farm -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2013Farm
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Backup-SP2013Farm {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Farm",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "Farm" "Starting" "Beginning SP Farm Backup Beginning..."
- # Set SP Farm Backup Directory Path
- If ($Name) {
- $FarmPath = $Path.Trimend('\') + "\$Name"
- }
- Else
- {
- $FarmPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2013BackupProcess -Path $FarmPath -Name "Farm" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Service Applications.
- .Description
- Automates the backup of the SharePoint Service Applications.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2013ServiceApplications -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2013ServiceApplications
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Backup-SP2013ServiceApplications {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "ServiceApplications",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "ServiceApplications" "Starting" "Beginning SP Service Applications Backup"
- # Set SP Service Applications Backup Directory Path
- If ($Name)
- {
- $SAPath = $Path.Trimend('\') + "\$Name"
- }
- else
- {
- $SAPath = $Path
- }
- }
- PROCESS {
- Invoke-SP2013BackupProcess -Path $SAPath -Name "ServiceApplications" -Days $Days -SendEmail:$SendEmail -Verbose:$Verbose
- }
- }
- <#
- .Synopsis
- Automated backup of the SharePoint Service Applications.
- .Description
- Automates the backup of the SharePoint Service Applications.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Name ("DirectoryName")
- Folder name of the SP Config backups to be appended to the Root Path. Default: "Farm".
- .Parameter Sites
- Array of sites to be backed up. Default: Get-SPSite -Limit All.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Backup-SP2013Sites -Path "\\servername\path\to\backups\root" -SendEmail
- .Notes
- Name: Backup-SP2013Sites
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Backup-SP2013Sites {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name = "Sites",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Collections.ArrayList]
- $Sites,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "Sites" "Starting" "Backing up SP2013 Site Collections"
- # Get sites
- If (!$Sites) {
- Write-SP2013Verbose "Sites" "Starting" "Getting ALL sites!"
- $Sites = Get-SPSite -Limit All
- }
- Write-SP2013Verbose "Sites" "Starting" "Sorting sites from largest to smallest by DiskSizeRequired"
- $Sites = $Sites | Sort-Object ContentDatabase.DiskSizeRequired
- If ($Name) {
- $SitesPath = $Path.Trimend('\') + "\$Name"
- }
- Create-SP2013BackupDirectory $SitesPath
- $BackupPath = Get-SP2013Directory $SitesPath
- Write-SP2013Verbose "Sites" "Starting" "Backing up SP2013 Farm to $SitesPath"
- }
- PROCESS {
- foreach ($site in $Sites)
- {
- $BackupPath = $site.PrimaryUri.Host
- If ($site.PrimaryUri.Segments.Length -gt 1)
- {
- $BackupPath += "." + $site.PrimaryUri.Segments[1].TrimEnd("/")
- If ($site.PrimaryUri.Segments.Length -gt 2)
- {
- $BackupPath += "." + $site.PrimaryUri.Segments[2]
- }
- }
- Write-SP2013Verbose "Sites" "Processing" "Backing up $($site.Url) to $BackupPath\$BackupPath.bak"
- # Take the site backup without locking the site (-NoSiteLock) and using SQL snapshot
- Backup-SPSite -Identity $site.id -Path "$BackupPath\$BackupPath.bak" -NoSiteLock -UseSqlSnapshot -Verbose:$Verbose
- }
- }
- END {
- Write-SP2013Verbose "Sites" "Ending" "Cleaning old Site Collection Backups"
- Remove-SP2013OldSites -Path $SitesPath - Days $Days
- }
- }
- <#
- .Synopsis
- Automated cleanup of the SharePoint Site Collections.
- .Description
- Automated cleanup of the SharePoint Site Collections.
- .Parameter Path ("Directory")
- The path to the Sites backups directory (e.g. \\servername\path\to\backups\root\Sites).
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Example
- Remove-SP2013OldSites -Path "\\servername\path\to\backups\root\Sites" -SendEmail
- .Notes
- Name: Remove-SP2013OldSites
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Remove-SP2013OldSites {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7
- )
- BEGIN {
- Write-SP2013Verbose "Sites - Clean" "Starting" "Cleaning up SP2013 Site Collection Backups"
- # Get/Set path
- If (!$Path -and $SP2013Path)
- {
- $Path = $SP2013Path
- }
- }
- PROCESS {
- # Remove old backup directories
- $old = gci $Path | ? { $_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) }
- # Check If old directories exist
- If ($old -eq $null)
- {
- # Do Nothing
- break
- }
- # Remove Old Directories
- If ($Verbose)
- {
- $old | % { Write-SP2013Verbose "Sites - Clean" "Processing" "Removing " + $_.FullName }
- }
- $old | % { Remove-Item $_.FullName -Recurse }
- }
- }
- <#
- .Synopsis
- Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications).
- .Description
- Automated cleanup of the SharePoint Backups (Farm, Config, & Service Applications; NOT Sites).
- .Parameter Path ("Directory")
- The full path to the specific backups directory (e.g. \\servername\path\to\backups\root\Farm\{date.time}).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter Days
- Number of days to maintain all backups. Default: 7.
- .Example
- Remove-SP2013OldBackups -Path "\\servername\path\to\backups\root\Farm\{date.time}" -Name "Farm"
- .Notes
- Name: Remove-SP2013OldBackups
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Remove-SP2013OldBackups {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.Int32]
- $Days = 7
- )
- BEGIN {
- Write-SP2013Verbose $Namespace "Cleaning" "Beginning to clean up old Farm backups"
- If (!$SPBRTOC)
- {
- $SPBRTOC = "$Path\spbrtoc.xml"
- }
- }
- PROCESS {
- # Import the SharePoint backup report xml file
- Write-SP2013Verbose $Namespace "Cleaning" "Importing the SharePoint Backup Report XML File"
- [xml]$sp = gc $SPBRTOC
- # Find the old backups in spbrtoc.xml
- Write-SP2013Verbose $Namespace "Cleaning" "Finding Old backups"
- $old = $sp.SPBackupRestoreHistory.SPHistoryObject | ? { $_.SPStartTime -lt ((Get-Date).adddays(-$Days)) }
- If ($old -ne $null)
- {
- If ($Verbose)
- {
- $old | % { Write-SP2013Verbose $Namespace "Cleaning" "Deleting" + $_.SPBackupDirectory }
- }
- # 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
- Write-SP2013Verbose $Namespace "Cleaning" "Saving the SharePoint Backup Report XML File"
- $sp.Save($SPBRTOC)
- }
- }
- }
- # REGION: Helper Functions
- <#
- .Synopsis
- Automated backup of the SharePoint Farm Config.
- .Description
- Automates the backup of the SharePoint Farm Config.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Parameter BackupMethod
- Method of Backup. Default: "Full".
- .Parameter OnError
- Error Action over-ride for ErrorActionPreference. Default: "Stop".
- .Parameter Percentage
- Percentage to expect a message. Default: 15.
- .Parameter SendEmail ("Email")
- Switch whether to send an email or not. Default: $false
- .Example
- Invoke-SP2013Backup -Path "\\servername\path\to\backups\root"
- .Notes
- Name: Invoke-SP2013Backup
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Invoke-SP2013Backup {
- [CmdletBinding()]
- param(
- # Expects full Backup Path
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Method")]
- [System.String]
- $BackupMethod = "Full",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $OnError = "Stop",
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Percent")]
- [System.String]
- $Percentage = 15,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Email")]
- [Switch]
- $SendEmail
- )
- PROCESS {
- # Perform the backup
- Try
- {
- Write-SP2013Verbose $Namespace "Processing" "Beginning backup..."
- # Run a new backup
- Switch ($Namespace)
- {
- "Config" {
- Write-Verbose "Doing Config Backup"
- # Run a new full configuration-only backup
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ConfigurationOnly -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- "Farm" {
- Write-Verbose "Doing Farm Backup"
- # Run a new full farm backup
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- "ServiceApplications" {
- Write-Verbose "Doing SA Backup"
- $Path
- $BackupMethod
- $OnError
- $Percentage
- Backup-SPFarm -Directory $Path -BackupMethod $BackupMethod -Item "Farm\Shared Services" -ErrorAction $OnError -Verbose:$Verbose -Percentage $Percentage
- }
- }
- }
- Catch [system.exception] # check for exceptions
- {
- Write-SP2013Verbose $Namespace "Processing" "Backup Failed!" -Error
- Write-SP2013Verbose $Namespace "Processing" $_.Exception.Message -Error
- If ($SendEmail)
- {
- # save off the exception message
- $eBody = $_.Exception.Message
- # new email subject
- $eSubject += "Backup Failed"
- # send an email containing the backup failure
- Send-SP2013BackupEmail $From $To $eSubject $eBody $SMTPHost -Versbose:$Verbose
- }
- # halt the script so we preserve older backups
- break
- }
- }
- }
- <#
- .Synopsis
- Creates the Backup Directory.
- .Description
- Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Example
- Create-SP2013BackupDirectory -Path "\\servername\path\to\backups\root" -Name "Config"
- Create-SP2013BackupDirectory -Path "\\servername\path\to\backups\root\Config"
- .Notes
- Name: Create-SP2013BackupDirectory
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Create-SP2013BackupDirectory {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("DirectoryName")]
- [System.String]
- $Name
- )
- BEGIN {
- $today = Get-SP2013Date
- }
- PROCESS {
- If ($Name)
- {
- Write-Verbose "Creating Directory $Path\$Name\$today"
- New-Item $Path\$Name\$today -Type directory
- }
- Else
- {
- Write-Verbose "Creating Directory $Path\$today"
- New-Item $Path\$today -Type directory
- }
- }
- }
- <#
- .Synopsis
- Gets the current date and caches it to a global variable, $SP2013Today.
- .Description
- Gets the current date and caches it to a global variable, $SP2013Today.
- .Parameter Format
- Format of the Date. Default: "yyyyMMdd.HHmmss".
- .Example
- Get-SP2013Date
- .Notes
- Name: Get-SP2013Date
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Get-SP2013Date {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $Format = "yyyyMMdd.HHmmss"
- )
- If ($SP2013Today)
- {
- Return $SP2013Today
- }
- $today = Get-Date -Format $Format
- Set-Variable -Name "SP2013Today" -Value $today -Scope Global
- Return $today
- }
- <#
- .Synopsis
- Creates the Backup Directory.
- .Description
- Creates the Backup Directory from the Path (Backups Root Path) & Name (Specific Backup Name) or the specific Path.
- .Parameter Path ("Directory")
- The path to the root backups directory (e.g. \\servername\path\to\backups\root\Config).
- .Example
- Get-SP2013Directory -Path "\\servername\path\to\backups\root\Config"
- .Notes
- Name: Get-SP2013Directory
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Get-SP2013Directory {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $false,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path
- )
- Write-Verbose "Path: $Path"
- Write-Verbose ("Returning: $Path" + "\" + (Get-SP2013Date))
- Return $Path.Trimend('\') + "\" + (Get-SP2013Date)
- }
- <#
- .Synopsis
- Watches the current backup for completion and completion status.
- .Description
- Watches the current backup for completion and completion status.
- .Parameter Path ("Directory")
- The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Namespace ("Name")
- Folder name of the SP Config backups to be appended to the Root Path.
- .Example
- Watch-2013BackupStatus -Path "\\servername\path\to\backups\root\Config" -Name "Config"
- .Notes
- Name: Watch-2013BackupStatus
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Watch-2013BackupStatus {
- [CmdletBinding()]
- param(
- # Expects
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace
- )
- BEGIN {
- ## Check backup progress and rip status when complete ##
- Write-SP2013Verbose $Namespace "Processing" "Monitoring backup..."
- $start = Get-Date
- # wait 15s for backup to initialize and restore log to be created
- Sleep 15
- $time = New-TimeSpan $start (Get-Date)
- }
- PROCESS {
- do
- {
- $time = New-TimeSpan $start (Get-Date)
- Write-SP2013Verbose $Namespace "Processing" "Still monitoring. It's only been " + $time.TotalSeconds + "s..."
- # Check for line at the end of the backup script
- $backupStatusSuccess = Get-SP2013BackupLogFile $Path -Contents | Select-String "Backup completed successfully." -Quiet
- # Check Success Result and check for Fail If nothing is there.
- If ($backupStatusSuccess -eq $null)
- {
- $backupStatusFail = Get-SP2013BackupLogFile $Path -Contents | Select-String "FatalError: Backup failed" -Quiet
- If ($backupStatusSuccess -ne $null)
- {
- Write-SP2013Verbose $Namespace "Processing" "FatalError: Backup failed. " + (Get-SP2013BackupLogFile $Path -Contents) -Error
- }
- }
- Else
- {
- Write-SP2013Verbose $Namespace "Processing" "Backup Completed Successfully." -ForegroundColor "Green"
- }
- # Run Loop while both Backup Statuses are empty
- } while ($backupStatusSuccess -eq $null -and $backupStatusFail -eq $null)
- }
- }
- <#
- .Synopsis
- Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
- .Description
- Gets the backup log file from farm backups or partial farm backups (Config/Service Applications).
- .Parameter Path ("Directory")
- The path to the specific root (FarmPath, ConfigPath, SitesPath, SAPath) backup directory (e.g. \\servername\path\to\backups\root\Config).
- .Parameter Content ("GetContents")
- Switch whether to return the file's contents. Default: $false.
- .Example
- Get-SP2013BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
- .Notes
- Name: Get-SP2013BackupLogFile
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Get-SP2013BackupLogFile {
- [CmdletBinding()]
- param(
- # Expects FarmPath, ConfigPath, SitesPath
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Directory")]
- [System.String]
- $Path,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("GetContents")]
- [Switch]
- $Contents
- )
- PROCESS {
- # find last backup directory
- $lastBackupDir = gci $Path | ? { $_.PSIsContainer } | sort -prop LastWriteTime | select -last 1
- # grab the backup log file from that directory
- $backupLogFile = $lastBackupDir.FullName + "\spbackup.log"
- If(Test-Path $backupLogFile -and $Contents)
- {
- Return gc $backupLogFile
- }
- ElseIf(Test-Path $backupLogFile)
- {
- Return $backupLogFile
- }
- Write-SP2013Verbose "Get-SP2013BackupLogFile" "Ending" "Backup Log File ($backupLogFile) does not exist." -Error
- Return
- }
- }
- <#
- .Synopsis
- Sets global variables for the email operations.
- .Description
- Sets global variables for the email operations: From ($SP2013From), To ($SP2013To), SMTP ($SP2013SMTPHost), SubjectPrefix ($SP2013SubjectPrefix)
- .Parameter From ("Sender")
- Global. Sender email address for all email notifications.
- .Parameter To ("Recipients")
- Global. Recipient(s) email address(es) for all email notifications.
- .Parameter SMTPHost ("SMTP")
- Global. SMTP hostname or IP address.
- .Parameter SubjectPrefix ("Subject")
- Global. Subject prefix of the email subject. Default: "[SP2013 Backup] ".
- .Example
- Set-SP2013EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
- .Notes
- Name: Set-SP2013EmailParameters
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Set-SP2013EmailParameters {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.String]
- $From,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Subject")]
- [System.String]
- $SubjectPrefix = "[SP2013 Backup] "
- )
- BEGIN {
- If ( (Get-PSSnapin -Name Microsoft.SharePoint.Powershell -EA "SilentlyContinue") -eq $null )
- {
- Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
- }
- Write-SP2013Verbose "Email" "Starting" "Beginning to set Email Global Variables..."
- }
- PROCESS {
- Set-Variable -Name "SP2013From" -Value $From -Scope Global
- Set-Variable -Name "SP2013To" -Value $To -Scope Global
- Set-Variable -Name "SP2013SMTPHost" -Value $SMTPHost -Scope Global
- Set-Variable -Name "SP2013SubjectPrefix" -Value $SubjectPrefix -Scope Global
- }
- END {
- Write-SP2013Verbose "Email" "Ending" "Email Global Variables Set."
- }
- }
- <#
- .Synopsis
- Sends the email.
- .Description
- Sends the email.
- .Parameter From ("Sender")
- Sender email address.
- .Parameter To ("Recipients")
- Recipient(s) email address(es).
- .Parameter Subject
- Subject of the email.
- .Parameter Body ("Message")
- Body of the email.
- .Parameter SMTPHost ("SMTP")
- SMTP hostname or IP address.
- .Parameter AttachmentName ("Attachment")
- Attachment Name.
- .Example
- Set-SP2013EmailParameters -From "no-reply@domain.com" -To "name@domain.com" -SMTP "mail.domain.com"
- .ToDo
- Use an array for Send-Message
- @{
- Subject = "Backup Failed: Farm Configuration Database"
- Body = "ERROR $_."
- From = $FromAddress
- To = $AdminEmail
- SmtpServer = $MailServer
- }
- [Parameter(
- Mandatory = $true,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Mail")]
- [System.Collections.ArrayList]
- $Mail
- .Notes
- Name: Send-SP2013BackupEmail
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Send-SP2013BackupEmail {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Sender")]
- [System.String]
- $From,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Recipients")]
- [System.String]
- $To,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [System.String]
- $Subject,
- [Parameter(
- Mandatory = $true,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Message")]
- [System.String]
- $Body,
- [Parameter(
- Mandatory = $true,
- Position = 4,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("SMTP")]
- [System.String]
- $SMTPHost,
- [Parameter(
- Mandatory = $false,
- Position = 5,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Attachment")]
- [System.String]
- $AttachmentName
- )
- PROCESS {
- # check for an attachment (successful backup)
- If ($AttachmentName)
- {
- # send email with attachment
- Send-MailMessage -from $from -to $to -Subject $subject -body $body -SmtpServer $SMTPHost -Attachments $AttachmentName -Verbose:$Verbose
- }
- else
- {
- # send email sans attachment
- Send-MailMessage -from $from -to $to -Subject $subject -body $body -SmtpServer $SMTPHost -Verbose:$Verbose
- }
- }
- }
- <#
- .Synopsis
- Write function for all write operations to format the output.
- .Description
- Write function for all write operations to format the output.
- .Parameter Namespace ("Name")
- Name of the current operation being run (e.g., "Farm", "Config", "Sites", "ServiceApplications")
- .Parameter Step ("Stage")
- Current stage of the operation (e.g., "Starting", "Processing", "Ending").
- .Parameter Message ("Msg")
- Message to be written to the screen.
- .Parameter ForegroundColor ("Color")
- Color to write the message on the screen.
- .Parameter WriteHost ("Host")
- Switch whether to write to host. Default: $false ($true if $ForegroundColor exists).
- .Parameter WriteWarning ("Warning")
- Switch whether to write a warning to host. Default: $false.
- .Parameter WriteError ("Error")
- Switch whether to write an error to host. Default: $false.
- .Example
- Get-SP2013BackupLogFile -Path "\\servername\path\to\backups\root\Config" -Contents
- .Notes
- Name: Write-SP2013Verbose
- Author: Travis Smith
- LastEdit: 07/15/2015
- #>
- Function Write-SP2013Verbose {
- [CmdletBinding()]
- param(
- [Parameter(
- Mandatory = $true,
- Position = 0,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Name")]
- [System.String]
- $Namespace,
- [Parameter(
- Mandatory = $true,
- Position = 1,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Stage")]
- [System.String]
- $Step,
- [Parameter(
- Mandatory = $true,
- Position = 2,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Msg")]
- [System.String]
- $Message,
- [Parameter(
- Mandatory = $false,
- Position = 3,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Color")]
- [System.String]
- $ForegroundColor,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Host")]
- [Switch]
- $WriteHost,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Warning")]
- [Switch]
- $WriteWarning,
- [Parameter(
- Mandatory = $false,
- ValueFromPipeline = $true,
- ValueFromPipelinebyPropertyName = $true)]
- [Alias("Error")]
- [Switch]
- $WriteError
- )
- BEGIN {
- $Step = $Step.ToUpper();
- $m = [string]::Format("[{0}] {1}: {2}", $Namespace, $Step, $Message)
- }
- PROCESS {
- If ($WriteWarning)
- {
- Write-Warning $m
- }
- ElseIf ($WriteError)
- {
- Write-Error $m
- }
- ElseIf ($WriteHost -or $ForegroundColor)
- {
- Write-Host $m -ForegroundColor $ForegroundColor
- }
- Else
- {
- Write-Verbose $m
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement