Introduction
STSADM can be used effectively with Windows Task Scheduler to schedule periodic/delayed backups.
STSADM
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
-url<URL name>
-filename<file name>
[-overwrite]
For catastrophic backup
stsadm -o backup
-directory<UNC path or local drive>
-backupmethod<full or differential>
[-item]<created path from tree>
[-percentage]<integer between 1 and 100>
[-backupthreads]<integer between 1 and 10>
[-showtree]
[-quiet]
Please refer the following to get more details about the backup syntax
Backup: Stsadm operation (Office SharePoint Server)
Locking Site
The backup process will take a 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 the Setsitelock
command of STSADM
till the backup process is complete. Once the backup process completes, reset the site collection to its default state.
Please refer the following for more information on Setsitelock
Setsitelock: Stsadm operation (Office SharePoint Server)
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
- Determine the lock status
- Locks the site collection
- Creates a file name using current date
- Creates backup
- Unlock the site collection
@echo off
@echo--------------------------------------------------------
@echo backing up site collection <URL of SiteCollection>
@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.