Tuesday, February 17, 2009

Scheduling Periodic/Delayed SharePoint-STSADM Backup using Windows Task Scheduler

STSADM can be used effectively with Windows Task Scheduler to schedule periodic/delayed backups.


STSADM is the powerful built-in command line tool to backup/restore a SharePoint server farm or web application or site collection or a database. This tool has very simple, faster and easy to use commands for backup/restore SharePoint data.

STSADM backup syntax

For site collection backup

stsadm -o backup

For catastrophic backup

stsadm -o backup

Please refer the following to get more details about the backup syntax

Locking Site

Backup process will take longer time for large site collections. If any changes made to the site collection during the backup process, the backup can become corrupted. The preferred approach would be locking the site collection using Setsitelock command of STSADM till the backup process complete. Once the backup process completes, reset the site collection to its default state.

Please refer the following for more information on Setsitelock

To get the current access level of the site
stsadm -o getsitelock -url <URL of Site Collection>

To lock the site/changing the site to read-only.
stsadm -o setsitelock -url <URL of Site Collection> -lock readonly

The following steps needs to be done for scheduling a periodic/delayed backup.

Batch File

Create a batch file, use Notepad to enter the below script and save as “Backup_Script.bat”.
This script creates daily backup of a site collection. A new backup file will be created each day and the file name is auto generated concatenating the current system date.

The script does the following

1) Determine the lock status
2) Locks the site collection
3) Creates a file name using current date
4) Creates backup
5) Unlock the site collection

Backup Script

@echo off
@echo --------------------------------------------------------
@echo backing up site collection <URL of Site Collection>
@echo --------------------------------------------------------
cd \Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
@echo locking site collection before the backup process starts
@echo off
stsadm -o getsitelock -url <URL of Site Collection>
stsadm -o setsitelock -url <URL of Site Collection> -lock readonly
@echo Site locked

@Echo Generating File Name using current date
@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set All=%%C%%B%%A

@echo off
stsadm -o backup -url <URL of Site Collection> -filename "C:\Backup\Backup_%All%.bak"
@echo backup completed
@echo off
stsadm -o setsitelock -url <URL of Site Collection> -lock none
@echo Site lock removed

Note: Replace “<URL of Site Collection>” with your site collection URL. Also I’ve used a folder named “C:\Backup” change it to your respective folder.Windows Scheduled Tasks

Create a new Windows Scheduled Task by navigating Start -> Control Panel -> Scheduled Tasks -> Add Scheduled Task. The following screen shows the Windows Scheduled Task Wizard.

Click the Browse button and select the backup batch file (Backup_Script.bat). Click on Next to schedule the backup. The screen below shows the available options for scheduling a task.

Select the convenient time to take the backup. It is better to choose low traffic hours for backing up a site.

Once the time schedule has been selected, click on Next and supply the credentials to run the task.

The account which is used for executing the task should have the Administrative permission. Click Finish to complete the wizard. Now a daily backup has been scheduled, the Task Scheduler will execute the batch file daily at specified time.

No comments: