Java实现数据库备份与还原 [MYSQL为例]
数据库数据的备份与还原最好是使用数据库自带的客户端软件来实现,
Java可以通过调用mysqldump、mysql客户端软件分别进行mysql数据库的备份还原,
当然前提是Java服务器必须安装mysql客户端软件,但是也可以通过java telnet登陆数据库服务器实现备份,关于telnet的相关资料请Google.
MYSQL支持的备份方式:
1.使用SELECT INTO ...OUTFILE,例如
SQL代码
SELECT * INTO OUTFILE "D:\test.sql" fields terminatedby ',' lines terminatedby '' from category ;
生成的文件会存放在数据库服务器上面
SELECT INTO只备份了数据库表中的数据:如下为category备份后的文件内容,即是数据记录,并无数据表结构
2.使用BACKUP TABLE,例如
SQL代码
BACKUPTABLE test_tableto 'D:ackup';
BACKUP只针对MyISAM表格,且在MYSQL 5.1的参考手册中“注释:本语句不理想”;
BACKUP语句效果为拷贝数据库表中的.frm文件到数据库服务器目标目录
3.使用mysqldump程序或mysqlhotcopy脚本
MYSQL 5.1的参考手册中关于mysqldump的说明:
“可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器)。转储包含创建表和/或装载表的SQL语句。“
JAVA可以运用Process类的exec(String str)方法调用系统命令,所以需要在服务器上安装mysqldump程序已完成备份,
可以对远程的数据库服务器进行备份(配置hostname参数),备份语句具体参数参考MYSQL参考手册
MYSQL备份命令行:
SQL代码
mysqldump -hhostname -uusername -ppassword databasename > ‘backupfile’
MYSQL还原命令行:
SQL代码
mysql -hhostname -uusername -ppassword databasename < ‘backupfile
下面贴一种方法的Demo
DBDataBase.java
package dbmanager.action; /** * 数据库连接信息 * @author Dana·Li */ public class DBDataBase { public final String USERNAME="root"; public final String PASSWORD="123456"; public final String DBUOSTIP="192.168.0.129"; public final String ENCONDING="UTF-8"; public final String NAME="visecmc"; }
DBBackUp.java
package dbmanager.action; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * 数据库备份与还原 * @author Dana·Li */ public class DBBackUp { public final String BACKUP_COMMAND ="mysqldump"; public final String ENCODING ="utf8"; /** * 备份数据 * @param file * @return */ public boolean backup(String file) { boolean isSuccess =true; try { Runtime rt = Runtime.getRuntime(); String backupStr =this.getBackupStr(); Process process = rt.exec(backupStr); BufferedReader br =new BufferedReader(new InputStreamReader(process.getInputStream(), ENCODING)); String inStr =""; StringBuffer sb =new StringBuffer(""); while ((inStr = br.readLine()) !=null) { sb.append(inStr).append(""); } String outStr = sb.toString(); OutputStreamWriter writer =new OutputStreamWriter(new FileOutputStream(file), ENCODING); writer.write(outStr); writer.flush(); br.close(); writer.close(); }catch (Exception e) { e.printStackTrace(); isSuccess =false; } return isSuccess; } /** * 执行语句 * @return */ private String getBackupStr() { DBDataBase db=new DBDataBase(); String backupStr = BACKUP_COMMAND +" -u" + db.USERNAME +" -p" + db.PASSWORD +" -h" + db.DBUOSTIP +" --set-charset=" + db.ENCONDING +" " + db.NAME; return backupStr; } }
DBRevert.java
package dbmanager.action; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /** * 数据库还原 * @author Dana·Li */ public class DBRevert { public final String REVERT_COMMAND ="mysql"; public final String ENCODING ="utf8"; /** * 数据还原 * @param file * @return */ public boolean revert(String file) { try { Runtime rt = Runtime.getRuntime(); String revertStr =this.getRevertStr(); Process process = rt.exec(revertStr); String inStr; StringBuffer sb =new StringBuffer(""); BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING)); while ((inStr = br.readLine()) !=null) { sb.append(inStr).append(""); } String outStr = sb.toString(); OutputStreamWriter writer =new OutputStreamWriter(process.getOutputStream(), ENCODING); writer.write(outStr); writer.flush(); br.close(); writer.close(); }catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 执行操作 * @return */ private String getRevertStr() { DBDataBase db=new DBDataBase(); String backupStr = REVERT_COMMAND +" -u" + db.USERNAME +" -p" + db.PASSWORD +" -h" + db.DBUOSTIP +" --set-charset=" + db.ENCONDING +" " + db.NAME; return backupStr; } }