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!

欢迎点评

猜你喜欢

转载自www.cnblogs.com/JunGeHuang/p/10138337.html