Logo
CAR REVIEW
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
PREGNANCY
 
 
Windows Server

SharePoint 2010 : The SharePoint Object Model (part 3) - Programmatically Using SQL Snapshots

5/24/2011 11:47:36 AM

Programmatically Using SQL Snapshots

The SPDatabase type in the Microsoft.SharePoint.Administration namespace represents a SQL Server database, and SharePoint uses SPDatabase and its derived types to read, write, and manipulate the contents of databases that are used by the farm. Particularly noteworthy among the SPDatabase-derived types is the SPContentDatabase type, which represents a content database housing site collections.

In SharePoint 2010, the SPDatabase type has been extended with a Snapshots property. As shown in Figure 4, the Snapshots property exposes a collection of type SPDatabaseSnapshotCollection. Through the Snapshots collection and each of its SPDatabaseSnapshot items, it is possible to create, delete, and manage SQL Server snapshots for the database in the underlying SQL Server instance.

Figure 4. Database snapshot support for SPDatabase and derived types.

When a snapshot is created, either programmatically through the SharePoint object model or directly through SQL Server, that snapshot is treated as if it were a completely different readonly database on SQL Server. Although snapshots are commonly created from live SharePoint content databases, they are not attached to the SharePoint farm. Because the snapshots aren’t affiliated with a SharePoint farm, you need to use the new unattached content database model (through the SPContentDatabase.CreateUnattachedContentDatabase method) to interact with them.

Listing 4 demonstrates the same site collection backup operation that was shown in Listing 3, but it shows how you can use a database snapshot to enhance the overall backup process. By executing the SPSiteCollection.Backup operation against a database snapshot instead of the live content database, there is no need to lock the live database while the backup is being performed. Users can continue to conduct read and write operations as they normally would, and the backup operation can proceed against the snapshot without worries of corruption or inconsistencies. When the backup operation is complete, the snapshot is deleted to free any resources it held on SQL Server.

Note

The code shown in Listing 4 assumes that the Microsoft.SharePoint.dll assembly is referenced and that the Microsoft.SharePoint, Microsoft.Share-Point.Administration, and Microsoft.SharePoint.Administration.Backup namespaces have been imported for use by the ExecuteSiteCollectionBackupWithSnapshot method. In addition, you should configure the Visual Studio project containing the ExecuteSiteCollectionBackupWithSnapshot method to target the .NET Framework 3.5 on an x64 platform.


Listing 4.
public static void ExecuteSiteCollectionBackupWithSnapshot()
{
// As with the ExecuteSiteCollectionBackup method, these variable values
// would normally be supplied to the method rather than set here.
String backupFilename = @"E:\Backup\SampleSiteBackup.bak";
String siteCollectionUrl = @"http://spdev:18380";
Boolean overwriteIfExisting = true;

// These two variables are set with properties from the SPSite object that
// is created. To minimize the size of the using block that follows, the
// variables are scoped here.
SPContentDatabase housingDb;
String rootRelativeUrl;

// Obtain the critical property values that are needed from the SPSite.
using (SPSite siteToBackup = new SPSite(siteCollectionUrl))
{
// The content database that houses the site collection is needed to
// generate a snapshot. The relative site collection URL
// is needed for the eventual unattached DB backup operation.
housingDb = siteToBackup.ContentDatabase;
rootRelativeUrl = siteToBackup.ServerRelativeUrl;
}

// Only Developer and Enterprise versions of SQL Server support snapshots. If
// snapshots aren't supported, the process can't continue.
if (!housingDb.Snapshots.IsSnapshotSupported)
{
throw new NotSupportedException("Snapshots not supported.");
}

// Each SPDatabase-derived type (including SPContentDatabase) has a Snapshots
// collection that serves as the gateway to working with snapshots. Refresh
// the collection and then use it to create a new snapshot.
SPDatabaseSnapshotCollection allSnapshots = housingDb.Snapshots;
allSnapshots.RefreshSnapshots();
SPDatabaseSnapshot hostingDbSnapshot = housingDb.Snapshots.CreateSnapshot();

// Once SQL Server creates the snapshot, it's just like any other database.
// SharePoint's unattached content database functionality is used to attach to
// the snapshot as if it were a read-only content database.
SPContentDatabase snapshotDb = SPContentDatabase.CreateUnattachedContentDatabase
(hostingDbSnapshot.ConnectionString);

// With a valid SPContentDatabase reference, the collection of site collections
// in the snapshot can be referenced and used to perform the site collection backup.
SPSiteCollection sitesInSnapshot = snapshotDb.Sites;
sitesInSnapshot.Backup(rootRelativeUrl, backupFilename, overwriteIfExisting);

// When the backup operation is over, delete the snapshot to instruct SQL Server
// to release the resources associated with it.
hostingDbSnapshot.Delete();
}



A parting word of caution regarding the use of snapshots is warranted. Listing 4 includes a call to the RefreshSnapshots method to ensure that the collection of SPDatabaseSnaphot objects is current prior to any collection manipulation activities. This is done because there are regular processes within SharePoint Foundation, such as the Microsoft SharePoint Foundation Snapshot Management timer job, that can create and delete database snapshots. Any SPDatabaseSnapshotCollectionEnabledManagement property value of true is subject to regular snapshot maintenance by SharePoint, and this that has an maintenance can lead to the addition and deletion of snapshots during the execution of your code. For this reason, it is prudent to refresh the contents of the collection through the RefreshSnapshots method before attempting any manipulation of the collection within your code.

Other -----------------
- SharePoint 2010 : The SharePoint Object Model (part 2) - Export, Import, and Associated Types & Site Collection Backup and Restore
- BizTalk 2010 Recipes : Business Activity Monitoring - Setting Up BAM Alerts
- BizTalk 2010 Recipes : Business Activity Monitoring - Using the BAM Portal
- Exchange Server 2010 : Ensuring Message Integrity (part 3) - Configuring Permissions on Active Directory Objects & Rights Management Services Federation
- Exchange Server 2010 : Ensuring Message Integrity (part 2) - Using TLS and MTLS & Implementing Domain Security
- Exchange Server 2010 : Ensuring Message Integrity (part 1) - Using S/MIME Extensions
- Windows Server 2003 : Designing a Security Infrastructure - Securing a Wireless Network
- Windows Server 2003 : Designing a Security Infrastructure - Planning a Security Update Infrastructure
- Windows Server 2008 : Network Addressing (part 3) - IPv4 to IPv6 Transitional Techniques
- Windows Server 2008 : Network Addressing (part 2) - Addressing IPv6
- Windows Server 2008 : Network Addressing (part 1) - Addressing and Subnetting IPv4
- Exchange Server 2010 : Implementing Compliance (part 4) - Implementing a Discovery Search & Creating and Configuring Ethical Walls
- Exchange Server 2010 : Implementing Compliance (part 3) - Using MailTips
- Exchange Server 2010 : Implementing Compliance (part 2) - Configuring Journaling
- Exchange Server 2010 : Implementing Compliance (part 1) - Configuring IRM
- Windows Server 2003 : Troubleshooting Name Resolution
- Windows Server 2003 : Planning DNS Security
- Windows Server 2003 : Implementing a NetBIOS Name Resolution Strategy
- BizTalk 2010 Recipes : Business Activity Monitoring - Deploying BAM Activities and Views
- BizTalk 2010 Recipes : Business Activity Monitoring - Creating BAM Activities and Views
 
 
Most view of day
- Using Microsoft SharePoint with Microsoft Dynamics CRM Functions (part 2) - Displaying Data Using BDC in Microsoft Office SharePoint Server
- How to Use System Restore, How to Troubleshoot Bluetooth Problems
- Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Sending email using Outlook
- Windows Server 2008 : Promoting a Domain Controller with dcpromo
- Windows Phone 8 : Phone-Specific Design (part 2) - Using the Panorama Control in Blend
- Microsoft Exchange Server 2007 : Understanding the Client Access Server (part 4) - Availability Service, POP and IMAP
- Microsoft Exchange Server 2007 : Leveraging the Capabilities of the Outlook Web Access Client - Understanding OWA Security Features, Tips for OWA Users with Slow Access
- Windows Server 2003 on HP ProLiant Servers : Assessment of the Enterprise - Conducting the Assessment
- Microsoft Visio 2010 : Sharing and Publishing Diagrams - Publishing Visio Drawings to SharePoint 2010 Visio Services
- SharePoint 2010 : Packaging and Deployment Model - Features (part 1) - Feature Designer
Top 10
- Windows Phone 8 : Scheduled Tasks - Scheduled Task API Limitations
- Windows Phone 8 : Scheduled Tasks - Updating Tiles Using a Scheduled Task Agent
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 5) - Editing an Existing To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 4) - Creating the To-Do Item Shell Tile, Saving a To-Do Item
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 3) - Debugging Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 2) - TodoService, TodoItemViewModel
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 1) - TodoItem,TodoDataContext
- Windows Phone 8 : Scheduled Tasks - Using Scheduled Tasks
- Windows Phone 8 : Scheduled Tasks - Background Agent Types
- Windows Phone 8 : Windows Phone Toolkit Animated Page Transitions - Reusing the Transition Attached Properties
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro