您的位置:首页 > 编程语言 > Java开发

Java 备份恢复MySQL数据库

2017-03-24 16:54 381 查看
/**

 * 备份数据库实体类

 * @author Administrator

 *

 */

public class SQLBac {

private String hostIP = "";//MySQL数据库所在服务器地址IP

private String userName = "";//进入数据库所需要的用户名

private String password = "";//进入数据库所需要的密码

private String savePath = ""//数据库导出文件保存路径

private String fileName = System.currentTimeMillis()+".sql";//数据库导出文件文件名

private String databaseName = "";//要导出的数据库名

private String FilePath; //保存文件的路径 

public String getFilePath() {
return FilePath;
}

public void setFilePath(String filePath) {
FilePath = filePath;
}

public String getHostIP() {
return hostIP;
}

public void setHostIP(String hostIP) {
this.hostIP = hostIP;
}

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 String getSavePath() {
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getDatabaseName() {
return databaseName;
}

public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}

}

/**

 * MySQL 数据库备份与恢复

 * @author Administrator

 *

 */

public class SQLBacUtil {

/**

     * Java代码实现MySQL数据库导出

     * 

     * @param hostIP MySQL数据库所在服务器地址IP

     * @param userName 进入数据库所需要的用户名

     * @param password 进入数据库所需要的密码

     * @param savePath 数据库导出文件保存路径

     * @param fileName 数据库导出文件文件名

     * @param databaseName 要导出的数据库名

     * @return 返回true表示导出成功,否则返回false。
     */

//cmd 语句

//C:\MySQL\bin>mysqldump -h172.16.12.44 -uroot -p123456 wdcms -r "D:\MyEclipse 2017 CI Workspaces\ssmcms\WebRoot\resource\data\2017-03-24\1490345865013.sql"

    public static boolean backup(SQLBac sqlBac) {

        String savePath = sqlBac.getSavePath();

        String fileName = savePath+"\\"+sqlBac.getFileName();

        File saveFile = new File(savePath);

        if (!saveFile.exists()) {// 如果目录不存在

            saveFile.mkdirs();// 创建文件夹

        }

        if (!savePath.endsWith(File.separator)) {

            savePath = savePath + File.separator;

        }

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("mysqldump -h").append(sqlBac.getHostIP());

        stringBuilder.append(" -u").append(sqlBac.getUserName()) .append(" -p").append(sqlBac.getPassword()).append(" "+sqlBac.getDatabaseName()).append(" -r ");

        stringBuilder.append("\""+fileName+"\"");

        try {

            Process process = Runtime.getRuntime().exec(stringBuilder.toString());

            if(process.waitFor() == 0){//0 表示线程正常终止。 并且返回保存地址

             sqlBac.setFileName(fileName);

                return true;  

            }

        }catch (Exception e) {  

            e.printStackTrace();  

        } 

        return false;  

    }

 

    /**

     * Java代码实现MySQL数据库导入

     * @param sqlBac

     * @return
     */

     //cmd 语句

    //C:\MySQL\bin>mysql -h172.16.12.44 -uroot -p123456  wdcms --execute=\"source D:\MyEclipse 2017 CI Workspaces\ssmcms\WebRoot\resource\data\2017-03-24\1490333324816.sql"

    public static boolean load(SQLBac sqlBac) { 

         StringBuilder stringBuilder = new StringBuilder();

         stringBuilder.append("mysql -h").append(sqlBac.getHostIP());

         stringBuilder.append(" -u").append(sqlBac.getUserName()) .append(" -p").append(sqlBac.getPassword()).append(" "+sqlBac.getDatabaseName()).append(" --execute=\"source ");

         stringBuilder.append(sqlBac.getFileName()+"\"");

         try {

             Process process = Runtime.getRuntime().exec(stringBuilder.toString());

             if(process.waitFor() == 0){//0 表示线程正常终止。 

                 return true;  

             }

             return true;

         }catch (Exception e) {  

             e.printStackTrace();  

         } 

         return false;  

    }

    public static void main(String[] args) {
SQLBac sqlBac = new SQLBac();
System.out.println(SQLBacUtil.backup(sqlBac));//直接传入对象
if(SQLBacUtil.backup(sqlBac)){ // 如果备份成功
System.out.println(sqlBac.getFileName());//获取备份地址
}else {
System.out.println("备份失败");
}
sqlBac.setFileName("备份文件地址");
sqlBac.setFileName(sqlBac);//传入数据库保存地址
System.out.println(SQLBacUtil.load(sqlBac));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: