Wednesday, July 22, 2009

SharePoint Site Backup and Restore using STSADM

Scenario: Backup sitecollection and restore it on a different server or same server.

Preface: Lets build some simple script (batch) files here. First declare the constants. Notice the BACKUPFILENAME variable. That would build the file name as YYYY_MM_DD_BACKUP.BAK

Code:

@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\12\bin\stsadm"
@SET MAINSITEURL="http://servername"
@SET BACKUPFILENAME="C:\SiteBackUp\%date:~10,4%_%date:~4,2%_%date:~7,2%_backup.bak"

Backup Commands: Before you take the backup, ensure the Site has 'none' lock. However, when you are ready to run the backup... make sure you make the site lock to 'readonly' so that no further changes can occur.

@echo off
Echo ------------------------------------------------------------------

Echo Getting Site Lock
stsadm -o getsitelock -url %MAINSITEURL%

Pause

Echo Setting Site Lock to ReadOnly
stsadm -o setsitelock -url %MAINSITEURL% -lock readonly

Echo Backing up the top level site
stsadm -o backup -url %MAINSITEURL% -filename %BACKUPFILENAME% -overwrite

Echo Setting Site Lock to None
stsadm -o setsitelock -url %MAINSITEURL% -lock none

Echo ------------------------------------------------------------------
Pause please press any key to continue...

Restore Commands: Use the below script to restore the backed up site onto the new site collection on a new machine.


Echo Getting Site Lock
stsadm -o getsitelock -url %MAINSITEURL%

Pause
@echo off
Echo ------------------------------------------------------------------

Echo Setting Site Lock to none
stsadm -o setsitelock -url %MAINSITEURL% -lock none

Echo Restoring the top level site
stsadm -o restore -url %MAINSITEURL% -filename %BACKUPFILENAME%

Echo Setting Site Lock to None
stsadm -o setsitelock -url %MAINSITEURL% -lock none

Echo ------------------------------------------------------------------
Pause please press any key to continue...



Output: In case the target site does not exist Restore commands would create a new site collection (if it were a different server). If restoring on the same server, you have to use '-override' parameter or have to create a new content database for that Site Collection and then restore.

No comments: