zoukankan      html  css  js  c++  java
  • java实现postgres备份功能

    package postgre_dump;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    import ch.ethz.ssh2.Connection;
    import ch.ethz.ssh2.Session;
    import ch.ethz.ssh2.StreamGobbler;
    
    
    public class Test_dump {
    
        public static void main(String[] args) {
            String host = "192.168.5.11";
            Connection conn = null;
             Session session = null;
             BufferedReader br = null;
            try {
                //建立连接
                conn = new Connection(host);
                conn.connect();
                //用户名和密码
                boolean status = conn.authenticateWithPassword("postgres", "123456");
                //链接是否成功
                if(status){
                      session = conn.openSession();
                     //运行的命令
                     //pg_dump -U postgres oesv11 -t oes.poi -f /home/share/01.数据备份/poi.sql
                     session.execCommand("pg_dump -U postgres oesv11 -t oes.poi -f /home/share/01.数据备份/poi.sql");
                     InputStream stdout = new StreamGobbler(session.getStdout());
                     br = new BufferedReader(new InputStreamReader(stdout));
                     String line = null;
                     while((line = br.readLine()) != null){
                         System.out.println(line);
                         System.out.println();
                     }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(br != null){
                    try {
                        br.close();
                        session.close();
                        conn.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }        
        }
    }
    想的都是好
  • 相关阅读:
    spring小结2:spring管理bean原理(转帖)
    JAVA Web.xml 加载顺序
    mysql into outfile导出方式
    从平安面试归来
    如何避开求职陷阱
    关注基金走势先~~
    如何谈薪资
    包含数据分析的坐标图测试
    面试题分享
    ShaiShai.net
  • 原文地址:https://www.cnblogs.com/freezone/p/5056836.html
Copyright © 2011-2022 走看看