Welcome to MSDN Blogs Sign in | Join | Help
Recursive Backup and Recovery in SharePoint

OK boys and girls.  It is time for a post that is more on topic than I have been lately.  Today’s story actually stems from an internal discussion between several of us Premier Field Engineers who specialize in SharePoint.  We were having a discussion on the best way to perform site collection backups in a more automated fashion.  One of my buddies (shout out to JC) had a VBScript (don’t know where he got it) that automated the process, but the script was broke and needed a little TLC. 

It was actually a very simple concept.  Enumerate the sites out to XML (you can do that via STSADM) and then loop through the XML to grab a backup of each site collection.  Since there was a limitation in STSADM that it didn’t automatically lock the site prior to backup, we were originally using a Keith Richie tool called SPLSBackup.  Because the STSADM in MOSS does perform locks, I switched over to the native tool for the newer version.  At the end of this post I will have a zip file that contains all of the files for both 2003 and 2007.

The final part of this script would only launch a certain number of processes at a time.  This could be set in the script to your whim.  Then the script would loop through and launch that many processes.  It would then wait until a process completed and launch another.  In this fashion, it would move through all the site collections until finished.  Throw in a little logging and you have a pretty usable script that you can run as needed to perform site collection level backups.  Now on to the good stuff.

To best show how all of this works, I have built a couple of demo batch files.  A quick not about setup though.  I usually have a clean install of MOSS when I go onsite to demo this to customers.  These batch files that I include are just there to get the demo running and clean it up afterwards.  So use in the wild may be different than demonstrated.  Don’t say I didn’t warn you!

Part of the first batch file I stole repurposed from a demo that Mr. Geoffrey Edge gave at TechReady 4 earlier this year.  It is a quick batch file that uses STSADM to create a site collection and a bunch of nested webs underneath.  I added in a couple more site collections as well.  A separate batch file in the zip will also create the same collections each in a separate content database.

stsadm.exe -o createsite -url http://portal/sites/css -title "Customer Service & Support" -sitetemplate STS -ownerlogin domain\user -owneremail user@domain.com
 
stsadm.exe -o createweb -url http:// portal /sites/css/account -title "Accounts" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/setup -title "Account Setup" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/changes -title "Account Changes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/addresschanges -title "Address & Billing Changes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/servicechanges -title "Service Changes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/namechanges -title "Name Changes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/statements -title "Statement Changes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/taxes -title "Taxes" -sitetemplate STS
stsadm.exe -o createweb -url http:// portal /sites/css/account/billingdetails -title "Billing Statement Details" -sitetemplate STS

Once this is done, you can alter the VBScript as needed for your environment.  Listed below are the main items that will need to be altered.

Const STSADM_PATH = "C:\path_to_stsadm\stsadm"
Const STSADM_LOGPATH = "C:\path_to_where_you_want_your_logs_to_go\"
Const STSADM_BAKPATH = "C:\ path_to_where_you_want_your_backup_files_to_go \"
Const STSADM_LOGFILE = "LogFileName.txt"
Const STSADM_XMLFILE = "XMLFileName.txt"
Const STSADM_PORTAL_URL =
http://portalURL/
Const NUM_BACKUP_PROCESSES = 4

Now you can run the VBScript and sit back to see what happens.  If all goes as planned, you should have a directory full of backup files for later use as well as an XML file (the output of the site enumeration we used in our loop) and a log file.  The log file should look something like this:

