\"{url}{datatime}.sql\"";//设置dos窗口的目录路径,这里就是自己安装mysql的bin目录(我们的mysqldump.exe和mysql.exe所在目录)working=数据库安装路径,mysqldump所在路径。_c">
赞
踩
1.利用调用dos窗口进行数据库备份
2. 备份命令: string command = $"mysqldump -u {user} -p{password} -h {server} -P{port} {database} -R --default-character-set=utf8 > \"{url}{datatime}.sql\"";
working=数据库安装路径,mysqldump所在路径
private static void StartCommand(string working, string command)
{
Process proc = new Process();
//调用dos窗口
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
proc.StartInfo.FileName = "cmd.exe";
}
else
{
proc.StartInfo.FileName = "/bin/bash";
}
//不显示窗体
proc.StartInfo.CreateNoWindow = true;
//设置dos窗口的目录路径,这里就是自己安装mysql的bin目录(我们的mysqldump.exe和mysql.exe所在目录)
proc.StartInfo.WorkingDirectory = working;
//允许输入流
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
//执行
proc.Start();
proc.StandardInput.WriteLine(command);
proc.WaitForExit(2000);
//proc.Close();
proc.StandardInput.WriteLine("exit");
}
3.数据库恢复
string create = "create database " + arr[0] + ";";
arr[0]=mysql 安装目录;
url=备份文件所在路径;
private static void StartRestore(string Working, string url, string user, string password, string[] arr, string create)
{
//创建进程对象
Process proc = new Process();
//调用dos窗口
proc.StartInfo.FileName = "cmd.exe";
//不显示窗体
proc.StartInfo.CreateNoWindow = true;
//设置dos窗口的目录路径,这里就是自己安装mysql的bin目录
proc.StartInfo.WorkingDirectory = Working;
//允许输入流
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
//执行
proc.Start();
proc.StandardInput.WriteLine("mysql -u" + user + " -p" + password);
proc.StandardInput.WriteLine(create);
proc.StandardInput.WriteLine("use " + arr[0] + ";");
proc.StandardInput.WriteLine("source " + url);
proc.WaitForExit(10000);
proc.Close();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。