MySqlBackup.NET

repository·master·Indexed 43 days ago

https://github.com/mysqlbackupnet/mysqlbackup.net

A .NET library providing programmatic MySQL backup and restore capabilities as a code-based alternative to MySqlDump. Supports MySqlConnector, MySQL .NET Connector, and Devart Express Connector.

Tokens
1.1K
Snippets
4
Records
5
Agent score
5%

What's inside MySqlBackup.NET

  1. Install MySqlBackup.NET via NuGet

    master

    MySqlBackup.NET requires a specific NuGet package depending on which MySQL connector you are using in your project. Choose the package that matches your existing database driver:

    • For MySqlConnector (MIT): Use MySqlBackup.NET.MySqlConnector.
    • For MySQL .NET Connector (Oracle/GPL): Use MySqlBackup.NET.
    • For Devart Express Connector (Custom): Use MySqlBackup.Net.DevartExpress.
    # For MySqlConnector
    PM> Install-Package MySqlBackup.NET.MySqlConnector
    
    # For MySQL .Data Connector
    PM> Install-Package MySqlBackup.NET
    
    # For Devart Express Connector
    PM> Install-Package MySqlBackup.Net.DevartExpress
  2. Configure MySqlBackup.NET for Unicode and Compatibility

    master

    When configuring your connection and backups, follow these best practices for data integrity:

    • Character Sets: Always use utf8mb4 (or utf8 for older MySQL versions) to ensure full Unicode support.
    • Datetime Compatibility: Include convertzerodatetime=true in your connection string to handle null datetime values correctly.
  3. Restore a MySQL database from a file

    master

    To restore a database, initialize MySqlBackup with a MySqlCommand from your connection and call ImportFromFile(filePath) after opening the connection.

    string constr = "server=localhost;user=root;pwd=1234;database=test1;convertzerodatetime=true;";
    string filePath = @"C:\backup.sql";
    
    using (MySqlConnection conn = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = conn.CreateCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                conn.Open();
                mb.ImportFromFile(filePath);
            }
        }
    }
  4. Backup a MySQL database to a file

    master

    To perform a backup, create a MySqlBackup instance by passing a MySqlCommand object initialized from your connection. Open the connection and call ExportToFile(filePath) to save the database contents to a .sql file.

    string constr = "server=localhost;user=root;pwd=1234;database=test1;convertzerodatetime=true;";
    string filePath = @"C:\backup.sql";
    
    using (MySqlConnection conn = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = conn.CreateCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                conn.Open();
                mb.ExportToFile(filePath);
            }
        }
    }
  5. Supported MySQL Connectors and Dependencies

    master

    MySqlBackup.NET is compatible with the following connectors. You must have one of these installed in your project to use the library.

    | Connector | Source | License | DLLs |
    |---|---|---|---|
    | **MySqlConnector** | [MySqlConnector](https://mysqlconnector.net/) | MIT | `MySqlConnector.dll` |
    | **MySql.Data** | [MySQL Connector/Net](http://www.mysql.com/downloads/connector/net/) | GPL | `MySql.Data.dll` |
    | **Devart Express** | [dotConnect for MySQL](https://www.devart.com/dotconnect/mysql/) | Custom | `Devart.Data.dll`, `Devart.Data.MySql.dll` |