Friday, 5 August 2011

How to create backup file automatically and create auto schedule

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
@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.
image001.jpg
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.
image002.jpg
Select the convenient time to take the backup. It is better to choose low traffic hours for backing up a site.
image003.jpg
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:

Post a Comment