I'm trying to do RavenDB backup/restore from within the application. Raven runs in embedded mode. I've got the backups working fine, now I need to do a restore.
Problem with restore that I'd like to restore into the same location that is currently used by embedded storage. My plan was to shut-down embedded storage, delete all the files in the location, restore there:
var configuration = embeddedStore.Configuration;
var backupLocation = // location of a backup folder
var databaseLocation = // location of current DB
// shutdown the storage???
documentStore.Dispose();
// now, trying to delete the directory, I get exception "Can't delete the folder it is in use by other process"
Directory.Delete(databaseLocation, true); // <-- exception here
Directory.CreateDirectory(databaseLocation);
DocumentDatabase.Restore(configuration, backupLocation, databaseLocation, s => { }, defrag: true);
(The full source on GitHub)
The problem with shutting down the storage. From the exception I get, the engine is not shut down, because I can't delete the folder:
The process cannot access the file '_0.cfs' because it is being used by another process.
The application I run is MVC5. Issue that the DB location is set in web.config
and I don't really want to modify it in any way, so the restore has got to go in the same location as existing DB.
What is the correct way to restore embedded database into the same location as the existing DB?