3/14/2007 12:37:18 PM: Begin backup
3/14/2007 12:37:20 PM: Enum Sites
3/14/2007 12:37:20 PM: Wrote XML
3/14/2007 12:37:20 PM: Read XML into DOM
3/14/2007 12:37:20 PM: Executing Command 'stsadm -o backup -url "http://portal" -filename "C:\backups\portal.bak"'
3/14/2007 12:37:20 PM: Executing Command 'stsadm -o backup -url "http://portal/sites/css" -filename "C:\backups\portal_sites_css.bak"'
3/14/2007 12:37:20 PM: Executing Command 'stsadm -o backup -url "http://portal/sites/hr" -filename "C:\backups\portal_sites_hr.bak"'
3/14/2007 12:37:20 PM: Executing Command 'stsadm -o backup -url "http://portal/sites/it" -filename "C:\backups\portal_sites_it.bak"'
3/14/2007 12:37:22 PM: Waiting for command to execute and complete
3/14/2007 12:37:23 PM: Waiting for command to execute and complete
3/14/2007 12:37:25 PM: Waiting for command to execute and complete
3/14/2007 12:37:26 PM: Waiting for command to execute and complete
3/14/2007 12:37:27 PM: Waiting for command to execute and complete
3/14/2007 12:37:28 PM: Waiting for command to execute and complete
3/14/2007 12:37:32 PM: Waiting for command to execute and complete
3/14/2007 12:37:33 PM: Waiting for command to execute and complete
3/14/2007 12:37:33 PM: Found Open Slot:  g_intOpenProcessSlot is 1
3/14/2007 12:37:33 PM: Executing Command 'stsadm -o backup -url "http://portal/sites/sales" -filename "C:\backups\portal_sites_sales.bak"'
3/14/2007 12:37:33 PM: g_intTotalProcessesLaunched is 5
3/14/2007 12:37:34 PM: Waiting for command to execute and complete
3/14/2007 12:37:34 PM: Found Open Slot:  g_intOpenProcessSlot is 3
3/14/2007 12:37:34 PM: Found Open Slot:  g_intOpenProcessSlot is 2
3/14/2007 12:37:34 PM: Executing Command 'stsadm -o backup -url "http://portal/sites/marketing" -filename "C:\backups\portal_sites_marketing.bak"'
3/14/2007 12:37:34 PM: g_intTotalProcessesLaunched is 6
3/14/2007 12:37:34 PM: Backup of portal site collections complete
Total Backup Processes:  6

Now that you have some backup files, what can you do with them?  The main possibilities come in 2 flavors.  This first is recovery of a whole site collection.  Let’s say that one of your junior SharePoint admins accidentally typed in the following command:

stsadm –o deletesite –url http://portal/sites/css

At this point, the recycle-bin is useless to you as you can’t recover site collections from it.  What do you do?  You start by deciding on a mean enough punishment for the bad admin.  It should include some form of shame and ridicule enough to get the point across.  Maybe a rolled up newspaper comes into play along with the words “Bad junior admin!!”  Once that is done, you can reach into your backup directory and pull out this command:

stsadm –o restore –url http://portal/sites/css -filename portal_sites_css.bak

