プログラムを使用してコンテンツを復元する

ここでは、Windows SharePoint Services 3.0 コンテンツ コンポーネントをバックアップから復元するアプリケーションの作成方法について説明します。このトピックでは、「Windows SharePoint Services でのデータのバックアップと復元の概要」および「Windows SharePoint Services のバックアップ/復元オブジェクト モデルを使用したプログラミング」の内容を理解していることを前提としています。

コンテンツ コンポーネントを復元するには

  1. Visual Studio のプロジェクトに Windows SharePoint Services への参照を追加し、コード ファイルに Microsoft.SharePoint.Administration 名前空間と Microsoft.SharePoint.Administration.Backup 名前空間の using ステートメントを追加します。

  2. Main メソッド内で、静的な GetRestoreSettings メソッドを使用して SPRestoreSettings オブジェクトを作成します。1 つ目のパラメータでは、バックアップの格納先のパスを渡します。 2 つ目のパラメータでは、SPRestoreMethodType のいずれかの値の文字列バージョンを渡します。

    SPRestoreSettings settings = SPBackupRestoreSettings.GetRestoreSettings((@"\\Server\WSSBackups", "Overwrite");
    
  3. 復元するコンテンツ コンポーネントを指定するようにユーザーに要求し、その名前を IndividualItem プロパティに割り当てます。前回の完全バックアップに含まれていた、バックアップ操作の対象にできるファーム内のコンポーネントの名前のリストを表示するには、サーバー コマンド ラインで stsadm -o restore -showtree コマンドを実行します。別の完全バックアップ パッケージを指定するには、-backupid パラメータを使用します。また、サーバーの全体管理アプリケーションで、[サーバー構成の管理] から [復元の実行] にアクセスすることもできます。ファーム全体を指定するには、名前で "Farm" を使用しますプロパティを null に設定した場合も、バックアップのファーム全体が選択されます。ただし、以降のすべてのコードで、バックアップするコンポーネントを IndividualItem を使用して名前で識別することが前提になります。例については、手順 9. の FindItems() メソッドの使用方法を参照してください)。

    Console.Write("Enter name of component to restore (default is whole farm):");
    settings.IndividualItem = Console.ReadLine();
    
  4. 最新のバックアップ以外のバックアップから復元する場合、BackupId プロパティにその GUID を割り当ててバックアップ パッケージを識別します。特定のバックアップ場所に対するバックアップ操作ごとのレコードは、その場所のルートの spbrtoc.xml に保存されます。個々のバックアップおよび復元操作は、ファイル内の <SPHistoryObject> 要素で表されます。 操作がバックアップの場合、<SPHistoryObject> 要素の子の <IsBackup> が "True" になります<SPHistoryObject> 要素の <SPId> 要素には、バックアップの GUID が含まれます。

    注意

    バックアップ操作と復元操作の全リストをプログラムによって取得するには、GetHistory() メソッドを使用します。このメソッドは、SPBackupRestoreHistoryObject オブジェクトを含む SPBackupRestoreHistoryList オブジェクトを返します。後者のオブジェクトはそれぞれ操作を表し、SelfId プロパティにその操作の GUID が保持されます。

    settings.BackupId = new Guid("GUID");
    
  5. 必要に応じて、IsVerbose プロパティと UpdateProgress プロパティのどちらかまたは両方を設定します (これらのプロパティの詳細については、それぞれのリファレンス トピックを参照してください)。

    settings.IsVerbose = true;
    settings.UpdateProgress = 10;
    
  6. 必要に応じて、FarmAdminLoginName プロパティと FarmAdminLoginPassword() プロパティを設定します。

    settings.FarmAdminLoginName = "Bob";
    settings.FarmAdminPassword = "7*j2U";
    
  7. CreateBackupRestore() メソッドを使用して復元操作を作成します (操作の履歴オブジェクトも作成されます)。

    Guid restore = SPBackupRestoreConsole.CreateBackupRestore(settings);
    
  8. UI でユーザーにコンポーネント名をリストから選択させるのではなく入力させる場合は、入力された名前がいずれかのコンポーネントと正確に一致することを確認する必要があります。

    SPBackupRestoreObject node = EnsureUniqueValidComponentName(settings, ref restore);
    
  9. 以下の EnsureUniqueValidComponentName メソッドの実装を追加します。FindItems() メソッドを使用して、ユーザーが入力した名前と名前が一致するコンテンツ オブジェクトのコレクションを取得します。一致する名前がない場合は、再度実行するようにユーザーに要求します。一致する名前が複数ある場合は、いずれかを指定するようにユーザーに要求します。ユーザーが入力したコンポーネント名が有効で特定できた場合は、ユーザーが復元するコンポーネントを表す SPBackupRestoreObject オブジェクトへの参照を取得します。

    private static SPBackupRestoreObject EnsureUniqueValidComponentName(SPBackupRestoreSettings settings, ref Guid operationGUID)
    {
        SPBackupRestoreObjectCollection list = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem);
        SPBackupRestoreObject component = null;
    
        if (list.Count <= 0)
        {
            Console.WriteLine("There is no component with that name. Run again with a new name.");
            Console.WriteLine("Press Enter to continue.");
            Console.ReadLine();
        }
        else if (list.Count > 1)  // The component name specified is ambiguous. Prompt user to be more specific.
        {
            Console.WriteLine("More than one component matches the name you entered.");
            Console.WriteLine("Run again with one of the following:");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine("\t{0}", list[i].ToString());
            }
            Console.WriteLine("Press Enter to continue.");
            Console.ReadLine();
        }
        else
        {
            component = list[0];
        }
    
        return component;
    
    }
    
  10. Main メソッドで、EnsureUniqueValidComponentName メソッドが有効なノードを返した場合にのみ実行される条件構造を作成します。

    if (node != null)
    {
        // TODO: Set the restore operation as the active operation
        // and run it.
    }
    
  11. 前の手順の "TODO" 行を次のコードに置き換えます。これにより、操作が **SetActive()**メソッドによってアクティブな操作として設定され、操作が成功するかどうかを確認するためにテストが実行されます。別のバックアップ操作または復元操作が既に実行中である場合など、操作に失敗した場合は、アプリケーションの UI でエラーを報告します。

    if (SPBackupRestoreConsole.SetActive(restore) == true)
    {
        // TODO: Run the operation. See next step.
    }
    else
    {
        // Report through your UI that another backup
        // or restore operation is underway. 
        Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.");
    }
    
  12. SetActive() 呼び出しが成功した場合に実行されるコード分岐で、Run() メソッドを使用して操作を実行します。操作が成功するかどうかをテストします。失敗した場合、操作が失敗したことを示すメッセージを UI で報告します。前の手順の "TODO" 行を次のコードに置き換えます。

    if (SPBackupRestoreConsole.Run(restore, node) == false)
    {
        // Report "error" through your UI.
        String error = SPBackupRestoreConsole.Get(restore).FailureMessage;
        Console.WriteLine(error);
    }
    
  13. Remove() メソッドを使用して復元をクリーンアップします。手順 10. で挿入した閉じかっこの直前に以下のコードを追加します。

    // Clean up the operation.
    SPBackupRestoreConsole.Remove(restore);
    
    Console.WriteLine("Restore attempt complete. Press Enter to continue.");
    Console.ReadLine();
    

