I have a smallish MVC 4 site with a database. In this case it isn't worthy of a dedicated SQL Server, so I decided to try out the new LocalDb database feature.
While the database isn't particularly mission critical, I would like to be able to easily back it up on demand to allow some level of disaster recovery.
So, without further adéu, I give you:
namespace SomeSimpleProject.Controllers { [Authorize(Roles="Admin")] public class BackupController : Controller { public ActionResult BackupDatabase() { var dbPath = Server.MapPath("~/App_Data/DBBackup.bak"); using (var db = new DbContext()) { var cmd = String.Format("BACKUP DATABASE {0} TO DISK='{1}' WITH FORMAT, MEDIANAME='DbBackups', MEDIADESCRIPTION='Media set for {0} database';" , "YourDB", dbPath); db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd);
} return new FilePathResult(dbPath, "application/octet-stream"); } } }