当前位置:   article > 正文

mysql定时数据备份工具(c#)_c# mysql数据库自动备份

c# mysql数据库自动备份

此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/40124773如果进行转载请注明出处。本文作者原创,邮箱zhujunxxxxx@163.com,如有问题请联系作者

为了确保数据的安全,我们往往要对数据进行备份。但是为了减少我们的工作量,我写了一个简单的数据备份工具,实现定时备份数据库。

其实程序很简单,数据备份的工作就是几个mysql的命令而已。

先看看程序的运行界面


可以看到界面是十分的简单的

我们使用的是命令行来进行数据备份,所以我们的程序需要一个能够执行命令行的函数

  1. /// <summary>
  2. /// 执行Cmd命令
  3. /// </summary>
  4. /// <param name="workingDirectory">要启动的进程的目录</param>
  5. /// <param name="command">要执行的命令</param>
  6. public static void StartCmd(String workingDirectory, String command)
  7. {
  8. Process p = new Process();
  9. p.StartInfo.FileName = "cmd.exe";
  10. p.StartInfo.WorkingDirectory = workingDirectory;
  11. p.StartInfo.UseShellExecute = false;
  12. p.StartInfo.RedirectStandardInput = true;
  13. p.StartInfo.RedirectStandardOutput = true;
  14. p.StartInfo.RedirectStandardError = true;
  15. p.StartInfo.CreateNoWindow = true;
  16. p.Start();
  17. p.StandardInput.WriteLine(command);
  18. p.StandardInput.WriteLine("exit");
  19. }

接下来是一个备份数据库的函数

  1. public void bakup_db()
  2. {
  3. try
  4. {
  5. //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";
  6. //构建执行的命令
  7. StringBuilder sbcommand = new StringBuilder();
  8. StringBuilder sbfileName = new StringBuilder();
  9. sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", "");
  10. String fileName = sbfileName.ToString();
  11. String directory = bakpath + fileName+".bak";
  12. sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory);
  13. String command = sbcommand.ToString();
  14. //获取mysqldump.exe所在路径
  15. //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
  16. StartCmd(appDirecroty, command);
  17. }
  18. catch (Exception ex)
  19. {
  20. }
  21. }

