失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java备份还原Mysql数据库

Java备份还原Mysql数据库

时间:2018-09-12 17:43:19

相关推荐

Java备份还原Mysql数据库

Java|java教程

代码片段,代码分享,PHP代码分享,Java代码分享,Ruby代码分享,Python代码分享,HTML代码分享,CSS代

Java-java教程

isapi筛选器 源码 例程,在vscode中写perl,ubuntu寻找目录,tomcat8 角色,python爬虫最快,php 值传递和引用传递,揭阳seo需要什么技术,jsp购物网站源代码 前台后台,服务意见反馈表模板lzw

///实体类package com.ews.util;/** * 系统备份展示对象 * * */public class DataFile { private String fileName;//备份文件的名称 private String fileDate;//备份文件的日期 private String filePath;//备份文件的地址 private String fileSize;//备份文件的大小 public String getFileSize() { return fileSize; } public void setFileSize(String fileSize) { this.fileSize = fileSize; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileDate() { return fileDate; } public void setFileDate(String fileDate) { this.fileDate = fileDate; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; }} ///实现备份代码package com.ews.action; import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.sql.Date;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List; import mons.dbcp.BasicDataSource;import mons.fileupload.FileItem;import org.apache.struts2.ServletActionContext;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ews.util.DataFile; public class DataAction extends EwsAction{ private String username; private String password; private String host; private String PORT; private String dbname; private List dataFiles = new ArrayList(); private File reductionFile; public File getReductionFile() { return reductionFile; } public void setReductionFile(File reductionFile) { this.reductionFile = reductionFile; } public List getDataFiles() { return dataFiles; } public void setDataFiles(List dataFiles) { this.dataFiles = dataFiles; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPORT() { return PORT; } public void setPORT(String pORT) { PORT = pORT; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDbname() { return dbname; } public void setDbname(String dbname) { this.dbname = dbname; } /** * 删除 * */ public String delete(){ String fileName = request.getParameter("fileName"); System.out.println(fileName); String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+fileName; File file = new File(backPath); file.delete(); return "delete"; } /** * 得到备份文件的List集合 * * */ public String findList(){ String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"; File file = new File(backPath); if (!file.exists()) return "findListData"; File[] file1 = file.listFiles(); for (int i = 0; i < file1.length; i++) { if(file1[i].getName().equals("ramdit.txt")) continue; SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); //前面的lSysTime是秒数,先乘1000得到毫秒数,再转为java.util.Date类型 java.util.Date dt = new Date(file1[i].lastModified());String sDateTime = sdf.format(dt); //得到精确到秒的表示:08/31/ 21:08:00 DataFile dataFile = new DataFile(); dataFile.setFileName(file1[i].getName()); dataFile.setFileDate(sDateTime); String path = request.getContextPath(); String filePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/ewssite/back/"+file1[i].getName(); dataFile.setFilePath(filePath); DecimalFormat df = new DecimalFormat( ".## "); dataFile.setFileSize(df.format(file1[i].length()/1024000f)); dataFiles.add(dataFile); } return "findListData"; } /** * 配置 Mysql bin目录 * */ public void getConfig(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BasicDataSource ba = (BasicDataSource)context.getBean("dataSource"); setUsername(ba.getUsername()); setPassword(ba.getPassword()); String url = ba.getUrl(); url = url.substring(13, url.length()); String[] temp = url.split("/"); String[] temp1 = temp[0].split(":"); setHost(temp1[0]); setPORT(temp1[1]); for (int i = 0; i "+"\""+backPath+"\"";Process proc = rt.exec("cmd.exe /c "+mysql);// 设置导出编码为utf8。这里必须是utf8 //String backExe = ServletActionContext.getServletContext().getRealPath("/")+"bin/mysqldump.exe"; //String mysql = getDbname()+ " -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" >"+"\""+backPath+"\""; int tag = proc.waitFor();// 等待进程终止 } catch (Exception e) { e.printStackTrace(); } return "backup"; } /** * 还原 * */ public String load(){ String sqlPath=""; if(request.getParameter("selectName")!=null) sqlPath = request.getParameter("selectName"); if(reductionFile!=null){ String name = upload(reductionFile); sqlPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/" + name; }// System.out.println(sqlPath); if(sqlPath.substring(sqlPath.lastIndexOf(".")+1).equals("sql")){ getConfig(); setHost("127.0.0.1"); setUsername("root"); setPassword("root"); setDbname("test"); //得到配置文件 try { Runtime rt = Runtime.getRuntime(); String createDb = "mysqladmin -u" + getUsername()+ " -p" + getPassword() + " create "+getDbname(); String mysql = "mysql -u" + getUsername()+ " -p" + getPassword() + " "+getDbname()+" <"+"\""+ sqlPath+"\"";//+"\""+backPath+"\"" rt.exec("cmd.exe /c "+createDb); Process proc = rt.exec("cmd.exe /c "+mysql); int tag = proc.waitFor();// 等待进程终止 } catch (Exception e) { e.printStackTrace(); } } return "load"; } /** * 把本地的数据库备份文件上传到服务器上 * file:从前台获取的file * */ public String upload(File file ){ String name = ""; try { DataInputStream in = new DataInputStream(new FileInputStream(file));// FileInputStream in = new FileInputStream(file); String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"; name = System.currentTimeMillis()+".sql"; backPath = backPath + name;// FileOutputStream out = new FileOutputStream(new File(backPath)); DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(backPath))); int b = -1; while ((b = in.read()) != -1) { out.write(b); } out.close(); in.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return name; }}

javase源码,ubuntu+查内核,tomcat7无法加入,大爬虫兽,好未来php笔试题,seo性价比lzw

如果觉得《Java备份还原Mysql数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。