达梦数据库备份还原

最近在做一个功能,是通过Java实现数据库备份还原,查看网上存在的多个资源。之前有写过MYSQL的备份与还原,现在把我达梦数据库实现备份还原的功能代码分享出来,达梦数据库实现 备份还原过程比较难,主要是对达梦数据库不太熟悉,以下的代码希望可以帮到那些正在寻找实现数据库备份还原的人。

import java.io.*;
import java.util.Date;

public class DmDatabaseBackupAndRestore {
    public static void resdStreamInfo(InputStream... inputStreams) {

        for (InputStream in : inputStreams) {
            new Thread(() -> {
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        System.out.println("数据" + line);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (IOException Ex) {
                        Ex.printStackTrace();
                    }
                }
            }).start();
        }
    }

    /**
     * 达梦数据库备份
     * param filePath 文件备份路径  路径格式 D:/code/backup/ 以实际情况而定
     * param binPath 数据库bin路径  路径格式 D:/dm/bin/ 以实际情况而定
     * param ip  数据库登录IP  以实际情况而定
     * param databasePort  数据库登端口  以实际情况而定
     * param password  数据库密码  root 以实际情况而定
     * param databasename 数据库名称  以实际情况而定
     */
    public static void DmDatabaseBackup(String filePath, String binPath, String ip, String databasePort, String password, String databasename) {
        String dirfire = filePath;
        File file = new File(dirfire);
        if (!file.exists()) {
            file.mkdir();
        }
        String filename = "backup_" + new Date().getTime() + ".dmp";
        File datafile = new File(file + File.separator + filename);
        if (datafile.exists()) {
            System.out.println("文件名已存在,请更换");
        }
        try {
            Runtime rt = Runtime.getRuntime();
            Process process = null;
            String IpAdd = ip + ":" + databasePort;
            //OWNER 对应的是要备份的用户名 
            String cmd = "cmd /c " + binPath + "/dexp " + databasename + "/" + password + "@" + IpAdd + " file=" + filename + " DIRECTORY=" + filePath + " OWNER=KMP";
            process = rt.exec(cmd);
            InputStream errorStream = process.getErrorStream();
            InputStream inputStream = process.getInputStream();
            resdStreamInfo(errorStream, inputStream);
            int i = process.waitFor();
            process.destroy();
            if (i == 0) {
                System.out.println("数据库备份成功");
                //此处可以添加代码,把以上操作的信息保存在相应的数据库中
            }
        } catch (Exception e) {
            System.out.println("备份数据库失败");
        }
    }

    /**
     * 达梦数据库还原
     * param ip       数据库连接的IP地址 如:127.0.0.1  以实际情况而定
     * param filePath 文件备份路径  路径格式 D:/code/backup/ 以实际情况而定
     * param binPath 数据库bin路径  路径格式 D:/dm/bin/ 以实际情况而定
     * param ip  数据库IP 以实际情况而定
     * param databasePort  数据库端口 以实际情况而定
     * param password  数据库密码  root 以实际情况而定
     * param databasename 数据库名称  以实际情况而定
     */
    public static void DmDatabaseRestore(String filePath, String binPath, String filename, String ip, String databasePort, String password, String databasename) {
        try {
            Runtime rt = Runtime.getRuntime();
            String os = System.getProperty("os.name");

            String IpAdd = ip + ":" + databasePort;
            //达梦数据库还原   table_first=y table_exists_action=replace   TABLE_EXISTS_ACTION参数有四个选项,REPLACE:先删除现有表,再导数据
            String cmd = "cmd /c " + binPath + "/dimp " + databasename + "/" + password + "@" + IpAdd + " file=" + filename + " DIRECTORY=" + filePath + " table_exists_action=replace OWNER=KMP";

            Process restoreData = rt.exec(cmd);
            InputStream errorStream = restoreData.getErrorStream();
            InputStream inputStream = restoreData.getInputStream();
            resdStreamInfo(errorStream, inputStream);
            int i = restoreData.waitFor();
            restoreData.destroy();
            if (i == 0) {
                System.out.println("数据库备份成功");
                //此处可以添加代码,把以上操作的信息保存在相应的数据库中
            }
        } catch (Exception e) {
            System.out.println("备份数据库失败");
        }
    }

}
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值