プログラムを使用して単一のサイト コレクションのバックアップと復元を行う
ここでは、プログラムを使用して個々のサイト コレクションのバックアップと復元を実行する方法について説明します。
サイト コレクションのバックアップと復元を実行するには
Visual Studio のプロジェクトに Windows SharePoint Services への参照を追加します。
Microsoft.SharePoint および Microsoft.SharePoint.Administration の using ステートメントを追加します。
ファームとそのサービスのコレクションへの参照を取得するために、以下の行を追加します。
SPFarm myFarm = SPFarm.Local; SPServiceCollection myServices = myFarm.Services;
サイト コレクションのホストとなる Web アプリケーションを発行する Web サービスへの参照を、そのサービスの Guid (Id() プロパティの値) を使用して取得します。
Guid serviceID = new Guid("21d91b29-5c5b-4893-9264-4e9c758618b4"); SPWebService webPubService = (SPWebService)myServices[serviceID];
Web サービスを発行するアプリケーションの Id() がわからない場合は、すべてのサービスを反復処理することで、それらの Name()、TypeName、および Id() をレポートできます。次に例を示します。
foreach (SPService service in myServices) { if (service is SPWebService) { Console.WriteLine("Web service name:" + webService.Name); Console.WriteLine("Web service type:" + webService.TypeName); Console.WriteLine("Web service ID:" + webService.Id); Console.WriteLine(); Console.Readline(); } }
サイト コレクションをホストする Web アプリケーションへの参照を取得します。Web アプリケーションの URL がわかっている場合は、静的な Lookup() メソッドを使用して、参照を取得できます。または、アプリケーションの Id() プロパティの値である、Guid を使用することもできます。次のコードは、2 番目の方法を示しています。
SPWebApplicationCollection myApps = webPubService.WebApplications; Guid appID = new Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37"); SPWebApplication myApp = myApps[appID];
サイト コレクションをホストする Web アプリケーションの Id() がわからない場合は、すべての Web アプリケーションを反復処理することで、それらの Name()、TypeName、および Id() をレポートできます。次に例を示します。
foreach (SPWebApplication app in webApps) { Console.WriteLine("Web application name:" + app.Name); Console.WriteLine("Web application type:" + app.TypeName); Console.WriteLine("Web application ID:" + app.Id); Console.WriteLine(); Console.Readline(); }
サイト コレクションの Web アプリケーションのコレクションへの参照を取得します。
SPSiteCollection mySiteCols = myApp.Sites;
サイト コレクションをバックアップするには、Backup() メソッドを呼び出します。以下をパラメータとして渡します。
サイト コレクションの完全な URL (トップレベル Web サイトの完全な URL)。
サイト コレクションの圧縮されたコンテンツを格納するファイルの完全なパスとファイル名。
同じ名前の既存のバックアップ ファイルを上書きする場合は True、上書きしない場合は false。
mySiteCols.Backup(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
サイト コレクションを復元するには、Restore() メソッドを呼び出します。パラメータは Backup() メソッドと同じです。Boolean パラメータは、指定した URL に既存のサイト コレクションがあった場合にそれを上書きするかどうかを示します。
mySiteCols.Restore(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
例
プログラムを使用してサイト コレクションをバックアップまたは復元する方法の簡単な例を次に示します。すべての Guid 値を展開の実際の値に置き換え、Backup メソッドと Restore メソッドに含まれるプレースホルダの値を展開の実際の URL とパスに置き換える必要があります。
// Get a reference to the Web application publishing
// Web service.
SPFarm myFarm = SPFarm.Local;
SPServiceCollection myServices = myFarm.Services;
Guid serviceID = new Guid("21d91b29-5c5b-4893-9264-4e9c758618b4");
SPWebService webPubService = (SPWebService)myServices[serviceID];
// Get a reference to the Web application that hosts the
// site collection.
SPWebApplicationCollection myApps = webPubService.WebApplications;
Guid appID = new Guid("10ea4e6f-ae37-4909-b04f-f516c066bc37");
SPWebApplication myApp = myApps[appID];
// As alternative to the preceding three lines, you can use
// the following when you know the URL of the Web application:
// SPWebApplication myApp = SPWebApplication.Lookup(url_of_Web_app)
// Get a reference to the Web application's collection of
// site collections.
SPSiteCollection mySiteCols = myApp.Sites;
// Back up a specified site collection.
mySiteCols.Backup(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
// Restoring the site collection is identical to the preceding
// code except that the "Restore" is used in place of "Backup".
//
// mySiteCols.Restore(@"https://Server/sites/MySiteCollection", @"\\OtherServer\WSSBackups\SiteCollections\BackupOfMySiteCollection", true);
SPSite クラスに IBackupRestore が実装されておらず、また、Backup() メソッドと Restore() メソッドで Microsoft.SharePoint.Administration.Backup 名前空間の機能が使用されていません。そのため、サイト コレクションのバックアップと復元のレコードは、バックアップ ディレクトリの履歴ファイル (spbrtoc.xml) に保存されません。同様に、バックアップと復元のデータは spbackup.xml ファイルまたは sprestore.xml ファイルに保存されず、サイト コレクションの操作も spbackup.log ファイルまたは sprestore.log ファイルに記録されません。
サイト コレクションのバックアップと復元の操作を何らかの方法でログに記録する場合は、独自のシステムをプログラムする必要があります。システムで作成された spbrtoc.xml,、spbackup.xml、sprestore.xml、spbackup.log、および sprestore.log の各ファイルへの書き込みは、Windows SharePoint Services 3.0 ではサポートされていません。また、それらの移動、削除、名前の変更もできません。ただし、システムで作成されたファイルのデータとサイト コレクションのバックアップおよび復元のデータを結合するファイルを作成できます。
See Also
タスク
[方法] バックアップと復元を実行できるコンテンツ クラスを作成する
参照
Microsoft.SharePoint.Administration.Backup
概念
Windows SharePoint Services のバックアップ/復元オブジェクト モデルを使用したプログラミング