10

I created a Winforms in order to backup my Database. Then When I run my program it gives an Win32Exception was unhandled. "The system cannot find the file specified" Although the file is already existed and resulted to that exception.

Here is my code regarding my problem

using System.Diagnostics;

private void btnProceed_Click(object sender, EventArgs e)
{
            path = @"D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"";
            Process p = new Process();
            p.StartInfo.FileName = path;
            p.Start();
}
Bon
  • 309
  • 1
  • 5
  • 16

5 Answers5

39

You can use MySqlBackup.NET as alternative to MySqlDump
Documentation:
http://www.codeproject.com/Articles/256466/MySqlBackup-NET-MySQL-Backup-Solution-for-Csharp-V
https://github.com/MySqlBackupNET/MySqlBackup.Net

Sample codes:

Backup a MySQL database

using MySql.Data.MySqlClient; 

then the code,

private void Backup()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ExportToFile(file);
                conn.Close();
            }
        }
    }
}


Restore a MySQL database

private void Restore()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ImportFromFile(file);
                conn.Close();
            }
        }
    }
}

Update:
I am one of the author of this library.

mjb
  • 7,649
  • 8
  • 44
  • 60
  • Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Sep 07 '12 at 05:06
  • By disclaimer, I mean about the fact that you are posting about your own product/website; what you added at the bottom in the 'Update'. – Andrew Barber Sep 10 '12 at 12:56
  • how can it be made to backup at regular intervals, say once everyday – Smith Dec 03 '17 at 10:02
  • @Smith Schedule task – mjb Dec 03 '17 at 14:30
  • If you have large database, mysqlcommand will give you timeout exception. You can increase it's timeout limit by MySqlCommand cmd = new MySqlCommand(); cmd.CommandTimeout = 600; – Hassan Rahman Jul 23 '19 at 17:43
  • @HardikVinzava Yes, it is possible. But you have to modify all your tables to include "update_time". Then you have to cache the "last_backup_time" in a text file in your application folder. Then you will export all rows that have the "update_time" older than the "last_backup_time". Apply the option of `MySqlBackup.BackupInfo.ExportRowsMode = Replace` – mjb Jan 04 '20 at 15:08
  • Hi @mjb Thanks for your reply. Actually I don't have any control on adding new columns (as it's a third-party system) on the existing system. Do you have any other suggestions? Or Can we use this https://dev.mysql.com/doc/mysql-enterprise-backup/4.1/en/mysqlbackup.incremental.html into your code? – Hardik Vinzava Jan 06 '20 at 04:57
  • @HardikVinzava I'm unsure how to do this at the momoent. – mjb Jan 07 '20 at 02:01
  • Why are you closing the connection manually? By using the "using statement" the connection is automatically closed. – JoeyB May 22 '20 at 11:19
  • 1
    Can you make a version of this library that doesn't use the Oracle GPL client? As a result your library is also GPL! – A X Aug 15 '20 at 23:59
  • 1
    @Abr I will try to have a look on this – mjb Aug 27 '20 at 12:43
0

I believe you have to mention the user, pwd, db name and the target path..

string path = @"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > " + txtBoxDBName.Text + @".sql"; 

backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

restore:# mysql -u root -p[root_password] [database_name] < dumpfilename.sql

http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/

hagensoft
  • 1,497
  • 13
  • 13
0

You may try this one.

public void BackUpData(string file)
{
    using MySqlConnection con = new MySqlConnection { ConnectionString = config };
    using MySqlCommand cmd = new MySqlCommand { Connection = con };
    using MySqlBackup mb = new MySqlBackup { Command = cmd };

    try
    {
        con.Open();
    }
    catch(MySqlException ex)
    {
        msgErr(ex.Message + " connection error.");
        return;
    }

    try
    {
        mb.ExportToFile(file);
    }
    catch(MySqlException ex)
    {
        msgErr(ex.Message + " sql query error.");
    }
}
Hille
  • 2,123
  • 22
  • 39
Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
0
  • Don't putt the whole call inside "path = ", you should use "Arguments" to specify arguments, as name says. If library checks for presence of the called file (your whole path) it shouldn't find it!
  • Are you sure that path is correct? You should find MySQL Server path using registry, not hard-coding the path, or if it can be not easy for you you can pass it as an argument from command line or specify from your form (settings page).
  • You may have missed credentials: -u should be used for username (even if I use --user) and -p should be for password (even if I use --password). Why do you pass "txtBoxDBName.Text" as password?!
  • Maybe your destination path is invalid: it contains spaces, if you use spaces you should use quotes.
  • What does txtBoxDBName.Text (?password?) contains? Spaces too? If yes it doesn't work.
  • Last presence of + @"" is completely useless, it doesn't insert any quotes.

A correct version of your code with quotes corrected is: path = @"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u " + txtBoxDBUsername.Text + @" -p " + txtBoxDBName.Text + @" > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup\" + maskeTxtBoxDBFile.Text + @"""";

For more readability: path = $@"""D:\MySQL\MySQL Server 5.5\bin\mysqldump.exe"" -u {txtBoxDBUsername.Text} -p {txtBoxDBName.Text} > ""D:\C#\Client\Salesmate - EMC\SalesMate\Backup{maskeTxtBoxDBFile.Text}""";

tedebus
  • 978
  • 13
  • 20
0
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\xampp\mysql\bin\mysql.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "password", "localhost", "your_dbname");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.StandardInput.Write(File.ReadAllText(inputFilePath));
process.StandardInput.Close();
process.WaitForExit();
process.Close();

This one worked for me you can try it out as long as you've your backed up .sql file

piertoni
  • 1,933
  • 1
  • 18
  • 30