java备份还原mysql数据库_Java备份还原Mysql数据库

///实体类

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 org.apache.commons.dbcp.BasicDataSource;

import org.apache.commons.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/2006 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 < temp[1].length(); i++) {

String temp2 = temp[1].charAt(i)+"";

if(temp2.equals("?")){

setDbname(temp[1].substring(0,5));

}

}

}

/**

* 备份

* */

public String backup(){

getConfig();

//得到配置文件

try {

Runtime rt = Runtime.getRuntime();

String backPath = ServletActionContext.getServletContext().getRealPath("/")+"ewssite/back/"+System.currentTimeMillis()+".sql";

String mysql = "mysqldump -u" + getUsername()+ " -p" + getPassword() + " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" " + getDbname() +" >"+"\""+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()+"

f68f2add0b68e4f9810432fce46917b7.png

相关文章

相关视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用Spring MVC实现数据库备份可以通过以下步骤实现: 1. 配置数据库连接信息:在Spring MVC的配置文件中,配置数据库的连接信息,包括数据库的URL、用户名和密码。 2. 创建备份文件夹:在服务器上创建一个用于存储数据库备份文件的文件夹。 3. 编写备份方法:在Spring MVC的控制器中,编写一个备份数据库的方法。可以使用Java的Runtime类来执行命令行操作,调用数据库备份命令将数据库备份到指定的文件夹中。 4. 编写恢复方法:同样在控制器中,编写一个恢复数据库的方法。通过调用数据库恢复命令,将备份文件中的数据还原数据库中。 5. 配置路由:在Spring MVC的配置文件中,配置备份恢复方法的路由信息,使其可以通过URL访问到。 下面是一个示例代码: ```java @Controller @RequestMapping("/database") public class DatabaseController { @RequestMapping("/backup") public void backupDatabase() { try { // 创建备份文件夹 File backupFolder = new File("/path/to/backup/folder"); if (!backupFolder.exists()) { backupFolder.mkdirs(); } // 执行备份命令 String command = "mysqldump -u username -p password database > /path/to/backup/folder/backup.sql"; Process process = Runtime.getRuntime().exec(command); int exitValue = process.waitFor(); if (exitValue == 0) { System.out.println("Database backup successful."); } else { System.out.println("Database backup failed."); } } catch (Exception e) { e.printStackTrace(); } } @RequestMapping("/restore") public void restoreDatabase() { try { // 执行恢复命令 String command = "mysql -u username -p password database < /path/to/backup/folder/backup.sql"; Process process = Runtime.getRuntime().exec(command); int exitValue = process.waitFor(); if (exitValue == 0) { System.out.println("Database restore successful."); } else { System.out.println("Database restore failed."); } } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,上述代码中的`/path/to/backup/folder`应替换为实际的备份文件夹路径,`username`、`password`和`database`应替换为实际的数据库连接信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值