Generate database backup via application

Asked

Viewed 628 times

2

In my project I connect to the database.

Is there any way I can do the backup from my database via Asp.net mvc 5 and keep that backup in a directory, either at the project root or in a folder in a safer place?

The database I use is the SQL Server Express 2012.

  • Related: http://answall.com/questions/39978/backup-database-sqlserver

  • 1

    I believe that if we base the question linked by @gmsantos (as well as the answer given there), just call those commands with the method Sqlcommand.Executenonquery. But I’m not 100% sure. If someone checks that this is correct and posts as an answer, they will have my +1.

  • Doing this by code c# ? Because what I saw in the answer is only by SQL Server itself.

  • Anyone else? Is there then any way to make a script and put in the project and run that script via application ?

  • I wanted to understand why an application has to generate and restore the backup of a database. It doesn’t make any sense to me.

  • I don’t need to restore, only generate. I don’t know either, but if the client asked, I’ll put right. I even understand. Even if the bank creates, and this is automated, it would be one more way to generate the backup if it doesn’t work the other way.

  • And how do I restore or use a certain backup but by the view also?

Show 2 more comments

1 answer

3


It is possible yes. 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 which executes the command to do the backup as the desired way to save your backup

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

Edition/Complement:

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

Your View can have a control for the user to inform the path in which he wants to save the backup and a button. When clicking the button occurs the Post for a Action of your Controller, passing the path the user wants to save the backup as a parameter.

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

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

Below exemplifies the View:

@model SeuViewModel

@using (Html.BeginForm("SalvarBakup", "SeuController"))
{
    @Html.TextBoxFor(model => model.Caminho)
    <input type="submit" value="Salvar Backup" />
}
  • And how would I put in a view ? Like a button? Or something.

  • Here: model => model. Path, I would have to create a table ? Or it already recognizes the variable that is in Action ?

  • It would have looked like this post instead of the user typing the walks, he choose where he wants to save ? Type when downloading a PDF, for example ?

  • The View is typed (in the example above it is of the Seuviewmodel type). Seuviewmodel is a class that represents your Model. Ideally, the properties that are in the View should be in this class. model => model.Path has no relation to table creation, it just means that a textbox control will be rendered in the View and its value will be sent in the post. Give studied on View Model Pattern. I took the opportunity to edit and make the answer clearer.

  • Got it. Anyway, I’d have to have a Model with the Path attribute and set a value on it. Right ?

  • It can be done without a Model. But by following the structure of Asp.Net MVC the correct is that you have the Model class yes.

  • Now yes ! I must edit the question and put an extra tip there. But thanks for the answer ! If I make a mistake, I warn you !

  • I am having permission error to access any directory and save the .bak... How do I do ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.