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