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

