C # Back up a MySQL database

Source: Internet
Author: User
Namespace BackupMySQL
{
Class Program
{
Public static void DoBackup ()
{
String [] ary = ReadFromText ();

String host = ary [0];
String port = ary [1];
String user = ary [2];
String password = ary [3];
String database = ary [4];
String fileName = database + "_ bak _" + DateTime. Now. ToString ("yyyyMMddhhmmss ");
String bakPath = ary [5] + "\" + fileName + ". SQL ";
String logPath = ary [6];

String comment STR = "/c mysqldump-h" + host + "-P" + port + "-u" + user + "-p" + password + "" + database +"> "+ bakPath;


Try
{
System. Diagnostics. Process. Start ("cmd", explain Str );
}
Catch (Exception ex)
{
WriteLog (logPath, ex. Message );
}

}

/** // <Summary>
/// Read Configuration
/// </Summary>
/// <Returns> </returns>
Public static string [] ReadFromText ()
{
String FileName = @ "d: \ BackupIni.txt ";
ArrayList list = new ArrayList ();
If (File. Exists (FileName ))
{
StreamReader sr = new StreamReader (FileName, Encoding. Default );
String s = "";
While (s = sr. ReadLine ())! = Null)
{
List. Add (s );
}
}
Return (string []) list. ToArray (typeof (string ));
}

/** // <Summary>
/// Write logs
/// </Summary>
/// <Param name = "filePath"> </param>
/// <Param name = "theStr"> </param>
Public static void WriteLog (string filePath, string theStr)
{
If (! File. Exists (filePath ))
{
File. Create (filePath );
}
StreamWriter sw = new StreamWriter (filePath, true, Encoding. Default );
Sw. WriteLine (theStr );
Sw. Flush ();
Sw. Close ();
}

Static void Main (string [] args)
{
DoBackup ();
}
}
}

 

Backup: mysqldump-hlocalhost-P3306-uroot-p123 -- default-character-set = utf8 mydata> d: \ test \ mydata. SQL

Restore: mysql-hlocalhost-P3306-uroot-p123 mydata <d: \ test \ mydata. SQL

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.