在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java备份还原Mysql数据库

2016-12-20 13:15| 发布者: zhangjf| 查看: 565| 评论: 0

摘要: ///实体类package com.ews.util;/** * 系统备份展示对象 * * */public class DataFile { private String fileName;//备份文件的名称 private String fileDate;//备份文件的日期 private String filePath;//备份文件 ...
  1. ///实体类
  2. package com.ews.util;
  3. /**
  4. * 系统备份展示对象
  5. *
  6. * */
  7. public class DataFile {
  8. private String fileName;//备份文件的名称
  9. private String fileDate;//备份文件的日期
  10. private String filePath;//备份文件的地址
  11. private String fileSize;//备份文件的大小
  12. public String getFileSize() {
  13. return fileSize;
  14. }
  15. public void setFileSize(String fileSize) {
  16. this.fileSize = fileSize;
  17. }
  18. public String getFileName() {
  19. return fileName;
  20. }
  21. public void setFileName(String fileName) {
  22. this.fileName = fileName;
  23. }
  24. public String getFileDate() {
  25. return fileDate;
  26. }
  27. public void setFileDate(String fileDate) {
  28. this.fileDate = fileDate;
  29. }
  30. public String getFilePath() {
  31. return filePath;
  32. }
  33. public void setFilePath(String filePath) {
  34. this.filePath = filePath;
  35. }
  36. }
  37. ///实现备份代码
  38. package com.ews.action;
  39. import java.io.BufferedOutputStream;
  40. import java.io.DataInputStream;
  41. import java.io.DataOutputStream;
  42. import java.io.File;
  43. import java.io.FileInputStream;
  44. import java.io.FileNotFoundException;
  45. import java.io.FileOutputStream;
  46. import java.sql.Date;
  47. import java.text.DecimalFormat;
  48. import java.text.SimpleDateFormat;
  49. import java.util.ArrayList;
  50. import java.util.List;
  51. import org.apache.commons.dbcp.BasicDataSource;
  52. import org.apache.commons.fileupload.FileItem;
  53. import org.apache.struts2.ServletActionContext;
  54. import org.springframework.context.ApplicationContext;
  55. import org.springframework.context.support.ClassPathXmlApplicationContext;
  56. import com.ews.util.DataFile;
  57. public class DataAction extends EwsAction{
  58. private String username;
  59. private String password;
  60. private String host;
  61. private String PORT;
  62. private String dbname;
  63. private List dataFiles = new ArrayList();
  64. private File reductionFile;
  65. public File getReductionFile() {
  66. return reductionFile;
  67. }
  68. public void setReductionFile(File reductionFile) {
  69. this.reductionFile = reductionFile;
  70. }
  71. public List getDataFiles() {
  72. return dataFiles;
  73. }
  74. public void setDataFiles(List dataFiles) {
  75. this.dataFiles = dataFiles;
  76. }
  77. public String getHost() {
  78. return host;
  79. }
  80. public void setHost(String host) {
  81. this.host = host;
  82. }
  83. public String getPORT() {
  84. return PORT;
  85. }
  86. public void setPORT(String pORT) {
  87. PORT = pORT;
  88. }
  89. public String getUsername() {
  90. return username;
  91. }
  92. public void setUsername(String username) {
  93. this.username = username;
  94. }
  95. public String getPassword() {
  96. return password;
  97. }
  98. public void setPassword(String password) {
  99. this.password = password;
  100. }
  101. public String getDbname() {
  102. return dbname;
  103. }
  104. public void setDbname(String dbname) {
  105. this.dbname = dbname;
  106. }
  107. /**
  108. * 删除
  109. * */
  110. public String delete(){
  111. String fileName = request.getParameter("fileName");
  112. System.out.println(fileName);
  113. String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+fileName;
  114. File file = new File(backPath);
  115. file.delete();
  116. return "delete";
  117. }
  118. /**
  119. * 得到备份文件的List集合
  120. *
  121. * */
  122. public String findList(){
  123. String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/";
  124. File file = new File(backPath);
  125. if (!file.exists())
  126. return "findListData";
  127. File[] file1 = file.listFiles();
  128. for (int i = 0; i < file1.length; i++) {
  129. if(file1[i].getName().equals("ramdit.txt")) continue;
  130. SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  131. //前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
  132. java.util.Date dt = new Date(file1[i].lastModified());
  133. String sDateTime = sdf.format(dt); //得到精确到秒的表示:08/31/2006 21:08:00
  134. DataFile dataFile = new DataFile();
  135. dataFile.setFileName(file1[i].getName());
  136. dataFile.setFileDate(sDateTime);
  137. String path = request.getContextPath();
  138. String filePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/ewssite/back/"+file1[i].getName();
  139. dataFile.setFilePath(filePath);
  140. DecimalFormat df = new DecimalFormat( ".## ");
  141. dataFile.setFileSize(df.format(file1[i].length()/1024000f));
  142. dataFiles.add(dataFile);
  143. }
  144. return "findListData";
  145. }
  146. /**
  147. * 配置 Mysql bin目录
  148. * */
  149. public void getConfig(){
  150. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  151. BasicDataSource ba = (BasicDataSource)context.getBean("dataSource");
  152. setUsername(ba.getUsername());
  153. setPassword(ba.getPassword());
  154. String url = ba.getUrl();
  155. url = url.substring(13, url.length());
  156. String[] temp = url.split("/");
  157. String[] temp1 = temp[0].split(":");
  158. setHost(temp1[0]);
  159. setPORT(temp1[1]);
  160. for (int i = 0; i < temp[1].length(); i++) {
  161. String temp2 = temp[1].charAt(i)+"";
  162. if(temp2.equals("?")){
  163. setDbname(temp[1].substring(0,5));
  164. }
  165. }
  166. }
  167. /**
  168. * 备份
  169. * */
  170. public String backup(){
  171. getConfig();
  172. //得到配置文件
  173. try {
  174. Runtime rt = Runtime.getRuntime();
  175. String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+System.currentTimeMillis()+".sql";
  176. String mysql = "mysqldump -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" " + getDbname() +" >"+"""+backPath+""";
  177. Process proc = rt.exec("cmd.exe /c "+mysql);// 设置导出编码为utf8。这里必须是utf8
  178. //String backExe = ServletActionContext.getServletContext().getRealPath("/")+"bin/mysqldump.exe";
  179. //String mysql = getDbname()+ " -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" >"+"""+backPath+""";
  180. int tag = proc.waitFor();// 等待进程终止
  181. } catch (Exception e) {
  182. e.printStackTrace();
  183. }
  184. return "backup";
  185. }
  186. /**
  187. * 还原
  188. * */
  189. public String load(){
  190. String sqlPath="";
  191. if(request.getParameter("selectName")!=null)
  192. sqlPath = request.getParameter("selectName");
  193. if(reductionFile!=null){
  194. String name = upload(reductionFile);
  195. sqlPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/" + name;
  196. }
  197. // System.out.println(sqlPath);
  198. if(sqlPath.substring(sqlPath.lastIndexOf(".")+1).equals("sql")){
  199. getConfig();
  200. setHost("127.0.0.1");
  201. setUsername("root");
  202. setPassword("root");
  203. setDbname("test");
  204. //得到配置文件
  205. try {
  206. Runtime rt = Runtime.getRuntime();
  207. String createDb = "mysqladmin -u" + getUsername()+ " -p" + getPassword() + " create "+getDbname();
  208. String mysql = "mysql -u" + getUsername()+ " -p" + getPassword() + " "+getDbname()+" <"+"""+ sqlPath+""";//+"""+backPath+"""
  209. rt.exec("cmd.exe /c "+createDb);
  210. Process proc = rt.exec("cmd.exe /c "+mysql);
  211. int tag = proc.waitFor();// 等待进程终止
  212. } catch (Exception e) {
  213. e.printStackTrace();
  214. }
  215. }
  216. return "load";
  217. }
  218. /**
  219. * 把本地的数据库备份文件上传到服务器上
  220. * file:从前台获取的file
  221. * */
  222. public String upload(File file ){
  223. String name = "";
  224. try {
  225. DataInputStream in = new DataInputStream(new FileInputStream(file));
  226. // FileInputStream in = new FileInputStream(file);
  227. String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/";
  228. name = System.currentTimeMillis()+".sql";
  229. backPath = backPath + name;
  230. // FileOutputStream out = new FileOutputStream(new File(backPath));
  231. DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(backPath)));
  232. int b = -1;
  233. while ((b = in.read()) != -1) {
  234. out.write(b);
  235. }
  236. out.close();
  237. in.close();
  238. } catch (Exception e) {
  239. // TODO Auto-generated catch block
  240. e.printStackTrace();
  241. }
  242. return name;
  243. }
  244. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 12:29

Copyright 2015-2025 djqfx

返回顶部