1

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?

trailmax
  • 34,305
  • 22
  • 140
  • 234

1 Answers1

-1

one workaround is initialize ravendb(properly in Application_Start) with a condition to not let ravendb start.

if (WebConfigurationManager.AppSettings["RavenDBStartMode"]=="Start")
    RavenDB.initialize();

for restoring db change "RavenDBStartMode" to "DontStart" in webconfig so application pool will restart and start your restore operation

string dbLocation = Server.MapPath("your database location");
string backupLocation = Server.MapPath("your backup location");

System.IO.File.Delete(dbLocation);

DocumentDatabase.Restore(new RavenConfiguration() { DataDirectory = dbLocation }
, backupLocation, dbLocation, x => { }, false);

RavenDB.initialize();

finally change your "RavenDBStartMode" to "Start" again so ravendb can start for other restart reasons

raoof hojat
  • 355
  • 4
  • 12
  • That's poor. Involves manual restarting of the web-site, manual update of `web.config`. Instead I can stop the site, delete the data, then do the restore. What is the point? – trailmax Nov 20 '14 at 10:22
  • it doesnt need manual restarting website or manual delete old data file, it just need manual updating web.config from a ftp client, i know its ugly by i couldnt do it in simpler way – raoof hojat Nov 20 '14 at 11:08
  • Yeah, I forgot that update to `web.config` causes site restart. But updating `web.config` is not an option. – trailmax Nov 20 '14 at 11:54