简体   繁体   中英

How to take backup of MySQL Database

  1. I need to take a backup of my entire database using MySql Query

2.Also to take a backup of DB using c# code

My application is an standalone application and using vs2010. This what I have tried so far.Not able to identify what error is thrown.any suggestions would be of help.Any help on this will be of great help

enter code here

        int year = Time.Year;
        int month = Time.Month;
        int day = Time.Day;
        int hour = Time.Hour;
        int minute = Time.Minute;
        int second = Time.Second;
        int millisecond = Time.Millisecond;

        //Save file to C:\ with the current date as a filename
        string path;
        path = "D:\\yourfoldername" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
        StreamWriter file = new StreamWriter(path);


        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "mysqldump";
        psi.RedirectStandardInput = false;
        psi.RedirectStandardOutput = true;
        psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "", "localhost", "database");
        psi.UseShellExecute = false;

        Process process = Process.Start(psi);

        string output;
        output = process.StandardOutput.ReadToEnd();
        file.WriteLine(output);
        process.WaitForExit();
        file.Close();
        process.Close();

enter code here

If it's an entire DB, then:

$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql

If it's all DBs, then:

$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql

If it's specific tables within a DB, then:

$ mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql

You can even go as far as auto-compressing the output using gzip (if your DB is very big):

$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz

If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306):

$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql

To IMPORT:

ype the following command to import sql data file:

$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

In this example, import 'data.sql' file into 'blog' database using vivek as username:

$ mysql -u sat -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:

$ mysql -u username -p -h 202.54.1.10 databasename < data.sql

OR use hostname such as mysql.cyberciti.biz

$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql

If you do not know the database name or database name is included in sql dump you can try out something as follows:

$ mysql -u username -p -h 202.54.1.10 < data.sql

Refer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html

Backing up Database in MySQL using C#

Backup a MySQL database

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();
            }
        }
    }
}

You can do it using mysqldump

$ mysqldump -u [uname] -p[pass] [dbname] > [backupfile.sql]

just have a look at this

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

http://www.mediacollege.com/computer/database/mysql/backup.html

Take a look at this project:

MySqlBackup.NET - MySQL Backup Solution for C#

You do not have to write a lot of code with that. I am commenting an example for you from official documentation:

Simple Export Example

using MySql.Data.MySqlClient;
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();
         }
     }
 }

I hope it helps for future readers.

Backing up Database in MySQL using C#

See this Windows Form

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > " + textBackup.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

textBackup.Text is actually the path with file, ex. e:\\tmp.sql.

IF YOU WANT TO ACCESS AN USER DEFINED PATH THEN WRITE LIKE THIS ----

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

Restore a MySQL database using C#

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < " + textRestore.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

textRestore.Text is actually the path with file, ex. e:\\tmp.sql.

IF YOU WANT TO ACCESS AN USER DEFINED PATH THEN WRITE LIKE THIS ----

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM