Implementing database backup and restore [inductive three ways]

Source: Internet
Author: User
Tags readline mysql backup stringbuffer

Java for database backup and restore [MySQL for example] the backup and restore of database data is best implemented using the client software that comes with the database, Java can be restored by calling Mysqldump, MySQL client software separately for MySQL database backup , of course, as long as the Java server must install the MySQL client software, but also through the Java telnet to the database server for backup, related information about Telnet please google.

Backup methods supported by MySQL:

1. Use SELECT INTO ... OUTFILE, such as SQL code

The generated files are stored on the database server. SELECT into backs up only the data in the database tables: The following is the file content after the category backup, which is the data record, and there is no data table structure
2. Use Backup TABLE, for example SQL code

Backup is for MyISAM forms only, and in the reference Manual of MySQL 5.1 "Note: This statement is not ideal"; The backup statement effect is to copy the. frm file from the database table to the database server destination directory

3. Use the mysqldump program or the mysqlhotcopy script Instructions for mysqldump in the MYSQL 5.1 reference manual: "Can be used to dump a database or collect a database for backup or to transfer data to another SQL Server (not necessarily a MySQL server). The dump contains SQL statements that create tables and/or Mount tables. "Java can invoke system commands using the EXEC (String str) method of the Process class, so you need to install the MYSQLDUMP program on the server to complete the backup, and you can back up the remote database server (configure the hostname parameter). Backup statement specific parameters reference MySQL reference manual mysql backup command line: SQL code

Mysqldump-hhostname-uusername-ppassword databasename > ' backupfile '

MySQL restore command line: SQL code

Mysql-hhostname-uusername-ppassword DatabaseName < ' backupfile

The following is a method of demo

Dbdatabase.java

Package dbmanager.action;/** * Database connection information * @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;ImportJava.io.BufferedReader;ImportJava.io.FileOutputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;/** * Database backup and restore * @author Dana Li*/PublicClassDBBackUp {Public final String Backup_command ="Mysqldump";Public final String ENCODING ="UTF8";/** * Backup Data * @param file * @return*/PublicBooleanBackup (String file) {Boolean issuccess =True;Try{Runtime RT =Runtime.getruntime (); String Backupstr =This. GETBACKUPSTR (); Process Process =Rt.exec (BACKUPSTR); BufferedReader br =New BufferedReader (NewInputStreamReader (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 (NewFileOutputStream (file), ENCODING); Writer.write (OUTSTR); Writer.flush (); Br.close (); Writer.close (); }Catch(Exception e) {e.printstacktrace (); issuccess =falsereturn issuccess;} /** * EXECUTE statement * @return */ Span style= "color: #0000ff;" >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;ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;/** * Database Restore * @author Dana Li*/PublicClassDbrevert {Public final String Revert_command ="MySQL";Public final String ENCODING ="UTF8";/** * Data Restore * @param file * @return*/PublicBooleanRevert (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 (NewFileInputStream (file), ENCODING));while ((inStr = Br.readline ())! =Null) {sb.append (INSTR). Append (""); } String outstr =Sb.tostring (); OutputStreamWriter writer =NewOutputStreamWriter (Process.getoutputstream (), ENCODING); Writer.write (OUTSTR); Writer.flush (); Br.close (); Writer.close (); }Catch(Exception e) {E.printstacktrace ();ReturnFalse; } return true;} /* * * Perform operation * @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;}}                

Implementing database backup and Restore [inductive three methods]

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.