Mysql database backup and Restoration

Source: Internet
Author: User

Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Date;

Public class BackupMysql {
/** Access the url of the MySQL database server */
Private String serverUrl;
/** Username used to access the MySQL database */
Private String username;
/** Password used to access the MySQL database */
Private String password;

 
Public String getServerUrl (){
Return serverUrl;
}

Public void setServerUrl (String serverUrl ){
This. serverUrl = serverUrl;
}

Public String getUsername (){
Return username;
}

Public void setUsername (String username ){
This. username = username;
}

Public String getPassword (){
Return password;
}

Public void setPassword (String password ){
This. password = password;
}

Public BackupMysql (String serverUrl, String username, String password ){
Super ();
This. serverUrl = serverUrl;
This. username = username;
This. password = password;
}

Public String backup (String backupPath, String dbName) throws IOException {

String backupFile = backupPath + dbName + new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss"). format (new Date () + ". SQL ";

String mysql = "mysqldump" + "-- host =" + serverUrl + "-- user =" + username + "-- password =" + password + "-- opt" + dbName + "> "+ backupFile;

Java.lang.runtime.getruntime(cmd.exe c ("cmd/c" + mysql );

System. out. println ("backup successful! ");
 
Return backupFile;

}

Public void restore (String restoreFile, String dbName) throws Exception {
 
String mysql = "mysql" + "-h" + serverUrl + "-u" + username + "-p" + password + "" + dbName + "<" + restoreFile;
 
System. out. println (mysql );

Java.lang.runtime.getruntime(cmd.exe c ("cmd/c" + mysql );
 
System. out. println ("restored successfully! ");
}

/**
* @ Param args
*/
Public static void main (String [] args ){

String serverUrl = "127.0.0.1 ";
 
String userName = "root ";

String pwd = "root ";

BackupMysql backup = new BackupMysql (serverUrl, userName, pwd );

Try {
Backup. restore ("d:/nationz2010-12-20-14-16-47. SQL", "nationz ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

 
From the column gdn_wolf

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.