SharePoint PowerShell Backup Script with Windows Scheduler

An easy script to backup your SharePoint farm with Windows scheduler. First create a powershell script:

1-Backup a site:
Add-PSSnapin Microsoft.SharePoint.PowerShell
backup-spsite -identity $args[0] -path $args[1] -force

2-Backup the complete farm:

Add-PSSnapin Microsoft.SharePoint.PowerShell
Backup-SPFarm -Directory $args[1] -BackupMethod full

Next, create a .bat script to start the powershell script:

@echo off
SET SOURCE_SITE=http://sharepointfarm/
SET DEST=E:\SQLBackupFolder\
echo “Backup SharePoint Started at” %DATE% >> E:\SQLBackupFolder\Logs\Log.txt
powershell -command E:\SQLBackupFolder\SPbackupScripts\backupSP.ps1 %SOURCE_SITE% %DEST%
echo “Backup completed successfully at %DEST%” on %DATE% >> E:\SQLBackupFolder\Logs\Log.txt
@echo on

Now you can use the .bat start script in Windows Scheduler to backup your sharepoint farm. In the Task Scheduler, the account needs to be set with admin permissions to run it properly.

To clean up older sharepoint backups, schedule the following powershell script. This script checks the spbrtoc.xml for the number of sharepoint backups.

# Location of spbrtoc.xml
$spbrtocXML = “\\UNC backup folder\spbrtoc.xml”
# 10 Days of backup will be remaining after backup cleanup.
$days = 10
# Import the Sharepoint backup spbrtoc.xml file
[xml]$sharepointlist = gc $spbrtocXML
# Find the number of backups in spbrtoc.xml
$oldbackups = $sharepointlist.SPBackupRestoreHistory.SPHistoryObject |
? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
# Delete the backups from the Sharepoint backup spbrtoc.xml file
$oldbackups | % { $sharepointlist.SPBackupRestoreHistory.RemoveChild($_) }
# Delete the folders of the old backups on file level
$oldbackups | % { Remove-Item $_.SPBackupDirectory -Recurse }
# Save the changed spbrtoc.xml
$sharepointlist.Save($spbrtocXML)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>