Home > Database > Mysql Tutorial > body text

java实现的MySQL自动备份和还原(struts2+Hibernate)---兼容 wi_MySQL

WBOY
Release: 2016-06-01 13:15:53
Original
1199 people have browsed it

HibernateStruts2

相信很多朋友都经历过数据库出问题的情况,我也同样(见我的上一篇博文:phpmyadmin误删表后的恢复过程(心惊胆跳啊)   )。如果数据很大或者很重要,那么恢复起来是相当困难的,所以我们在做一个相对完善的系统时,数据库的备份/还原功能是必不可少的。本文将在javaEE环境下实现MySQL的自动备份/还原,使用了struts2和hibernate框架,MySQL版本是5.1.16。

下图展示的是web application的执行流程,

Timer是在一个随着application启动而启动的servlet中初始化,并接受一个名叫‘BACKUP_DEPLY'的参数,它告诉Timer多久去备份一次数据库(单位为小时):

 1 <servlet> 2 <servlet-name>StartupServlet</servlet-name> 3 <servlet-class>com.nerve.web.servlet.StartupServlet</servlet-class> 4  5 <load-on-startup>1</load-on-startup> 6  7 <init-param> 8 <param-name>BACKUP_DELAY</param-name> 9 <param-value>24</param-value><!--自动备份程序执行间隔,单位为小时-->10 </init-param>11 </servlet>
Copy after login

每次备份数据库后,会将备份信息保存到数据库中,为此我们定义了一个PO,如下:

1 public class Backup{2 private int id;3 private String name;4 private long size;5 private Date addDate;6 7 //setter and getter8 }
Copy after login

上图是备份过程中主要用到的类,大概过程为:

1.当Timer开始执行备份操作时,会调用BackupService(BackupServiceImpl为其实现类)中的backup()方法

2.BackupService会实例化一个BackWorker(MySQLBackupWorker为其实现类),并调用它的backup(boolean isRestore)throws Excetion方法来完成备份

3.当BackupWorker的isDone()方法返回true时,则表示备份成功(这时可以通过BackupWorker的getFileName()方法获取保存的文件名),否则是失败

BackupWorker接口定义如下:

 1 public interface BackupWorker { 2  3 public void backup(boolean isRestore) throws Exception; 4  5 public void reload(String path) throws Exception; 6  7 /** 8  * 是否备份成功 9  * @method name: isDone10  * @return type: boolean11  *@return12*/13 public boolean isDone();14 15 /**16  * 获取备份后文件的名称(不包括目录)17  * @method name: getFileName18  * @return type: String19  *@return20*/21 public String getFileName();22 23 /**24  * 获取备份后文件的大小25  * @method name: getFileSize26  * @return type: long27  *@return28*/29 public long getFileSize();30 31 /**32  * 获取配置文件中自动备份的时间间隔,单位为小时33  * @method name: getHours34  * @return type: int35  *@return36*/37 public int getHours();38 39 /**40  * 更新自动备份的时间间隔41  * @method name: setHours42  * @return type: void43  *@param hours44*/45 public void setHours(int hours);46 47 /**48  * 删除备份文件49  * @method name: delete50  * @return type: void51  *@param fileName52*/53 public void delete(String fileName);54 }
Copy after login

MySQLBackupWorker是MySQL的备份/还原类,实现了BackupWorker接口,在构造函数被执行时,会读取我们预设的MySQL相关设置(即上面类图中的MySQLBackup.properties),配置文件中包含如下内容:

 

(使用时,请将这些配置修改为本机的实际值,当OS为Linux是,mysqlpath可以不用设置,前提是 mysqldump 命令有效)

下面是MySQLBackupWorker中在window环境下的备份实现(默认的编码是utf-8,可以改为本机值):

 1 public void backup(boolean isRe) throws Exception { 2 boolean isWindow = isWindowsOS(); 3 isRestore = isRe; 4 if(isWindow){ 5 this.backupWindow(); 6 }else{ 7 this.backupLinux(); 8 } 9 }10 11 /**12  * window下的mysql备份13  * @method name: backupWindow14  * @return type: void15*/16 private void backupWindow(){17 try {18 String sqlPath = bkPath + getBackupName();19 mkDir(sqlPath);20 21 StringBuffer sb = new StringBuffer();22 sb.append(mysqlPath);23 sb.append("mysqldump ");24 sb.append("--opt ");25 sb.append("-h ");26 sb.append(host);27 sb.append(" ");28 sb.append("--user=");29 sb.append(loginName);30 sb.append(" ");31 sb.append("--password=");32 sb.append(loginPass);33 sb.append(" ");34 sb.append("--lock-all-tables=true ");35 sb.append("--result-file=");36 sb.append(sqlPath);37 sb.append(" ");38 sb.append("--default-character-set=utf8 ");39 sb.append(dbName);40 41 System.out.println(sb.toString());42 Runtime cmd = Runtime.getRuntime();43 try {44 Process p = cmd.exec(sb.toString());45 int tag = p.waitFor();46 System.out.println("result::: "+tag);47 if(tag == 0)48 done = true;49 } catch (IOException e) {50 e.printStackTrace();51 }52 } catch (Exception e) {53 e.printStackTrace();54 }55 }
Copy after login


获取更多源代码:源代码

 【本文为小弟原创文章,引用请注明出处,谢谢】

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!