文件复制,灾备中易被忽视的关键点 11月14日 星期五 15:00 云祺视频号准时直播
云祺直播二维码
扫码预约直播,观看精彩内容! 扫码预约直播,观看精彩内容!
关闭按钮
云祺Logo 云祺Logo
关于我们

技术分享

技术分享 c# Process cmd 执行完回调 Proc_OutputDataReceived mysql mysqldump mysql source备份还原数据

c# Process cmd 执行完回调 Proc_OutputDataReceived mysql mysqldump mysql source备份还原数据

2020-08-04

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

 

还原:

c# Process cmd 执行完回调 Proc_OutputDataReceived  mysql mysqldump mysql source备份还原数据

备份:

c# Process cmd 执行完回调 Proc_OutputDataReceived  mysql mysqldump mysql source备份还原数据

直接上代码 

 

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!


云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 技术分享

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载

请添加好友为您提供支持
jia7jia_7

微信售后服务二维码

请拨打电话
为您提供支持

400-9955-698