Generate bank backup via application

2

In my project, I connect to the database.

Is there any way I can back up my database via ASP.NET MVC 5 and save that backup directory, either in the project root or in a folder in a more secure place?

The database I use is SQL Server Express 2012 .

    
asked by anonymous 27.11.2014 / 13:28

1 answer

3

Yes, it is possible. You need to mount in your C # code the sql command of backup and run it, something like:

Open the connection and mount a query that executes the backup command according to the path you want to save backup p>

public void ExecutarBackup(string caminho)
{ 
    suaConexao = new SqlConnection(...);
    var query = "Backup database NomeSeuBanco to disk='" + caminho + "'";
    var cmd = new SqlCommand(query, suaConexao);
    cmd.ExecuteNonQuery();
}

Editing / Complement:

  

And what would I do to put it in a view? Like a button? Or something like that.

Your View can have a control for the user informing the path where he wants to save the backup and a button. When you click the button the Action of your Controller occurs, passing the path that the user wants to save the backup as a parameter. p>

This Action can call a service / method to generate the backup .

[HttpPost]
public ActionResult SalvarBakup(SeuViewModel seuViewModel)
{
    _servicoBackup.ExecutarBackup(seuViewModel.Caminho);
    return View();
}

Below I'll explain to View:

@model SeuViewModel

@using (Html.BeginForm("SalvarBakup", "SeuController"))
{
    @Html.TextBoxFor(model => model.Caminho)
    <input type="submit" value="Salvar Backup" />
}
    
02.12.2014 / 14:28