One of the most popular chapters in our SharePoint 2007 Unleashed book has been the Backup and Restore chapter. I believe that this is mainly due to the fact that I've included code for a script that automates the backup of site collections in a SharePoint Site. This script has always been available on the SAMSPublishing website, but I've had so many requests for it recently that I felt the need to make it more broadly available in this blog. You can freely download the script here. Right-click and select 'Save Target As' to download the file. If you follow the link and try cutting/pasting the page into notepad, the script won't work as your browser will attempt to translate it into XML. After downloading the file, be sure to remove the .txt extension (it should be named SPSiteBackup.wsf).
The script, written by a contributing writer of my book, Tyson Kopczynski, is simple, but powerful. It invokes the pre-existing STSADM command-line utility, runs 'enunsites' to create a list of site collections, then invokes STSADM to backup every site that gets enumerated. It allows for site backups to remote file servers (Assuming the account that runs it has permissions) and even emails a backup report upon completion. One of the bigger advantages of the script is that it works with any version of STSADM, so the script will work for SharePoint Portal Server 2003, MOSS 2007, WSS 2.0 or WSS 3.0.
If you're familiar with STSADM, you know it do point-in-time backups of individual site collections to flat files, which can then be restored to the same location or to a different location. It does a full-fidelity backup, so all data, lists, and permissions are retained. I've had some clients put this into place as a 'free' backup solution, but there are some caveats. First, if your SharePoint environment is very large, it can take a long time to backup (expect 10GB an hour, your mileage may vary. Secondly, it can have a performance hit on the server that it is run from. Lastly, it requires rights to the inviduals site collections to run.
Script syntax is fairly straightforward, you need to enter some variables, such as what virtual server to run it agains, where to backup the files, the name of the SMTP server, and an email address to send a report to. The following is sample syntax:
cscript C:"\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\SPSiteBackup.wsf"
/virt:"https://sp.companyabc.com"
/path:"\\backupserver\backupshare\SPBackups"
/smtpserver:"mail.companyabc.com"
/reportto:"spadmin@companyabc.com"
In this example, the script, which resides in the same location as the STSADM file (the ...\12\BIN folder), is invoked, and backs up the sp.companyabc.com SharePoint virtual server. The backup files are created in \\backupserver\backups\SPBackup and an email report is emailed to the SPAdmin mailbox via the mail server mail.companyabc.com. You could also simplify the path by moving STSADM and the WSF script to a different folder (STSADM needs to be in the same location at the script for it to work), rather than the default 12 Hive location. I have a C:\Backup folder that has both the STSADM file and the script in it (Yes, this is how we backup the CCO SharePoint environment). Easiest way to automate the script is to create a batch file that invokes the script, then to use Windows Scheduled tasks to schedule it for a specific time. I do nightly backups, for example.
So, I encourage you to download the script and try it out! Please let me know if you have any questions or run into any problems getting it setup.
Cheers,
Michael

Backup Script
I have attempted to use the script and when it gets to the enumsites portion of the code it get the prescribed error message:
Enum Sites Critical Error: stsadm command failed.
Any ideas why this may be happening?
I replaced all of the information in the Syntax to reflect my enviornment? Is there any changes that need to be made to the script?
Thanks,
AJ
Backup Script
Hi AJ,
The script simply invokes the existing STSADM file and runs a backup using the default syntax. For example, 'stsadm.exe -o enumsites -url http://fqdn.companyabc.com'. To troubleshoot, try running the stsadm command without the script to see if it fails. It may give you a more descriptive error. Also, try changing the FQDN to http://localhost or another option to see if it changes.
If it still doesn't work, can you cut/paste the exact syntax you are running the script as? Also, make sure you are right-clicking and downloading the script, instead of cut/pasting it from your browser.
Cheers,
Michael
Backup Script
Hi Michael,
Today I was finally able to get the script to execute properly. I believe that I made a syntax error referring to the location of my shared folder. The script works great and has saved me a ton of time. Thank you for your quick response and for producing this helpful script.
Thanks,
AJ
Backup Script
Great to hear, glad you got things working AJ!
Cheers,
Michael
Site by site backup in WSS
Hi,
I wanted to do site-by-site backup in Windows SharePoint Service.Can anyone tell me that is it possible or not and if yes then how ?
SPSiteBackup.wsf runs forever.
Hi Michael
This script looks really good, but when I try running it, it only gives EnumSite [OK] message, a 1 KB log file is created and then nothing changes. I noticed that CPU activity is happening from Cscript but even after leaving this thing running overnight it does not do any backup. I think it is going into an infinite loop. I have only a few sites with very less data.
Please suggest what could be the problem.
Thanks
Nidhi
Script running forever
Hi Nidhi,
It should run fairly quickly, especially if you don't have alot of data. Did you download the file (right-click - Save As) instead of copying/pasting? Also, have you tried manually running STSADM -o backup to see if it's a problem with the STSADM file? If you're still having issues, cut/paste the syntax you are running the script as...
Best Regards,
Michael
Hi Michael Thanks for your
Hi Michael
Thanks for your response. Yes , I did do "Save as" and I am able to backup using stsadm -o backup command without any issues. Its only with the script that it doesnt do anything after EnumSites.
Here is my syntax:
cscript spsitebackup.wsf /virt:"http://share-test" /path:"d:\backups\site_coll\" /smtpserver:"smtpmail.abc.com" /reportto:"nidhi.seth@abc.com"
NOTE: I have copied the script as well as stsadm.exe in the c drive. I have also tried with the UNC path, but that doesnt change anything, the script still gets stuck.
Thanks much
Nidhi
Hi Nidhi, Try replacing
Hi Nidhi,
Try replacing http://Share-test with localhost, it will work. Stsadm.exe is unable to recognize the site name.
Thanks
Rashmi
Enum Sites: Critical Error
Hi there
Were you able to solve your problem?
I am getting the following error :
"Enum Sites Critical Error: stsadm command failed"
1) Have done "Save as" for the script (while downloading)
2) Tried replacing the URL (localhost and other options)
3) Tried on different test servers
No Success so far ;-(
Any idea or suggestion where I could be going wrong?
PS: Am running this for MOSS2007 standalone environment
Thanks & Regards
Vivek Bhatia