还原数据库

  1. public void recovery_db()
  2. {
  3. //string s = "mysql --port=端口号 --user=用户名 --password=密码 数据库名<还原文件所在路径";
  4. try
  5. {
  6. StringBuilder sbcommand = new StringBuilder();
  7. OpenFileDialog openFileDialog = new OpenFileDialog();
  8. if (openFileDialog.ShowDialog() == DialogResult.OK)
  9. {
  10. String directory = openFileDialog.FileName;
  11. //在文件路径后面加上""避免空格出现异常
  12. sbcommand.AppendFormat("mysql --host=localhost --default-character-set=utf8 --port=3306 --user={0} --password={1} {2}<\"{3}\"",uname,upass,dbname,directory);
  13. String command = sbcommand.ToString();
  14. //获取mysql.exe所在路径
  15. //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
  16. DialogResult result = MessageBox.Show("您是否真的想覆盖以前的数据库吗?那么以前的数据库数据将丢失!!!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  17. if (result == DialogResult.Yes)
  18. {
  19. StartCmd(appDirecroty, command);
  20. MessageBox.Show("数据库还原成功!");
  21. }
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. MessageBox.Show("数据库还原失败!");
  27. }
  28. }

为了实现定时备份,我们使用的是一个Timer组件,来实现定时的数据备份

  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3. int h = DateTime.Now.Hour;
  4. if (h == hour)
  5. {
  6. bakup_db();
  7. }
  8. }

给出完整的代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;
  9. namespace MysqlBak
  10. {
  11. public partial class Form1 : Form
  12. {
  13. //备份文件的路径
  14. public String bakpath="d:\\db_bak\\";
  15. public String appDirecroty = @"C:\Program Files (x86)\MySQL\MySQL Server 6.0\bin";
  16. public String uname = "root";
  17. public String upass = "root";
  18. public String dbname = "losscar_db";
  19. public int hour=18;
  20. public Form1()
  21. {
  22. InitializeComponent();
  23. timer1.Interval=1000*10;
  24. timer1.Start();
  25. txt_uname.Text = uname;
  26. txt_upass.Text = upass;
  27. txt_dbname.Text = dbname;
  28. txt_bakpath.Text = bakpath;
  29. txt_mysql.Text = appDirecroty;
  30. txt_hour.Text = hour.ToString();
  31. }
  32. /// <summary>
  33. /// 执行Cmd命令
  34. /// </summary>
  35. /// <param name="workingDirectory">要启动的进程的目录</param>
  36. /// <param name="command">要执行的命令</param>
  37. public static void StartCmd(String workingDirectory, String command)
  38. {
  39. Process p = new Process();
  40. p.StartInfo.FileName = "cmd.exe";
  41. p.StartInfo.WorkingDirectory = workingDirectory;
  42. p.StartInfo.UseShellExecute = false;
  43. p.StartInfo.RedirectStandardInput = true;
  44. p.StartInfo.RedirectStandardOutput = true;
  45. p.StartInfo.RedirectStandardError = true;
  46. p.StartInfo.CreateNoWindow = true;
  47. p.Start();
  48. p.StandardInput.WriteLine(command);
  49. p.StandardInput.WriteLine("exit");
  50. }
  51. private void btn_bak_Click(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";
  56. //构建执行的命令
  57. StringBuilder sbcommand = new StringBuilder();
  58. StringBuilder sbfileName = new StringBuilder();
  59. sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", "");
  60. String fileName = sbfileName.ToString();
  61. String directory = bakpath + fileName + ".bak";
  62. sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory);
  63. String command = sbcommand.ToString();
  64. //获取mysqldump.exe所在路径
  65. //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
  66. StartCmd(appDirecroty, command);
  67. MessageBox.Show(@"数据库已成功备份到 " + directory + " 文件中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  68. }
  69. catch (Exception ex)
  70. {
  71. MessageBox.Show("数据库备份失败!");
  72. }
  73. }
  74. public void bakup_db()
  75. {
  76. try
  77. {
  78. //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";
  79. //构建执行的命令
  80. StringBuilder sbcommand = new StringBuilder();
  81. StringBuilder sbfileName = new StringBuilder();
  82. sbfileName.AppendFormat("{0}", DateTime.Now.ToShortDateString()).Replace("-", "").Replace(":", "").Replace(" ", "").Replace("/", "");
  83. String fileName = sbfileName.ToString();
  84. String directory = bakpath + fileName+".bak";
  85. sbcommand.AppendFormat("mysqldump --quick --host=localhost --default-character-set=utf8 --lock-tables --verbose --force --port=3306 --user={0} --password={1} {2} -r \"{3}\"", uname, upass, dbname, directory);
  86. String command = sbcommand.ToString();
  87. //获取mysqldump.exe所在路径
  88. //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
  89. StartCmd(appDirecroty, command);
  90. }
  91. catch (Exception ex)
  92. {
  93. }
  94. }
  95. private void btn_recovery_Click(object sender, EventArgs e)
  96. {
  97. recovery_db();
  98. }
  99. public void recovery_db()
  100. {
  101. //string s = "mysql --port=端口号 --user=用户名 --password=密码 数据库名<还原文件所在路径";
  102. try
  103. {
  104. StringBuilder sbcommand = new StringBuilder();
  105. OpenFileDialog openFileDialog = new OpenFileDialog();
  106. if (openFileDialog.ShowDialog() == DialogResult.OK)
  107. {
  108. String directory = openFileDialog.FileName;
  109. //在文件路径后面加上""避免空格出现异常
  110. sbcommand.AppendFormat("mysql --host=localhost --default-character-set=utf8 --port=3306 --user={0} --password={1} {2}<\"{3}\"",uname,upass,dbname,directory);
  111. String command = sbcommand.ToString();
  112. //获取mysql.exe所在路径
  113. //String appDirecroty = System.Windows.Forms.Application.StartupPath + "\\";
  114. DialogResult result = MessageBox.Show("您是否真的想覆盖以前的数据库吗?那么以前的数据库数据将丢失!!!", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  115. if (result == DialogResult.Yes)
  116. {
  117. StartCmd(appDirecroty, command);
  118. MessageBox.Show("数据库还原成功!");
  119. }
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. MessageBox.Show("数据库还原失败!");
  125. }
  126. }
  127. private void btn_edit_Click(object sender, EventArgs e)
  128. {
  129. if (btn_edit.Text=="修改")
  130. {
  131. txt_dbname.Enabled = true;
  132. txt_uname.Enabled = true;
  133. txt_upass.Enabled = true;
  134. txt_bakpath.Enabled = true;
  135. txt_mysql.Enabled = true;
  136. txt_hour.Enabled = true;
  137. btn_edit.Text = "确定";
  138. }
  139. else if (btn_edit.Text == "确定")
  140. {
  141. uname = txt_uname.Text;
  142. upass = txt_upass.Text;
  143. dbname = txt_dbname.Text;
  144. appDirecroty = txt_mysql.Text;
  145. bakpath = txt_bakpath.Text;
  146. hour = int.Parse(txt_hour.Text);
  147. MessageBox.Show("修改成功!");
  148. btn_edit.Text = "修改";
  149. txt_dbname.Enabled = false;
  150. txt_uname.Enabled = false;
  151. txt_upass.Enabled = false;
  152. txt_bakpath.Enabled = false;
  153. txt_mysql.Enabled = false;
  154. txt_hour.Enabled = false;
  155. }
  156. }
  157. private void timer1_Tick(object sender, EventArgs e)
  158. {
  159. int h = DateTime.Now.Hour;
  160. if (h == hour)
  161. {
  162. bakup_db();
  163. }
  164. }
  165. }
  166. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/711555
推荐阅读
相关标签
  

闽ICP备14008679号