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 = ;
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[]);
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[]);// "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 >= )
{
//string strShowTables1 = "select count(1) from ib_response201812171400;";
//int cnt = SessionFactory.GetSessionMySQLIB(DATABASE_PROVIDER,
// DATABASE_CONSTRING_BAK).Count(strShowTables1);
//Console.WriteLine(CurrentRestoreTableName + "还原后数据条数:--" + cnt);
cntMM = ;
}
if(!string.IsNullOrEmpty(e.Data) && e.Data.ToLower().IndexOf("mysql") >= )
{ cntMM++; }
} }

已测试

有疑问可联系QQ 1023360745  仅供技术交流  骚扰不回。

建议先备份再还原

注意:还原会锁表 自己查看SQL

查询数据条数 自己根据自己的mysql 查询一下

我的场景:

备份数据:window服务中 一段代码 来自己备份数据

还原数据:client发送请求则还原需要查询的数据表 回调告诉client还原成功数据已ready!

欢迎点评

c# Process cmd 执行完回调 Proc_OutputDataReceived mysql mysqldump mysql source备份还原数据的更多相关文章

  1. MySQL mysqldump与source导入慢的解决方法

    Jquery中文网 >  数据库  >  mysql  >  正文 MySQL mysqldump与source导入慢的解决方法 MySQL mysqldump与source导入慢的 ...

  2. MYSQL的日志与备份还原

    一.错误日志 当数据库出现任何故障导致无法使用时,第一时间先去查看该日志 1.服务器启动关闭过程中的信息 2.服务器运行过程中的错误信息 日志存放路径,可以通过命令查看: 日志文件命名格式:host_ ...

  3. MySQL - 日常操作二 备份还原

    登录mysql的命令 # 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 mysql -h 110. -P3306 -uroot -p mysql -uroot -p -S /dat ...

  4. Mysql表复制及备份还原

    1.复制表结构   1.1 含有主键等信息的完整表结构   CREATE table 新表名 LIKE book;     1.2 只有表结构,没有主键等信息   create table 新表名 s ...

  5. MySQL远程连接和备份还原

    连接远程数据库 mysql -h 数据库地址 -P 端口号 -u 用户名 -p mysql -h -u root -p 备份数据库, 热备份 mysqldump -h 127.0.0.1 -u roo ...

  6. mfc调用cmd执行完保留黑框

    mfc调用cmd的方法有很多,本文采用 ShellExecute ShellExecute(AfxGetMainWnd()->m_hWnd,L"open",L"cm ...

  7. MySQL 利用binlog增量备份+还原实例

    一,什么是增量备份 增量备份,就是将新增加的数据进行备份.假如你一个数据库,有10G的数据,每天会增加10M的数据,数据库每天都要备份一次,这么多数据是不是都要备份呢?还是只要备份增加的数据呢,很显然 ...

  8. 使用mysqldump进行mysql数据库备份还原

    mysqldump是mysql自带的备份还原工具,默认在安装目录的bin下 可通过cmd命令行启动,然后运行: 还原一个数据库: mysql -h 主机 -u 用户名 -p密码 数据库名 < 指 ...

  9. MySQL mysqldump 数据备份

    1.mysqldump 命令工具说明 参数注解: mysqldump 是采用SQL 级别的备份机制,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最常用的备 ...

随机推荐

  1. SQLServer 2008以上误操作数据库恢复方法

    解决方法:       对于这类问题,主要是找回误操作之前的数据,在2008之前,有个很出名的工具Log Exploer,听说还挺好用的,这个网上大把教程,这里就不多说了.但是唯一遗憾的是,不支持20 ...

  2. 【最新】Xmanager Power Suite 6.0 Build 0010

    永久最新版地址:https://www.netsarang.com/download/down_live.html 弹出来的下载地址,在.exe前面加r即可. 截至2018年11月14日发布的最新版本 ...

  3. pyCharm最新2018激活码

    本教程对jetbrains全系列可用例:IDEA.WebStorm.phpstorm.clion等 因公司的需求,需要做一个爬取最近上映的电影.列车号.航班号.机场.车站等信息,所以需要我做一个爬虫项 ...

  4. WSDL文档

    portType 相当于一个类. operation 相当于该类里有一个方法名,方法名为processAPNManagement,该方法里有一个输入消息,一个输出消息,一个错误消息.

  5. First Python script

    learn what is api Jailbreak pycharm install requests on pycharm write first request: get, post MFA l ...

  6. laravel 常见问题

    1. Specified key was too long; max key length is 767 bytes 处理: 修改config/database.php , mysql配置.删除数据库 ...

  7. 如何在xlwt中编写多个列的单元格?

    目的,写下面的表格: ---------------- | Long Cell | ---------------- | 1 | 2 | ---------------- 如果下面这样写: sheet ...

  8. uirecorder 启动webdriver服务报错

    在安装好uirecorder后,执行起来是各种错误. 不是少这个就是缺那个,也是因为自己对自动化测试知识太匮乏. 导致刚开始走自动化测试绕了很多弯路,报个错都不知所措.后来才知道要多看ERROR后面的 ...

  9. Integer类toString(int i,int radix)方法

    Integer类toString(int i,int radix)方法: 首先抛出java的api中的介绍: public static String toString(int i, int radi ...

  10. MyBatis-plus使用

    https://blog.csdn.net/qq_32867467/article/details/82944674 官网: https://mp.baomidou.com/guide/optimis ...