次のコードは、コンテンツ コンポーネントの復元をプログラムする方法を示しています。プレースホルダ \\Server\WSSBackups を、バックアップ場所のパスに置き換えます。実行時に、その場所の最新のバックアップが自動的に検索されます。

using System;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Backup;

namespace MyCompany.SharePoint.Administration.Backup
{
    class Restore
    {
        static void Main(string[] args)
        {
            // Create the restore settings.
            SPRestoreSettings settings = SPBackupRestoreSettings.GetRestoreSettings(@"\\Server\WSSBackups", "Overwrite");

            // Identify the content component to restore.
            Console.Write("Enter name of component to restore (default is whole farm):");
            settings.IndividualItem = Console.ReadLine();
            
            // Set optional operation parameters.
            settings.IsVerbose = true;
            settings.UpdateProgress = 10;
            
            // Create the restore operation and return its ID.
            Guid restore = SPBackupRestoreConsole.CreateBackupRestore(settings);

            SPBackupRestoreObject node = EnsureUniqueValidComponentName(settings, ref restore);

            if (node != null)
            {
                // Set the restore as the active job and run it.
                if (SPBackupRestoreConsole.SetActive(restore) == true)
                {
                    if (SPBackupRestoreConsole.Run(restore, node) == false)
                    {
                        // Report "error" through your UI.
                        String error = SPBackupRestoreConsole.Get(restore).FailureMessage;
                        Console.WriteLine(error);
                    }
                }
                else
                {
                    // Report through your UI that another backup
                    // or restore operation is underway. 
                    Console.WriteLine("Another backup or restore operation is already underway. Try again when it ends.");
                }

                // Clean up the operation.
                SPBackupRestoreConsole.Remove(restore);

                Console.WriteLine("Restore attempt complete. Press Enter to continue.");
                Console.ReadLine();
            }
        }// end Main

        private static SPBackupRestoreObject EnsureUniqueValidComponentName(SPBackupRestoreSettings settings, ref Guid operationGUID)
        {
            SPBackupRestoreObjectCollection list = SPBackupRestoreConsole.FindItems(operationGUID, settings.IndividualItem);
            SPBackupRestoreObject component = null;

            if (list.Count <= 0)
            {
                Console.WriteLine("There is no component with that name. Run again with a new name.");
                Console.WriteLine("Press Enter to continue.");
                Console.ReadLine();
            }
            else if (list.Count > 1)  // The component name specified is ambiguous. Prompt user to be more specific.
            {
                Console.WriteLine("More than one component matches the name you entered.");
                Console.WriteLine("Run again with one of the following:");
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine("\t{0}", list[i].ToString());
                }
                Console.WriteLine("Press Enter to continue.");
                Console.ReadLine();
            }
            else
            {
                component = list[0];
            }

            return component;

        }// end EnsureUniqueValidComponentName

    }// end Restore class
}// end namespace

See Also

タスク

[方法] プログラムを使用してコンテンツをバックアップする

[方法] プログラムを使用して単一のサイト コレクションのバックアップと復元を行う

[方法] バックアップと復元を実行できるコンテンツ クラスを作成する

[方法] STSADM ユーティリティを拡張する

参照

Microsoft.SharePoint.Administration.Backup

Backup

Restore

概念

Windows SharePoint Services のバックアップ/復元オブジェクト モデルを使用したプログラミング

その他のリソース

Stsadm コマンド ライン ツール (Office SharePoint Server)