Multiple database backup

Jun 11, 2013 at 1:20 PM
Is that possible to create a backup from every database on a server?
Coordinator
Jun 11, 2013 at 11:50 PM
You can do this:
DataTable dt = new System.Data.DataTable();
string con = "server=localhost;user=root;pwd=1234;";

using (MySqlConnection conn = new MySqlConnection(con))
{
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = conn;
    conn.Open();

    cmd.CommandText = "show databases;";
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    da.Fill(dt);
}

foreach (DataRow dr in dt.Rows)
{
    string constr = "server=localhost;user=root;pwd=1234;database=" + dr[0] + ";";
    string file = "C:\\backup\\" + dr[0] + ".sql";

    MySqlBackup mb = new MySqlBackup();
    mb.ExportInfo.FileName = file;
    mb.Export();
}
Jun 12, 2013 at 6:40 AM
Edited Jun 12, 2013 at 9:51 AM
Thanks for the help and your work on this great lib.

btw, here is my solution (partly borrowed from your idea):
public void MySqlServerBackup(string connectionString, string targetDirectory)
{
    using (var connection = new MySqlConnection(connectionString))
    using (var da = new MySqlDataAdapter("show databases", connection))
    {
        var dt = new DataTable();
        da.Fill(dt);

        if (dt.Rows.Count == 0)
            return;

        if (!Directory.Exists(targetDirectory))
            Directory.CreateDirectory(targetDirectory);

        foreach (DataRow dr in dt.Rows)
        {
            if (connection.State != ConnectionState.Open)
                connection.Open();

            connection.ChangeDatabase(dr.Field<string>(0));

            using (var mb = new MySqlBackup(connection))
            {
                mb.ExportInfo.FileName = Path.Combine(targetDirectory, Path.ChangeExtension(connection.Database, ".sql"));
                mb.Export();
            }
        }
    }
}