Java-implemented MySQL automatic backup and restore (struts2+hibernate)---compatible window+linux

Source: Internet
Author: User
Tags mysql in mysql automatic backup mysql backup

I believe many friends have experienced database problems, I also (see my previous blog: phpMyAdmin mistakenly deleted the recovery process after the table (the courage to jump AH)). If the data is large or important, it is very difficult to recover, so when we are doing a relatively perfect system, the backup/restore function of the database is essential. This article will implement the automatic backup/restore of MySQL in Java EE environment, using the STRUTS2 and Hibernate framework, the MySQL version is 5.1.16.

Shows the execution flow of the Web application,

  

The timer is initialized in a servlet that starts with application and accepts a parameter called ' backup_deply ', which tells the timer how often to back up the database (in hours):

  

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><!--Automatic Backup program execution interval, in hours--Ten</init-param> One</servlet>

After each backup of the database, the backup information is saved to the database, for which we define a PO, as follows:

1  Public classbackup{2     Private intID;3     PrivateString name;4     Private Longsize;5     PrivateDate adddate;6     7     //Setter and Getter8}

  

  

  

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

1. When the timer starts performing a backup operation, it calls the backup () method in Backupservice (Backupserviceimpl for its implementation class)

2.BackupService instantiates a backworker (Mysqlbackupworker for its implementation class) and calls it's backup (Boolean Isrestore) throws Excetion method to complete the backup

3. When Backupworker's Isdone () method returns True, the backup is successful (at this point the saved file name can be obtained by Backupworker's GetFileName () method), otherwise it fails

  

The Backupworker interface is defined as follows:

1  Public InterfaceBackupworker {2 3      Public voidBackupBooleanIsrestore)throwsException;4     5      Public voidReload (String path)throwsException;6     7     /**8 * Whether the backup was successful9 * @method Name:isdoneTen      * @returnType:boolean One      *    @return A      */ -      Public BooleanIsDone (); -      the     /** - * Get the name of the file after backup (excluding directory) - * @method Name:getfilename -      * @returntype:string +      *    @return -      */ +      PublicString GetFileName (); A      at     /** - * Get the size of the file after backup - * @method name:getfilesize -      * @returnType:long -      *    @return -      */ in      Public LongGetFileSize (); -      to     /** + * Gets the time interval for automatic backups in the configuration file, in hours - * @method name:gethours the      * @returnType:int *      *    @return $      */Panax Notoginseng      Public intgetHours (); -      the     /** + * Update the time interval for automatic backups A * @method name:sethours the      * @returntype:void +      *    @paramhours -      */ $      Public voidSethours (inthours); $      -     /** - * Delete backup files the * @method Name:delete -      * @returntype:voidWuyi      *    @paramFileName the      */ -      Public voidDelete (String fileName); Wu}

Mysqlbackupworker is a backup/restore class for MySQL that implements the Backupworker interface and, when the constructor is executed, Will read our preset MySQL-related settings (mysqlbackup.properties in the class diagram above), and the configuration file contains the following:

(When used, modify these configurations to the actual values of the native, when the OS for Linux is, Mysqlpath can not be set, provided the mysqldump command is valid)

The following is the backup implementation in the window environment in Mysqlbackupworker (the default encoding is Utf-8, which can be changed to a native value):

1  Public voidBackupBooleanISRE)throwsException {2     BooleanIsWindow =Iswindowsos ();3Isrestore =Isre;4     if(IsWindow) {5          This. Backupwindow ();6}Else{7          This. Backuplinux ();8     }9 }Ten  One /** A * MySQL backup under Window - * @method Name:backupwindow -  * @returntype:void the  */ - Private voidBackupwindow () { -     Try { -String SQLPath = Bkpath +getbackupname (); + MkDir (SQLPath); -          +StringBuffer SB =NewStringBuffer (); A sb.append (mysqlpath); atSb.append ("Mysqldump"); -Sb.append ("--opt"); -Sb.append ("-H"); - Sb.append (host); -Sb.append (""); -Sb.append ("--user="); in sb.append (loginName); -Sb.append (""); toSb.append ("--password="); + sb.append (loginpass); -Sb.append (""); theSb.append ("--lock-all-tables=true"); *Sb.append ("--result-file="); $ sb.append (SQLPath);Panax NotoginsengSb.append (""); -Sb.append ("--default-character-set=utf8"); the sb.append (dbName); +          A System.out.println (sb.tostring ()); theRuntime cmd =runtime.getruntime (); +         Try { -Process p =cmd.exec (sb.tostring ()); $             intTag =p.waitfor (); $SYSTEM.OUT.PRINTLN ("Result:::" +tag); -             if(Tag = = 0) -Done =true; the}Catch(IOException e) { - e.printstacktrace ();Wuyi         } the}Catch(Exception e) { - e.printstacktrace (); Wu     } -}


Get more source code: source Code

"This is my brother's original article, quote please indicate the source, 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.