How to Backup SharePoint Site Collection automatically using PowerShell!
Site Collections backup can be initiated using the Central Admin Site but in order to have something automated with periodic bases along with notifications need a scripted flow to cover all the requirements.
SharePoint offers a way to perform backups at site collection level using “Backup-SPSite” command. This procedure is designed to take an exact copy of a SharePoint Site and in that case no data will be lost, changed and transformed., the backup file generated more or less as a SQL dump (per-site-bases) of SharePoint content database. It maintains the GUID of every object, except the GUID of the root site. when backup is restored, SharePoint generates a new GUID for the site collection.
Site Collections Backup / Email Notification / Scheduled backups. (All in One)
Using PowerShell automatic backups can be taken at a Site Collections level, the below script can be used to perform Site Collections level backups and it will also shoot an email to Admin(s) on a successful or failed backup jobs.
Windows Batch File and Task scheduler can be used to automate periodic backups plan. The following steps can be followed to setup an automatic flow.
- Copy the below provided backup script and save as “SPSiteCollectionsBackup.PS1”
- Create a batch file and give it a name and save as “RunSPBackup.bat” — Place the following lines in Windows Batch File (bat) and re-save.
cd /d %~dp0
PowerShell -file “.\SPSiteCollectionsBackup.ps1” - Schedule a Batch File to run this task automatically, On SharePoint Server :
-
- Click on Start and under search, type in Task and click open Task Scheduler.
- Select Create Basic Task from the Action pane on the right of the window.
- Under Create Basic Task, type in the name you like and click Next.
- From the Trigger select the option you like and click Next.
- Then click on Start a Program and click Next.
- Now click on Browser and select the batch file you like to run. Finally, click on Finish to create the Task.
Following is the script to take backup at Site Collection level and send email notification. (Please specify backup path, site url and smtp settings as per your own environment).
– On top of that underneath a simple way to restore the Site Collection using “Restore-SPSite“command.
#==================== Backup Script Starts ======================
Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
{
$today = (Get-Date -Format MM-dd-yyyy)
# Backup Folder Path (Source)
[IO.Directory]::CreateDirectory(“C:\site-backup-weekly\$today“)
# SharePoint Site Collection to backup
$Site = https://CustomerA.hostingdomain.com
# initiate the backup
Backup-SPSite –Identity $Site -Path “C:\site-backup-weekly\$today\CustomerA.bak”
# From Address as per your environment.
$emailFrom = “no-reply@hostingdomain.com”
# email address to which the Notification should be sent.
$emailTo = “SPAdmins@hostingdomain.com”
# Subject for the email.
$subject = “SharePoint Site Collection “+”$Site was backed-up Successfully on “+”$today”
# Body of the email..
$body = “SharePoint Site Collection “+”$Site was backed-up Successfully on “+”$today”
# your SMTP server/Port. Make sure relay settings are enabled for the SharePoint servers on your SMTP server.
$smtpServer = “smtp.hostingdomain.com”
$port = “25”
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $port)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
Catch
{
$ErrorMessage = $_.Exception.Message
# email details.
$emailFrom = “no-reply@hostingdomain.com “
$emailTo = “SPAdmins@hostingdomain.com “
$subject = “SP Site Collection “+”$Site backup failed on “+”$today”
$body = “SP Site Collection “+”$Site backup failed on “+”$today , error message $ErrorMessage.”
$smtpServer = “smtp.hostingdomain.com”
$port = “25”
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $port)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
# ======================= Backup Script Ends ===========================
Restore SharePoint Site Collections.
In order to restore the Site Collections back up the below PowerShell statement can be utilized.
Restore-SPSite -Identity https://customerA.hostingdomain.com -Path C:\site-backup-weekly\12-04-2017\CustomerA.bak -HostHeaderWebApplication https://hostingdomain.com -Force
-Identity = Site Collections URL
-Path = Backup path (Source)
-HostHeaderWebApplication = web application URL (if Site is a hostnamed site collection).
Enjoy !
Author : Jameel
…
For product & sales related queries, please email us at sales@machsol.com
Disclaimer: The sample scripts are provided AS IS without warranty of any kind. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall MachSol, its authors be liable for any business or Data damages. we strongly suggest to use it in a test environment first and then try in production platform.