Java-implemented MySQL automatic backup and restoration (struts2 + Hibernate) --- compatible with wi_MySQL

Source: Internet
Author: User
Tags mysql automatic backup mysql backup
Java-based MySQL automatic backup and restoration (struts2 + Hibernate) --- Compatible with Windows + Linux HibernateStruts2

I believe many of my friends have experienced database problems, and I am the same (see my previous blog post: the restoration process after phpmyadmin accidentally deletes the table )). If the data is large or important, it is very difficult to recover. Therefore, when we build a relatively complete system, the backup/restoration functions of the database are essential. This article will implement automatic MySQL backup/restoration in the javaEE environment, using the struts2 and hibernate frameworks, MySQL version is 5.1.16.

The execution process of web application is displayed,

Timer is initialized in a servlet started with the application and receives a parameter named 'backup _ deply', which tells Timer how often to back up the database (in hours ):

1
 
  
2
  
   
StartupServlet
  3
  
   
Com. nerve. web. servlet. StartupServlet
  4 5
  
   
1
  6 7
  
   
8
   
    
BACKUP_DELAY
   9
   
    
24
   
   10
  11
 

After each database is backed up, the backup information is saved to the database. Therefore, we define a PO as follows:

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 }

Is the main class used in the backup process, the approximate process is:

1. When Timer starts the backup operation, it will call the backup () method in BackupService (BackupServiceImpl for its implementation class ).

2. BackupService will instantiate a BackWorker (MySQLBackupWorker for its implementation class) and call its backup (boolean isRestore) throws Excetion method to complete the backup.

3. when the isDone () method of BackupWorker returns true, the backup is successful (the saved file name can be obtained through the getFileName () method of BackupWorker); otherwise, the backup fails.

The BackupWorker interface is defined as follows:

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 * whether the backup is successful 9 * @ method name: isDone10 * @ return type: boolean11 * @ return12 */13 public boolean isDone (); 14 15/** 16 * get the name of the backup file (excluding the directory) 17 * @ method name: getFileName18 * @ return type: string19 * @ return20 */21 public String getFileName (); 22 23/** 24 * get the size of the backup file 25 * @ method name: getFileSize26 * @ return type: long27 * @ return28 */29 public long getFileSize (); 30 31/** 32 * get the interval of automatic backup in the configuration file, in the unit of 33 hours * @ method name: getHours34 * @ return type: int35 * @ return36 */37 public int getHours (); 38 39/** 40 * update the automatic backup interval 41 * @ method name: setHours42 * @ return type: void43 * @ param hours44 */45 public void setHours (int hours); 46 47/*** 48 * delete backup file 49 * @ method name: delete50 * @ return type: void51 * @ param fileName52 */53 public void delete (String fileName); 54}

MySQLBackupWorker is a MySQL backup/restoration class that implements the BackupWorker interface. when the constructor is executed, it reads the preset MySQL settings (that is, MySQLBackup in the above class diagram. properties), the configuration file contains the following content:

(During use, modify these configurations to the actual value of the local machine. If OS is Linux, mysqlpath does not need to be set, provided that the mysqldump command is valid)

The following is the backup implementation of MySQLBackupWorker in the window environment (the default encoding is UTF-8 and can be changed to the local value ):

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 backup 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.exe c (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}


Get more source code: Source code

[This is an original article by younger brother. please specify the source for your reference. thank you]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.