Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, December 05, 2011

Run a SharePoint PowerShell Backup Script using Windows Scheduler

SharePoint administrators need to run regular backups using PowerShell, the STSADM tool or in Central Administration. There is no "built in" way to automate these backups. Wouldn't it be great to devise a method to automate these jobs.
The solution is just to create a batch file that can execute a PowerShell script, and then launch it from the Windows Task Scheduler.

PowerShell Command to Backup SharePoint Site Collection

backup-spsite -identity http://moss2010/ -path D:\Backup\Backup.bak
So, you can use the backup-spsite command to do site backup (the example shows http:// moss2010 /). The following script will start a full backup to D:\backup where you can send a site collection URL and backup file name as a parameter to the PowerShell Script.
$args[0] = http://moss2010/ [Source site location URL] 
$args[1] = D:\backup\backup_site.bak [Destination path] 

Create Windows PowerShell script

Add-PSSnapin Microsoft.SharePoint.PowerShell 
backup-spsite -identity $args[0] -path $args[1] -force
Save it as PowerShell D:\Scripts\BackupSPSite.ps1

Create Batch Script to execute PowerShell script

@echo off
SET SOURCE_SITE=http://moss2010/ 
SET DEST=D:\backup\Backup_site.bak
echo "backup Started at" %DATE% >> D:\backup\Log.txt
powershell -command D:\Scripts\BackupSPSite.ps1  %SOURCE_SITE% %DEST%
echo "Backup completed successfully at %DEST%" on %DATE% >> D:\backup\Log.txt
@echo on
Save it as Batch file D:\Scripts\BackupSPSite.bat. Now you have to run this script.

Run Batch Script to execute PowerShell script

  • Run batch script to check successful backup and log creation at D:\backup.
  • Now run it from the Windows Task Scheduler. In the Task Scheduler, the account needs to be set with admin permissions to run it properly.
So now you can automate your daily backup of a SharePoint Site. You can also run an entire Farm backup just by using the following command in a PowerShell Script (i.e. D:\Scripts\BackupSPSite.ps1)

No comments:

Post a Comment