c# Process cmd 執行完回撥 Proc_OutputDataReceived mysql mysqldump mysql source備份還原資料
c# Process 執行完回撥 Proc_OutputDataReceived
mysql mysqldump mysql source備份還原資料
直接貼程式碼
前提:mysql5.7
vs2017 3.5
mysql資料夾 C:\Program Files\MySQL\MySQL Server 5.7\bin
備份資料夾 D:\Backup\2018\201812\20181217
還原:
備份:
直接上程式碼
class Program
{
static void Main(string[] args)
{
string cmdRestore = "source D:\\Backup\\2018\\201812\\20181217\\ib_response201812171400.sql";
///還原
StartCmdRestore(strMysqlFile, new string[] { cmdRestore }, "ib_response201812171400");
//備份
//string cmdBackUp = "mysqldump -hlocalhost -uroot -proot ibsdk_qsmessage_bak ib_response201812171400 > d:\\123.sql";
//StartCmd(strMysqlDumpFile, new string[] { cmdBackUp });
Console.Read();
}
static string strMysqlRoot = "root";
static string strMysqlPassword = "root";
static string strMysqlDataBaseNameBAK = "ibsdk_qsmessage_bak";
static string CmdPath = @"C:\Windows\System32\cmd.exe";
static string strMysqlDumpFile = "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\";
static string strMysqlFile = @"C:\Program Files\MySQL\MySQL Server 5.7\bin\";
static int cntMM = 0;
static string CurrentRestoreTableName = string.Empty;
private static string DATABASE_PROVIDER = "MySql.Data.MySqlClient";
private static string DATABASE_CONSTRING_BAK = "Data Source=localhost;Initial Catalog=ibsdk_qsmessage_bak;Persist Security Info=True;User ID=root;Password=root;Min Pool Size=1;Max Pool Size=3;Allow User Variables=True;Allow Zero Datetime = true;";
/// <summary>
/// 執行Cmd命令
/// </summary>
/// <param name="workingDirectory">要啟動的程序的目錄</param>
/// <param name="command">要執行的命令</param>
public static void StartCmd(string workingDirectory, string[] commands)
{
Process process = new Process();
process.StartInfo.FileName = CmdPath;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += Process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
//process.StandardInput.WriteLine("cd " + workingDirectory);
process.StandardInput.WriteLine(commands[0]);
process.StandardInput.WriteLine("exit");
process.StandardInput.WriteLine("exit");
process.Close();
}
private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if(e != null && e.Data != null)
Console.WriteLine("out:" + e.Data.ToString());
//Console.WriteLine("aaa:" + cnt);
}
/// <summary>
/// 執行Cmd命令
/// </summary>
/// <param name="workingDirectory"></param>
/// <param name="command"></param>
/// <param name="tableName"></param>
public static void StartCmdRestore(string workingDirectory, string[] command, string tableName)
{
//建立程序物件
Process proc = new Process();
//呼叫dos視窗
proc.StartInfo.FileName = "cmd.exe";
//不顯示窗體
proc.StartInfo.CreateNoWindow = true;
//設定dos視窗的目錄路徑,這裡就是自己安裝mysql的bin目錄
proc.StartInfo.WorkingDirectory = workingDirectory;
//允許輸入流
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.EnableRaisingEvents = true;//程式退出引發事件
proc.OutputDataReceived += Proc_OutputDataReceived;
//執行
proc.Start();
proc.BeginOutputReadLine();
//登陸資料庫,這裡的內容和我們直接使用dos視窗登陸資料庫的方式一致
proc.StandardInput.WriteLine(string.Format("mysql -u{0} -p{1}", strMysqlRoot, strMysqlPassword));
//切換到我們需要操作的資料庫
proc.StandardInput.WriteLine(string.Format("use {0};", strMysqlDataBaseNameBAK));
//先前備份的sql指令碼檔案讀取
proc.StandardInput.WriteLine(command[0]);// "source D:\\Backup\\2018\\201812\\20181216\\ib_response201812161030.sql");
CurrentRestoreTableName = tableName;
proc.StandardInput.WriteLine("exit");
proc.StandardInput.WriteLine("exit");
proc.Close();
}
private static void Proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
//Console.WriteLine("out");
if(e != null && e.Data != null)
Console.WriteLine("out:" + e.Data.ToString());
if(cntMM >= 1)
{
//string strShowTables1 = "select count(1) from ib_response201812171400;";
//int cnt = SessionFactory.GetSessionMySQLIB(DATABASE_PROVIDER,
// DATABASE_CONSTRING_BAK).Count(strShowTables1);
//Console.WriteLine(CurrentRestoreTableName + "還原後資料條數:--" + cnt);
cntMM = 0;
}
if(!string.IsNullOrEmpty(e.Data) && e.Data.ToLower().IndexOf("mysql") >= 0)
{ cntMM++; }
}
}
已測試
有疑問可聯絡QQ 1023360745 僅供技術交流 騷擾不回。
建議先備份再還原
注意:還原會鎖表 自己檢視SQL
查詢資料條數 自己根據自己的mysql 查詢一下
我的場景:
備份資料:window服務中 一段程式碼 來自己備份資料
還原資料:client傳送請求則還原需要查詢的資料表 回撥告訴client還原成功資料已ready!
歡迎點評