c# winform 实现对postgresql数据库的自动备份还原功能
2014-03-07 12:09
567 查看
//创建进程
public static void StartCmd(String workingDirectory, String command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = workingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
p.Close();
}
//构建执行的命令
StringBuilder sbcommand = new StringBuilder();
sbcommand.AppendFormat("pg_dump.exe -h localhost -p 5556 -U pgs -Fc -b -v -f\"{0}\" tj", @path);
String command = sbcommand.ToString();
//获取pg_dump.exe所在路径
String appDirecroty = @"C:\Program Files\pgsql\bin";
StartCmd(appDirecroty, command);
string backtime = time.ToString("yyyy年MM月dd日HH点mm分ss秒");
string sql = "insert into backup(version,date,path)values('1.0.7','" + backtime + "','" + path2 + "')";
MainFrmClient mfClient = new MainFrmClient();
mfClient.ExecSql(sql);
DataBind();
MessageBox.Show("恭喜,备份成功!");
//数据库还原
OpenFileDialog file = new OpenFileDialog();
file.Title = "打开文件";
file.Filter = "备份文件(*.backup)|";
file.InitialDirectory = "D:\\backup\\"; //默认路径
file.Multiselect = false;
if (file.ShowDialog() == DialogResult.OK)
{
//构建执行的命令
StringBuilder sbcommand = new StringBuilder();
sbcommand.AppendFormat("pg_restore.exe -h localhost -p 5556 -U pgs -d test -v \"{0}\"", file.FileName);
String command = sbcommand.ToString();
//获取pg_dump.exe所在路径
String appDirecroty = @"C:\Program Files\pgsql\bin";
StartCmd(appDirecroty, command);
MessageBox.Show("恭喜,还原成功!");
}
//对postgresql数据库内某一模式备份命令:
//h:主机IP,p:端口, U:用户名,f:备份文件存储位置,
sbcommand.AppendFormat("pg_dump.exe -h localhost -p 5556 -U pgs -Fc -v -f \"{0}\" --schema \"{1}\" tjtz", path, schemaName);
String command = sbcommand.ToString();
还原该模式数据时同上还原:
sbcommand.AppendFormat("pg_restore.exe -h localhost -p 5556 -U pgs -d test -v \"{0}\"", file.FileName);
博客来自:http://blog.csdn.net/swarb/article/details/6935485
public static void StartCmd(String workingDirectory, String command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = workingDirectory;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
p.Close();
}
//构建执行的命令
StringBuilder sbcommand = new StringBuilder();
sbcommand.AppendFormat("pg_dump.exe -h localhost -p 5556 -U pgs -Fc -b -v -f\"{0}\" tj", @path);
String command = sbcommand.ToString();
//获取pg_dump.exe所在路径
String appDirecroty = @"C:\Program Files\pgsql\bin";
StartCmd(appDirecroty, command);
string backtime = time.ToString("yyyy年MM月dd日HH点mm分ss秒");
string sql = "insert into backup(version,date,path)values('1.0.7','" + backtime + "','" + path2 + "')";
MainFrmClient mfClient = new MainFrmClient();
mfClient.ExecSql(sql);
DataBind();
MessageBox.Show("恭喜,备份成功!");
//数据库还原
OpenFileDialog file = new OpenFileDialog();
file.Title = "打开文件";
file.Filter = "备份文件(*.backup)|";
file.InitialDirectory = "D:\\backup\\"; //默认路径
file.Multiselect = false;
if (file.ShowDialog() == DialogResult.OK)
{
//构建执行的命令
StringBuilder sbcommand = new StringBuilder();
sbcommand.AppendFormat("pg_restore.exe -h localhost -p 5556 -U pgs -d test -v \"{0}\"", file.FileName);
String command = sbcommand.ToString();
//获取pg_dump.exe所在路径
String appDirecroty = @"C:\Program Files\pgsql\bin";
StartCmd(appDirecroty, command);
MessageBox.Show("恭喜,还原成功!");
}
//对postgresql数据库内某一模式备份命令:
//h:主机IP,p:端口, U:用户名,f:备份文件存储位置,
sbcommand.AppendFormat("pg_dump.exe -h localhost -p 5556 -U pgs -Fc -v -f \"{0}\" --schema \"{1}\" tjtz", path, schemaName);
String command = sbcommand.ToString();
还原该模式数据时同上还原:
sbcommand.AppendFormat("pg_restore.exe -h localhost -p 5556 -U pgs -d test -v \"{0}\"", file.FileName);
博客来自:http://blog.csdn.net/swarb/article/details/6935485
相关文章推荐
- c# winform 实现对postgresql数据库的自动备份还原功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- T-SQL语句实现数据库备份与还原功能
- python语言实现postgresql数据库自动备份
- 利用T-SQL语句,实现数据库的备份与还原的功能
- asp.net实现数据库备份还原功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 数据库自动备份与手动备份功能的实现
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 实现数据库的备份与还原的功能
- 利用MySQL的mysqlhotcopy实现数据库自动备份的SH脚本源码
- 用java代码实现数据库的自动备份
- 在SQL Server 2005数据库中实现自动备份
- web服务文件更新自动同步、数据库主从复制、shell脚本实现网站代码备份和mysql备份
- C#实现数据库的备份与还原
- 【转】使用SQL Server维护计划实现数据库定时自动备份