Windows环境下

在 RyTask 类下添加以下方法

public void windowsDump(String dir, String host, String port, String username, String password, String databasename) throws Exception {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        String sqlname = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        File datafile = new File(file + File.separator + sqlname + ".sql");
        if (datafile.exists()) {
            System.out.println(sqlname + "文件名已存在,请更换");
            return;
        }
        //拼接cmd命令  windows下 cmd   Linux下 /bin/sh
        Process exec = Runtime.getRuntime().exec("cmd /c /usr/bin/mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + datafile);
        if (exec.waitFor() == 0) {
            System.out.println("数据库备份成功,备份路径为:" + datafile);
        }
    }

然后启动项目进行如下配置
在这里插入图片描述

Linux环境下

public void linuxDump(String dir, String host, String port, String username, String password, String databasename) throws Exception {
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdir();
        }
        String sqlname = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        File datafile = new File(file + File.separator + sqlname + ".sql");
        if (datafile.exists()) {
            System.out.println(sqlname + "文件名已存在,请更换");
            return;
        }
        //拼接cmd命令  windows下 cmd   Linux下 /bin/sh
        Process exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "/usr/bin/mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + datafile});
        if (exec.waitFor() == 0) {
            System.out.println("数据库备份成功,备份路径为:" + datafile);
        }
    }

配置方式和Windows类似

Logo

快速构建 Web 应用程序

更多推荐