程序实现postgresql备份还原

来源:互联网 发布:linux shell sleep 编辑:程序博客网 时间:2024/05/27 19:25

1、windows下Java程序备份还原:

①、备份:

String command = "cmd /c pg_dump -h "+hostName+" -o -U "+userName+" -F t -d "+dbName+" > " + pathName + File.separator  + fileName;  

Runtime.getRuntime().exec(command);

②、还原:

String command = "cmd /c pg_restore -h "+hostName+" -U "+userName+" -c -d "+dbName+" < " + fileNamePath;

Runtime.getRuntime().exec(command);

2、linux下Java程序备份还原:

①、备份:
String[] command = new String[]{"sh","-c","pg_dump -h "+hostName+" -o -U "+userName+" -F t -d "+dbName+" > " + pathName + File.separator +fileName}; 
Runtime.getRuntime().exec(command);

②、还原:

String[] command = new String[]{"sh","-c","pg_restore -h "+hostName+" -U "+userName+" -c -d "+dbName+" < " + fileNamePath};

Runtime.getRuntime().exec(command);