如何使用springbootjava进行postgresql数据库表备份

drnojrws  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(286)

我正在尝试使用JavaProcessBuilder处理一些pgadmin表,但是代码为pgu dump抛出了错误

@PostMapping("/backup")

public void backup() throws Exception {
    System.out.println("BACK Up BACKEND ");
    System.out.println("Backup Started at " + new Date());

    Date backupDate = new Date();
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    String backupDateStr = format.format(backupDate);
    //String dbNameList = "client_1 client_2";
    String IP = "IP Address";
    String user = "postgres";
    String dbase = "postgres";
    String password = "1234567890";
    String host = "localhost";
    String port = "5432";
    String table = "test_temp";
    String url = "awsdatabase-2url";

    String fileName = "Daily_DB_Backup"; // default file name
    String folderPath = "C:/Downloads/Temp_Newfolder";
    File f1 = new File(folderPath);
    f1.mkdir(); // create folder if not exist

        System.out.println("line 155 Started at " + new Date());

        String saveFileName = fileName + "_" + backupDateStr + ".sql";
        String savePath = folderPath + File.separator + saveFileName;

    String executeCmd = "C:/Program Files (x86)/pgAdmin 4/v4/runtime/pg_dump.exe -u " + user +"-h"+ url+"-p"+port+"-t"+table+"--databases " + dbase+"-w"+password+" -r " + savePath;

    Process runtimeProcess = null;
        try {
            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            System.out.println("executecmd=====>"+runtimeProcess.toString());

            System.out.println("Command is done");
            runtimeProcess.waitFor();
            System.out.println("Process Wait is done");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
         System.out.println("line 172 Started at " + new Date());
        int processComplete = 0;
        try {
            processComplete = runtimeProcess.waitFor();
            System.out.println("line 174 Started at " + new Date());
            System.out.println("IN TRY processComplete:======> " + processComplete);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("BEFORE LOOP processComplete:======> " + processComplete);
        if (processComplete == 0) {
            System.out.println(" IN LOOP processComplete:======> " + processComplete);
            System.out.println("Backup Complete at " + new Date());
            System.out.println("line 181 Started at " + new Date());
        } else {
            System.out.println("Backup Failure");
            System.out.println("line 184 Started at " + new Date());
        }
    }

当我尝试上面的代码时,它会给我以下输出,其中runtimeprocess.waitfor()给值1,executecmd给我一些processimpl值。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题