SharePoint Site Backup and Restore using STSADM

Scenario: Backup a site collection and restore it on a different server or on the same server.

Preface: Let’s build some a simple batch file here. First declare the constants. Notice the BACKUPFILENAME variable. This lets you build the file name as YYYY_MM_DD_BACKUP.BAK

Code:


	
@SET STSADM="c:\program files\common files\microsoft shared\web server extensions\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...

 

Things to know:

Different Server Restore: If the target site does not exist the Restore commands will create a new site collection.

Same Server Restore: You have to use ‘-override’ parameter with the restore command or you have to create a new content database for that Site Collection and then restore.

stsadm-backup