Java执行Mysql数据库备份/还原命令

在实际系统当中,有时会出现数据丢失的情况,为避免此类情况的发生,就需要我们对数据库文件进行备份,从而在意外发生时,可及时恢复。
由于以前执行Mysql命令行都是在终端中执行的,而没有尝试过在java中如何执行。在最近的学习当中,正好遇到了这个内容,就记录一下,方便以后查找。
在备份数据的时候需要注意,最终保存的文件是.sql文件,在执行恢复的时候,执行的也是.sql文件。

public class BackupAndRestore {

    /**
     * 备份数据库到指定到文件中
     * @param host host地址
     * @param username 用户名
     * @param password 密码
     * @param backupFilePath 备份的文件路径
     * @param database 备份的数据库名称
     * @return
     * @throws Exception
     */
    public static boolean backup(String host, String username, String password, String backupFilePath, String database) throws Exception {
        // mysqldump  -hlocalhost -uroot -ppassword --result-file=/Users/liyanfei/MyCode/mango-platform/mango-backup/dev/mango4.sql --default-character-set=utf8 mango
        // 通过StringBuilder拼接Mysql备份命令;
        StringBuilder stringBuilder = new StringBuilder();  // 可变字符串
        stringBuilder.append("mysqldump ");
        stringBuilder.append(" -h").append(host).append(" -u").append(username).append(" -p").append(password);
        stringBuilder.append(" --result-file=").append(backupFilePath).append(" --default-character-set=utf8 ").append(database);
        // 调用外部执行exe文件的java api
        Process process = Runtime.getRuntime().exec(getCommand(stringBuilder.toString()));
        if (process.waitFor() == 0) {
            // 0表示线程正常终止
            System.out.println("数据已经备份到 " + backupFilePath + " 文件中");
            return true;
        }
        return false;
    }

    /**
     * 恢复数据库备份文件
     * @param restoreFilePath 备份文件所在路径地址
     * @param host host地址
     * @param username 用户名
     * @param password 密码
     * @param database 还原的数据库名称
     * @return
     * @throws Exception
     */
    public static boolean restore(String restoreFilePath, String host, String username, String password, String database) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("mysql -h").append(host).append(" -u").append(username).append(" -p").append(password);
        stringBuilder.append(" ").append(database).append(" < ").append(restoreFilePath);
        try {
            Process process = Runtime.getRuntime().exec(getCommand(stringBuilder.toString()));
            if (process.waitFor() == 0) {
                System.out.println("数据已从 " + restoreFilePath + " 导入到数据库中");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static String[] getCommand(String command) {
        String os = System.getProperty("os.name");
        String shell = "/bin/bash";
        String c = "-c";
        if(os.toLowerCase().startsWith("win")){
            shell = "cmd";
            c = "/c";
        }
        String[] cmd = { shell, c, command };
        return cmd;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值