How to restore IIS metadata backup using C #

I found a stack overflow question that describes how to backup the IIS metabase in C # here , and I was successful in getting this to work using the code shown here . However, I am having difficulty restoring these backups (or even any manually created backups in IIS) using C # code.

Does anyone know how to do this, or even if it can be done? I could not find any examples of this on the Internet, unlike the backup itself.

I tried the following code, but I get the error "Exception was selected as the target of the call"

using (DirectoryEntry localhostIIS = new DirectoryEntry("IIS://LocalHost"))
{
   localhostIIS.Invoke("Restore", new object[] { string.Empty, 0, 0});
}

Now that I'm sure that I am calling a method with the wrong name and / or structure of the object, I could not find the correct way to call it anywhere ....

Can someone point me in the right direction?

+3
source share
1 answer

I tried this with a named backup and got this to work with some settings:

const uint MD_BACKUP_HIGHEST_VERSION = 0xfffffffe;
const uint MD_BACKUP_NEXT_VERSION = 0xffffffff;
const uint MD_BACKUP_SAVE_FIRST = 2;

using(DirectoryEntry de = new DirectoryEntry("IIS://Localhost"))
{
  // Backup using the next version number (MD_BACKUP_NEXT_VERSION)
  de.Invoke("Backup", new object[] {
      "test-backup",
      MD_BACKUP_NEXT_VERSION,
      MD_BACKUP_SAVE_FIRST
  });

  // Restore the highest version number (or specify the specific version)
  de.Invoke("Restore", new object[] {
    "test-backup",
    MD_BACKUP_HIGHEST_VERSION,
    0
  });
}
+1
source

Source: https://habr.com/ru/post/1731873/


All Articles