Implement MySQL database backup in Java

Source: Internet
Author: User

Implement MySQL database backup in Java

In my spare time, I would like to summarize how to back up MySQL databases using Java:

Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;
Import java. io. PrintWriter;

/**
* MySQL Database Backup
*
* @ Author GaoHuanjie
*/
Public class MySQLDatabaseBackup {

/**
* Java code for MySQL Database Export
*
* @ Author GaoHuanjie
* @ Param hostIP the IP address of the MySQL database server
* @ Param userName the userName required to enter the database
* @ Param userName the userName required to enter the database
* @ Param password the password required to enter the database
* @ Param savePath: path for saving the exported database file
* @ Param fileName: name of the exported file in the database
* @ Param databaseName name of the database to be exported
* @ Return true indicates that the export is successful; otherwise, false is returned.
*/
Public static boolean exportDatabaseTool (String hostIP, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException {
File saveFile = new File (savePath );
If (! SaveFile. exists () {// If the directory does not exist
SaveFile. mkdirs (); // create a folder
}
If (! SavePath. endsWith (File. separator )){
SavePath = savePath + File. separator;
}

PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
Try {
PrintWriter = new PrintWriter (new OutputStreamWriter (new FileOutputStream (savePath + fileName), "utf8 "));
Process process = runtime.getruntime(cmd.exe c ("mysqldump-h" + hostIP + "-u" + userName + "-p" + password + "-- set-charset = UTF8" + databaseName );
InputStreamReader inputStreamReader = new InputStreamReader (process. getInputStream (), "utf8 ");
BufferedReader = new BufferedReader (inputStreamReader );
String line;
While (line = bufferedReader. readLine ())! = Null ){
PrintWriter. println (line );
}
PrintWriter. flush ();
If (process. waitFor () = 0) {// 0 indicates that the thread is terminated normally.
Return true;
}
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
If (bufferedReader! = Null ){
BufferedReader. close ();
}
If (printWriter! = Null ){
PrintWriter. close ();
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
Return false;
}
 
Public static void main (String [] args ){
Try {
If (exportDatabaseTool ("172.16.0.127", "root", "123456", "D:/backupDatabase", "2014-10-14. SQL", "test ")){
System. out. println ("the database is successfully backed up !!! ");
} Else {
System. out. println ("database backup failed !!! ");
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

Java code for MySQL database backup and import

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.