Home >Database >Mysql Tutorial >Mysql数据库备份与还原_MySQL

Mysql数据库备份与还原_MySQL

WBOY
WBOYOriginal
2016-06-01 13:43:151021browse

bitsCN.com

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

public class BackupMysql {
 /** 访问MySQL数据库服务器所在的url */
 private String serverUrl;
 /** 访问MySQL数据库的用户名 */
 private String username;
 /** 访问MySQL数据库的密码 */
 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().exec("cmd /c " + mysql);
  
  System.out.println("备份成功!");
 
  return backupFile;

 }

 public void restore(String restoreFile, String dbName) throws Exception {
 
  String mysql = "mysql "+"-h"+serverUrl+" -u" + username + " -p"+ password + " " + dbName + "  
        System.out.println(mysql);
       
  java.lang.Runtime.getRuntime().exec("cmd /c " + mysql);
 
  System.out.println("还原成功!");
 }

 /**
  * @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();
  }
 }
}

 
摘自 gdn_wolf的专栏

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn