viernes, 8 de marzo de 2013

Powershell script to backup farm configuration and service applications

In summary, this script will:
  1. Delete all files in the existing backup directory.
  2. Backup the farm configuration database.
  3. Catch any errors that occur and send an e-mail to the administrator.
  4. Backup ALL service applications in the farm (full).
  5. Catch any errors that occur and send an e-mail to the administrator.
  6. Write all actions / error messages to a timestamped log file.
Update the variables in the script to suit your requirements.

#Variables
$logfile = "SPFarm-Backup-" + $(Get-Date -Format dd-MM-yy) + ".log"
$ConfigDB = "SP_Config"
$DBServer = "server"
$BackupConfigFolder = "\\server\SharePoint\Backup\Config"
$BackupSAFolder = "\\server\SharePoint\Backup\ServiceApp"
$AdminEmail = <a href="mailto:admin@sharepoint.com">admin@sharepoint.com</a>
$MailServer = "mail.sharepoint.com"
$FromAddress = <a href="mailto:sharepoint.notifications@sharepoint.com">sharepoint.notifications@sharepoint.com</a>
 
Write-Host "Script starting.."
 
# Backup Farm Configuration
try
{
 ac $logfile "$(Get-Date)`t Clearing old configuration backups.."
 Get-ChildItem $BackupConfigFolder | foreach ($_) {Remove-Item $_.fullname}
 ac $logfile "$(Get-Date)`t Done."
 ac $logfile "$(Get-Date)`t Backing up SharePoint Farm Configuration.. "
 Backup-SPConfigurationDatabase -Directory $BackupConfigFolder -DatabaseServer $DBServer -DatabaseName $ConfigDB -Verbose
 ac $logfile "$(Get-Date)`t Done."
}
catch [system.exception] 
{
   ac $logfile "$(Get-Date)`t An error occured while backing up the farm configuration database: $_."
 
     $messageParameters = @{
    Subject = "Backup Failed: Farm Configuration Database"
    Body = "ERROR $_."
    From = $FromAddress
    To = $AdminEmail
    SmtpServer = $MailServer
   }
 
   Send-MailMessage @messageParameters
}
 
# Backup Service Applications
try
{
 ac $logfile "$(Get-Date)`t Clearing old service application backups.."
 Get-ChildItem $BackupSAFolder | foreach ($_) {Remove-Item $_.fullname}
 ac $logfile "$(Get-Date)`t Done."
 ac $logfile "$(Get-Date)`t Backing up SharePoint Farm Configuration.. "
 Backup-SPFarm -Directory $BackupSAFolder -BackupMethod Full -Item "Farm\Shared Services" -Verbose
 ac $logfile "$(Get-Date)`t Done."
}
catch [system.exception] 
{
   ac $logfile "$(Get-Date)`t An error occured while backing up the service application database: $_."
 
     $messageParameters = @{
    Subject = "Backup Failed: Service Application Database"
    Body = "ERROR $_."
    From = $FromAddress
    To = $AdminEmail
    SmtpServer = $MailServer
   }
 
   Send-MailMessage @messageParameters
}
 
Write-Host "Done."

No hay comentarios:

Publicar un comentario