RavenDB importing data to an existing database with C#

149 views Asked by At

I need to chain some operations in order to migrate unencrypted data to an encrypted database:

  1. backup a database (BackupOperation)
  2. delete the database (DeleteDatabasesOperation)
  3. create an encrypted database with the same name (CreateDatabaseOperation)
  4. importing the ravendump's data to the new database (RestoreBackupOperation)

Everything works fine exept for the RestoreBackupOperation, which gives me this error :

Raven.Client.Exceptions.RavenException: System.ArgumentException: Cannot restore data to an existing database named MyDatabaseName

So obviously the RestoreOperation also creates the database and it conflicts with the one created the step before. I could do it using the UI, but the problem is that I need to import the data for 250+ databases, so it needs to be scripted.

I did not find anything in RavenDB's documentation regarding the import of datas from a ravendump file programatically. Is there any way for this process to work?

Here's the code :

private static async Task RestoreDatabase(string tenantName, IDocumentStore store, string backupLocaltion)
        {
            var config = new RestoreBackupConfiguration
            {
                DatabaseName = tenantName,
                BackupLocation = backupLocaltion
            };
            await store.Maintenance.Server.SendAsync(new RestoreBackupOperation(config));
        }

It works as long as the database does not exist to begin with.

1

There are 1 answers

0
UmbrellaCorpAgent On BEST ANSWER

Turns out RevenDB offers a Smuggler object to do just that :

var options = new DatabaseSmugglerImportOptions()
            {
                OperateOnTypes = DatabaseItemType.Documents | DatabaseItemType.DatabaseRecord | DatabaseItemType.Attachments | 
                                 DatabaseItemType.RevisionDocuments | DatabaseItemType.LegacyAttachmentDeletions 
                                 | DatabaseItemType.LegacyAttachments | DatabaseItemType.LegacyDocumentDeletions | DatabaseItemType.Identities
            };
            var operation = await store.Smuggler.ForDatabase(tenantName).ImportAsync(options, backupLocation);
            await operation.WaitForCompletionAsync(new TimeSpan(1,0,0));

Using this operation I'm able to import data to the encrypted database