Net Core备份MySQL数据库


 一、操作环境

1、Net Core3.1

2、Visual Studio 2022 企业版

3、Mysql5.6

4、Dapper 2.0.78

二、来个图提个神

​编辑

三、上代码

(~ ̄(OO) ̄)ブ

备份代码,核心就是调用mysql的备份命令

controller层

[Route("/database/backup_db")]
        public JsonResult BackupDB()
        {
            try {
                string server = "127.0.0.1";
                string port = "3306";
                string user_name = "sa";
                string user_pass = "sa123$";
                string data_base_name = "yadinghao";
                string backup_filename = Tools.GetDateTimeNoSplit()   "yadinghao.sql";
                string relativePath = $@"\backup\";
                string filePath = _Environment.WebRootPath   relativePath;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string back_up_folder = filePath   backup_filename;
                new MySqlAccess().BackUpDb(server, port, user_name, user_pass, data_base_name, back_up_folder);
                return JsonResultRight("备份成功");
            }
            catch (Exception ex) {
                return JsonResultError("备份失败"   ex.Message);
            }
            
        }

 C#执行dos命令。

data层

  public bool BackUpDb(string server, string port, string user, string pwd, string dbname, string folder) {
			try
			{
				string str_sql = string.Format("mysqldump -h {0} -P {1} -u {2} -p{3} --databases {4} > {5}", server, port, user, pwd, dbname, folder);
                string myDumpToolPath = @"C:Program FilesMySQLMySQL Server 5.6in";
                Tools.ExecuteDosCommad(myDumpToolPath, str_sql);
				return true;
			}
			catch (Exception ex)
			{
				return false;
			}
			
        }

这里需要注意的参数string strPath。他是你备份机器的mysql安装路径。net比较垃圾他的根路径居然是项目的路径,而不是系统的路径。

public static string ExecuteDosCommad(string strPath, string strcmd)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = strPath;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口

            p.Start();
            p.StandardInput.WriteLine(strcmd);
            p.StandardInput.WriteLine("exit");
            string output = p.StandardOutput.ReadToEnd();
            string myError = p.StandardError.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程     
            p.Close();
            return myError;
        }

 一、操作环境

1、Net Core3.1

2、Visual Studio 2022 企业版

3、Mysql5.6

4、Dapper 2.0.78

二、来个图提个神

​编辑

三、上代码

(~ ̄(OO) ̄)ブ

备份代码,核心就是调用mysql的备份命令

controller层

[Route("/database/backup_db")]
        public JsonResult BackupDB()
        {
            try {
                string server = "127.0.0.1";
                string port = "3306";
                string user_name = "sa";
                string user_pass = "sa123$";
                string data_base_name = "yadinghao";
                string backup_filename = Tools.GetDateTimeNoSplit()   "yadinghao.sql";
                string relativePath = $@"\backup\";
                string filePath = _Environment.WebRootPath   relativePath;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string back_up_folder = filePath   backup_filename;
                new MySqlAccess().BackUpDb(server, port, user_name, user_pass, data_base_name, back_up_folder);
                return JsonResultRight("备份成功");
            }
            catch (Exception ex) {
                return JsonResultError("备份失败"   ex.Message);
            }
            
        }

 C#执行dos命令。

data层

  public bool BackUpDb(string server, string port, string user, string pwd, string dbname, string folder) {
			try
			{
				string str_sql = string.Format("mysqldump -h {0} -P {1} -u {2} -p{3} --databases {4} > {5}", server, port, user, pwd, dbname, folder);
                string myDumpToolPath = @"C:Program FilesMySQLMySQL Server 5.6in";
                Tools.ExecuteDosCommad(myDumpToolPath, str_sql);
				return true;
			}
			catch (Exception ex)
			{
				return false;
			}
			
        }

这里需要注意的参数string strPath。他是你备份机器的mysql安装路径。net比较垃圾他的根路径居然是项目的路径,而不是系统的路径。

public static string ExecuteDosCommad(string strPath, string strcmd)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.WorkingDirectory = strPath;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;//不显示程序窗口

            p.Start();
            p.StandardInput.WriteLine(strcmd);
            p.StandardInput.WriteLine("exit");
            string output = p.StandardOutput.ReadToEnd();
            string myError = p.StandardError.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程     
            p.Close();
            return myError;
        }

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在