最近在做的一个项目需要对mysql数据库中的单个表格进行备份

其中,一部分表格需要进行筛选备份(例如对最近插入的1000条记录进行备份)

 

思路:java调用系统命令完成备份操作

 

testdb

test_table

  备份操作所做的是,把指定的数据库表中的一部分记录生成一个sql备份文件,以供还原操作,文件名使用日期生成。

 

备份操作



public void backUpTable(String tableName, Integer number) throws Exception {
		StringBuilder command  = new StringBuilder();
		Runtime rt = Runtime.getRuntime();
		Process pro = null;
		
		command.append("mysqldump -u" + username + " -p" + password + " testdb ");
		//username为数据库用户名 password为密码

		String time = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
		String fileName ="E://" + tableName + "/" + time + ".sql";//放在E盘下
        
		
		//备份表中最新number行的数据
//		mysqldump -uusername -ppassword testdb test_table -w \" 1=1 ORDER BY id DESC LIMIT 0,300\" --result-file=E://test.sql 
		command.append(tableName);
		command.append(" ");
		command.append("-w \" 1=1 ORDER BY id DESC LIMIT 0,");
		command.append(number);
		command.append("\"");
		command.append(" ");
		command.append("--result-file=" + fileName);
		pro = rt.exec(command.toString());
				
		//命令执行出错则打印相关错误信息
		BufferedReader br = new BufferedReader(new InputStreamReader(	pro.getErrorStream()));
		String errorLine = null;
		while ((errorLine = br.readLine()) != null) {
			System.out.println(errorLine);
		}
		br.close();
	}



其中,使用mysqldump进行备份操作

由于需要进行筛选,使用-w添加了筛选条件,--result-file= 指向生成文件的存放路径

 

备份完成了,接下来就是还原操作

  假如我们需要将E://test.sql还原到数据库中,

  需要执行的命令为mysql -uusername -ppassword testdb < E://test.sql

>,

  而java无法识别重定向符和管道操作符,故不能向上面备份操作一样进行还原,需要采用其他方式,如下

 

在类中定义了字符串数组String[] command = new String[3];

1、需要先判断当前系统环境



public void initOSCommand(){
		String osName = System.getProperty("os.name");
		logger.info("系统环境为:" + osName);
		osName = osName.toLowerCase();
		if(osName.indexOf("win")>-1){
			command[0] = "cmd";
			command[1] = "/c";
		}else if(osName.indexOf("linux")>-1){
			command[0] = "/bin/bash";
//			command[0] = "/bin/sh";  sh和bash的区别不大,bash是sh的增强版本
			command[1] = "-c";
		}else{
			logger.error("当前系统环境不支持数据库还原操作");
		}
	}



2、执行还原操作



public void restoreTable()throws Exception {
		//mysql -uusername -ppassword testdb < E://test.sql
		StringBuilder commandStr  = new StringBuilder();
		Runtime rt = Runtime.getRuntime();
		Process pro = null;
		String fileName = "E://test.sql";
		
		commandStr.append("mysql -u" + username + " -p" + password + " testdb < ");
		commandStr.append(fileName);
		command[2] = commandStr.toString();
//		System.out.println(command[0] + command[1] + command[2]);
		pro = rt.exec(command);
		
//		命令执行出错则打印相关错误信息
		BufferedReader br = new BufferedReader(new InputStreamReader(	pro.getErrorStream()));
		String errorLine = null;
		while ((errorLine = br.readLine()) != null) {
			System.out.println(errorLine);
		}
		br.close();
	}


 

以上为解决方法中的一种,另一种等测试完可行之后再贴上来。

以上代码在window10下能正常运行,尚未在linux上测试,后面测试完了再更新此随笔。

 

需要特别注意的是,mysql5.6版本以后,执行mysqldump -uusername -ppassword ····        

会无法执行并提示安全警告

解决方法:对mysqldump命令进行配置(详情百度2333)或降低mysql的版本到5.6以下

 

个人能力渣渣,随笔有不对或者讲述的不到位的地方,求读者指点批判,谢谢阅读