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.