0

Is it possible to periodically backup my MySQL Database from my application (C#). I will create the application as a Windows Service or Console app and schedule using Windows Task Scheduler.

1 Answer 1

2

There is an awesome Library available to Backup and Restore MySQL database in C#.

The code is available on GitHub and can also be added as a Nuget package as well.

Use the below code to do a backup.

string connstr = "server=localhost;user=root;pwd=mypass;database=mydb;sslmode=none;convertdatetime=true;";
string backupfile = "C:\\backup.sql";

using (MySqlConnection conn = new MySqlConnection(connstr))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            conn.Open();
            cmd.Connection = conn;

            mb.ExportToFile(backupfile);

            conn.Close();
        }
    }
}

Backup will be saved in C:\backup.sql

Sign up to request clarification or add additional context in comments.

1 Comment

That's a nice way to accomplish the task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.