packagecom.ly.jxc.util;importjava.io.IOException;importjava.text.SimpleDateFormat;importjava.util.Date;public classDBRecover {/**访问MySQL数据库服务器所在的url*/
privateString serverUrl;/**访问MySQL数据库的用户名*/
privateString username;/**访问MySQL数据库的密码*/
privateString password;publicString getServerUrl() {returnserverUrl;
}public voidsetServerUrl(String serverUrl) {this.serverUrl =serverUrl;
}publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicDBRecover(String serverUrl,String username, String password) {super();this.serverUrl=serverUrl;this.username =username;this.password =password;
}public String backup(String backupPath, String dbName) throwsIOException {
String backupFile= backupPath+ new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".sql";
String mysql=backupPath+"mysqldump "+"--host="+serverUrl+" --user=" + username + " --password="+ password + " --opt " + dbName + "> "+backupFile ;
System.out.println("备份"+mysql);
java.lang.Runtime.getRuntime().exec("cmd /c " +mysql);
System.out.println("备份成功!");returnbackupFile;
}public void restore(String restoreFile, String dbName,String path) throwsException {
String mysql= path+"mysql "+"-h"+serverUrl+" -u" + username + " -p"+ password + " " + dbName + " < " +restoreFile;
System.out.println("+++++++++++++++++++++++++++"+mysql);
java.lang.Runtime.getRuntime().exec("cmd /c " +mysql);
System.out.println("还原成功!");
}/***@paramargs*/
public static voidmain(String[] args) {
String serverUrl="192.168.1.6";
String userName= "root";
String pwd= "root";
DBRecover backup= newDBRecover(serverUrl,userName, pwd);try{
backup.backup("", "lyjxc");//backup.restore("d:/nationz2010-12-20-14-16-47.sql", "nationz");
} catch(Exception e) {
e.printStackTrace();
}
}
}