How to: Back Up While the Web Application Is Still Running
Applies to: SharePoint Foundation 2010
This topic explains how you can combine the database snapshot and unattached database features of Microsoft SharePoint Foundation to back up one or more (or all) of the site collections in a content database while its hosting Web application and its child Web sites are up and running. This is accomplished by making a point-in-time copy of the content database and backing up the copy while the original continues to operate.
Backing Up a Live Deployment
There are three steps to backing up a SharePoint Foundation site collection that is up and running. First, create a snapshot of a content database. Second, create an unattached database object out of the snapshot. Third, use the unattached database as the source of the backup instead of the live content database.
Because the unattached database is an SPContentDatabase object, your code can call the Backup(String, String, Boolean) method of its Sites property. You can pass the URL of a particular site collection to the method or your code can loop through all of the site collections and call Backup(String, String, Boolean) for each of them.
To back up all of the content databases in a Web application, your code can iterate through the SPWebApplication.ContentDatabases property and run the three-step process on all the content databases. In addition, of course, you can iterate through all the Web applications of the server farm to back up all the content databases in the server farm without making the Web applications read-only.
For more information about programmatic control of database snapshots, see Programmatic Administration of Database Snapshots.
To Backup a Site Collection
Get a reference to a content database.
Pass the snapshot’s ConnectionString property to the static CreateUnattachedContentDatabase(SqlConnectionStringBuilder) method.
Call Backup(String, String, Boolean) and pass it the URL of a particular site collection.
In the following example, the current site collection is the one that is backed up, but the URL of any other site collection in the same content database could have been passed to the Backup(String, String, Boolean) method.
SPSite siteCol = SPContext.Current.Site; SPContentDatabase cDB = siteCol.ContentDatabase; SPDatabaseSnapshot snap = cDB.Snapshots.CreateSnapshot(); SPContentDatabase unDB = SPContentDatabase.CreateUnattachedContentDatabase(snap.ConnectionString); unDB.Sites.Backup(siteCol.ServerRelativeUrl.Trim('/'), "\\Server\Backups\MySite.bak", true);
Dim siteCol As SPSite = SPContext.Current.Site Dim cDB As SPContentDatabase = siteCol.ContentDatabase Dim snap As SPDatabaseSnapshot = cDB.Snapshots.CreateSnapshot() Dim unDB As SPContentDatabase = SPContentDatabase.CreateUnattachedContentDatabase(snap.ConnectionString) unDB.Sites.Backup(siteCol.ServerRelativeUrl.Trim("/"c), "\Server\Backups\MySite.bak", True)