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

Source: Internet
Author: User
Tags mysql version 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,

650) this.width=650; "alt=" 080916411826719.png "src=" Http://images.cnitblog.com/i/545946/201405/080916411826719.png "/>

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--     </init-param>   </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 class backup{2privateint  ID; 3 Private String name; 4 Private Long size; 5 Private Date adddate; 6 7 // Setter and Getter 8 }



650) this.width=650; "alt=" 080927044179052.png "src=" Http://images.cnitblog.com/i/545946/201405/080927044179052.png "/>

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 PublicInterfaceBackupworker {23 PublicvoidBackupBooleanIsrestore)throwsException;45 PublicvoidReload (String path)throwsException;67/**8* Whether the backup was successful9* @method Name:isdoneTen     * @returnType:boolean One     *    @return A*/ - PublicBooleanIsDone (); - 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 PublicLongGetFileSize (); - to/** +* Gets the time interval for automatic backups in the configuration file, in hours -* @method name:gethours the     * @returnType:int *     *    @return $*/Panax Notoginseng PublicintgetHours (); - the/** +* Update the time interval for automatic backups A* @method name:sethours the     * @returntype:void +     *    @paramhours -*/ $ PublicvoidSethours (inthours); $ -/** -* Delete backup files the* @method Name:delete -     * @returntype:voidWuyi     *    @paramFileName the*/ - PublicvoidDelete (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:

650) this.width=650; "alt=" 080937019012155.png "src=" Http://images.cnitblog.com/i/545946/201405/080937019012155.png "/>

(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 PublicvoidBackupBooleanISRE)throwsException {2BooleanIsWindow =Iswindowsos ();3Isrestore =Isre;4if(IsWindow) {5 This. Backupwindow ();6}Else{7 This. Backuplinux ();8    }9}Ten One/** A* MySQL backup under Window -* @method Name:backupwindow - * @returntype:void the*/ -PrivatevoidBackupwindow () { -Try { -String SQLPath = Bkpath +getbackupname (); +MkDir (SQLPath); - +StringBuffer SB =NewStringBuffer (); Asb.append (mysqlpath); atSb.append ("Mysqldump"); -Sb.append ("--opt"); -Sb.append ("-H"); -Sb.append (host); -Sb.append (""); -Sb.append ("--user="); insb.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"); thesb.append (dbName); + ASystem.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"


--------------------------------

Article Starter: http://www.cnblogs.com/nerve/p/3715487.html

--------------------------------


This article is from the "Random Studio" blog, be sure to keep this source http://suiji.blog.51cto.com/3074335/1408218

Related Article

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.