Sunday, March 11, 2012

How to Back Up While the Web Application Is Still Running


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.


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.


To Backup a Site Collection

  1. Get a reference to a content database.
  2. Call the CreateSnapshot() method of the content database's Snapshots collection.
  3. Pass the snapshot's ConnectionString property to the static CreateUnattachedContentDatabase(SqlConnectionStringBuilder) method.
  4. 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);
    





No comments:

Post a Comment