Click here to Skip to main content
15,881,139 members
Articles / Database Development / MySQL
Article

Backup & Restore MySQL database on CentOS (Linux/Ubuntu) in C# with MySqlBackup.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Feb 2022CPOL2 min read 4.1K   74   6   1
Building a .NET Core Linux Project with MySqlBackup.NET
This article explained the steps to build .NET Core in C# for running on CentOS/Linux/Ubuntu to backup & restore of MySQL database.

Introduction

MySqlBackup.NET is an open source project written in C# to perform backup & restore of MySQL database. This article will focus on how this library can be built to run on CentOS (Linux/Ubuntu).

Using the Code

The idea is to build a project in .NET Core and publish it as Linux package.

MySqlBackup.NET is an open source .NET Library (C#/VB.NET) to backup & restore MySQL database.

Here, we’re looking into how MySqlBackup.NET can be run on CentOS/Linux to perform backup & restore of MySQL database.

We can test this out with a simple Console App.

Image 1

Start by creating a .NET Core Console Linux project:

Image 2

Check the project properties, ensure the project is built with .NET Core Framework:

Image 3

Install Nuget Package of MySqlBackup.NET (with MySqlConnector):

Image 4

Image 5

A simple sample code to test out:

C#
static void Main(string[] args)
{
    try
    {
        Console.WriteLine("Enter the MySQL Connection String: ");
        Console.WriteLine();

        string constr = Console.ReadLine();

        Console.WriteLine();
        Console.WriteLine();

        Console.WriteLine(("Begin MySQL backup process..."));

        string folder = Environment.CurrentDirectory;
        string file = Path.Combine(folder, "backup.sql");

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

                    mb.ExportToFile(file);

                    conn.Close();
                }
            }
        }

        Console.WriteLine("MySQL backup file saved at: " + file);
    }
    catch (Exception ex)
    {
        Console.WriteLine();
        Console.WriteLine("Error: " + ex.Message);
    }

    Console.WriteLine();
    Console.WriteLine("Press any key to exit...");

    Console.ReadKey();
}

Next, publish the project for .NET Core (Linux):

There are two ways to build the .NET Core projects for Linux:

  • Method 1: Publish the package with runtime support installed on CentOS
  • Method 2: Publish as “self-contained” binary

Method 1: Publish the Package with Runtime Support Installed on CentOS

First, publish the project.

Image 6

Set the target runtime to linux-x64:

Image 7

In above example, the Linux compiled binaries are located at:

\bin\Release\netcoreapp3.1\publish\linux-x64

Copy the DLL in the above folder and paste it to your CentOS:

At the CentOS, make sure that you have installed Dotnet runtime by the following command:

sudo dnf install dotnet-sdk-<version>

For example:

sudo dnf install dotnet-sdk-3.1
sudo dnf install dotnet-sdk-5.0

Assume that you have copied your .NET Core DLL files at the following folder:

/home/<username>/testapp

Then you can test out your app with the following command:

cd /home/admin/testapp
dotnet ConsoleApp.dll

Method 2: Publish as “self-contained” Binary

.NET Core App can also be run without installing dotnet runtime, which is by compiling the project into a single “self-contained” binary.

Here is the screenshot for publishing the “self-contained” binary:

Image 8

Here is the example of how it looks like after publishing, there is only 1 single file:

Image 9

Copy the compiled binary to CentOS.

At the terminal, browse to the folder that contains the self-contained binary, for example:

cd /home/admin/testapp

Next, is to allow execution permission to the file:

sudo chmod 777 ConsoleApp2

Run the app:

./ConsoleApp2

For more information on publishing “self-contained” binary, read more here.

History

  • 3rd February, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Other Other
Programming is an art.

Comments and Discussions

 
PraiseNice Work! Pin
FretelWEB4-Feb-22 3:51
FretelWEB4-Feb-22 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.