Another flavor is if someone needs to recover something from a site, sub-site or list that is no longer in the recycle-bin or for some reason can’t be properly recovered.  I will often build a separate web application within MOSS (or on another server if you wish) that I use for recovery.  You can then run the same script as above except on the other application/server.  My recovery web application is usually hidden and uses a non-standard port (I like to use 1130 since it is the anniversary of the day my wife really took over my life (love ya honey!)  So the command would be:

stsadm –o restore –url http://portal:1130/sites/css -filename portal_sites_css.bak

There are probably other tricks that you can do from here depending on your abilities and how far you want to automate this.  My colleagues who use PowerShell may have additional comments on this, but that is another topic way off my current beaten path (“Bad PowerShell junkies!!”)

I have delivered this little demo to 3 customers so far and as far as I know, 2 of the 3 have added this bit of flair to their kit bag.  This is a great thing to be able to automate.  My only word of caution here deals with the limitations of STSADM backups in SharePoint 2003.  You should read this link (http://support.microsoft.com/default.aspx/kb/889236/en-us) on the subject.  This is the reason that in 2003 we used SPLSBackup as mentioned earlier.  This file is included with the 2003 zip file.

I hope this post is helpful to you.  If you have questions or comments, you can add them to the end of this post and I will do what I can to get you answers.  And to my friends in California who asked for this post to be written sooner rather than later (you know who you are), here it is.  Until our paths cross again!  Cheers.

Posted: Saturday, March 17, 2007 1:01 AM by stevenro
Attachment(s): Backup-Restore.zip

Comments

Evaldo Santos said:

I there,

The vbs scrips seems to run... but in the backup folder still empty. No log files are created.

I don´t knwo if i am missing something!

Thanks for the time anyway.

Evaldo Santos

# June 28, 2007 10:08 AM

stevenro said:

Did you change all of the items as listed above?  You need to make sure you change the various constants to point to the place you want them to go as well as pointing the STSADM path to the actual file location.  I genericized it for this blog post.  If you still have problems, let me know and I will try and work with you offline to solve this.  I use it at many customer sites and have rarely had a problem that wasn't a typo.

# June 28, 2007 5:12 PM

ReN said:

After changing all the pat to suit my needs I'm getting an error from line 128 char 5.

I didn't touch this line at all.

# September 13, 2007 11:28 PM

stevenro said:

ReN...is this the VBScript from inside the ZIP?  I have been using this script with customers for awhile now and haven't run into any problems.  Maybe there is something in the URL that causes the script to bomb.  What happens if you run the individual commands from a command prompt instead of via the VBS file?  Also, make sure you are running the right version.  There is a 2003 version and a 2007 version and they do have differences inside the script.

If you want, you cansend me the script file as .txt and I will take a look at it.  

# September 19, 2007 1:11 AM

Mauk said:

But how can I get this zip file with your examples?

Mauk

# September 26, 2007 2:35 AM

stevenro said:

You actually have to click into the post itself (it no longer shows on the main page) at http://blogs.msdn.com/stevenro/archive/2007/03/17/recursive-backup-and-recovery-in-sharepoint.aspx and then you will see the file just above the comments.  It is listed as "Attachments:" above.  It used to show on the main page, but after a certain number of posts happen after it, the link is no longer displayed out there.

# September 26, 2007 8:14 AM

Chris said:

Fantastic - does exactly what I need - thank you very much.

# September 28, 2007 6:28 AM

Mauk said:

Thank you, I found the zip file. As I know the site locking is very important before the backup. I couldn´t findin backup string anything about locking sites?

# September 28, 2007 4:03 PM

Keith said:

Great script... just what I needed. I disabled the part that deletes log files (the comment says it deletes the backup folder) and changed CreateTextFile to OpenTextFile (for appending), so that I have a running log file. Thanks again for something that will definately give me some extra security. (and sleep)

# November 27, 2007 5:21 PM

Tykkedyr said:

Hello

I'm getting an error on line 56 char 1 saying:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm -o enumsites -url http://cph1sps1 is not a valid Win32 application.

Can you help me on that one?

# November 28, 2007 7:45 AM

Gunther said:

Just what I was looking for. Thx!  

I put all the files of the zip in C:\Backup and updated the constants (I pasted all paths to my updated contants in Windows Explorer to make sure they're valid as well as the portal URL).  Everything checks out and when I run the VBS, the BackupXML.txt is created with an enumeration of all my site collections.  The BackupLog.txt also has these entries in it:

 11/29/2007 11:44:11 AM: Begin backup

 11/29/2007 11:44:26 AM: Enum Sites

 11/29/2007 11:44:26 AM: Wrote XML

 11/29/2007 11:44:26 AM: Read XML into DOM

But then the VBS errors out with the following message:

 Script: C:\Backup\Rcursive SPS Backup.vbs

 Line: 72

 Char: 1

 Error: Object required" 'DocumentElement'

 Code: 800A01A8

 Source Microsoft VBScript runtime error

Any ideas what's causing this?  Much appreciated!

# November 29, 2007 4:58 PM

Martin said:

Gunther:  I got the same error.   Run an stsadm -o enumsites on your root url.   Carefully check the output.   I've found if you get any errors here the script won't work.   These errors can be caused by pratially deleted site collections.

# December 4, 2007 8:16 PM

stevenro said:

I would ask a couple of questions.  First, are you using the right script.  There is one for 2003 and one for 2007.  Second, do you have all of the latest MSXML updates?  I have gotten that before by not having the right updates installed.  Third, run the enumsites like above.  If you find issues (and are running SP2/SP3), you might also want to try and run STSADM -o repairorphans to see if that fixes anything.  Sometimes you get these orphaned sites that still show up and cause problems.  This fixes that issue.

# December 4, 2007 10:24 PM

David Kearns said:

Great post!

I have created a similar script but an now searching for a way to only back up sites that have changed since last back up.

Enumsites does not get granular enough to identify sites enough to do a diff against yesterdays enum results and todays.

Do you have an suggestions?

thanks

# December 14, 2007 6:58 PM

AES said:

Thanks for this! It's exactly what I needed and is working great except for one thing.

The .bak files are not being deleted when I run the script, but the log files are being deleted. The log file shows no errors and the script runs without errors, but no new .bak files are created. If I manually delete them before I run the script all is well.

I'm sending the bak files to file server, and logs are stored locally.

Some of the bak files are to large for the recycle bin, but even the smaller site collection bak files are not deleted as well.

Any suggestions?

Thanks!

# January 22, 2008 12:24 PM

me said:

Hi

This sounds like a great script but the need for it concerns me.

It has been suggested to me that running the following quoted line will backup everything necessary to be backed up in order to restore my entire MOSS installation.

"

stsadm.exe -o backup -directory [directory] -backupmethod full

"

Is this suggestion incorrect??

Will the supplied backup utility not actually backup my entire MOSS instance??

Thank you very much in advance!!

# April 16, 2008 3:38 PM

Nixon said:

Hi,

 We tried to  export the sites using export command in the script..its not working..Does anybody know the change to be made in the script for this to work

Nixon  

# May 19, 2008 3:10 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker