• 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();
                    }
                }
            }        
        }
    }
    想的都是好
  • 相关阅读:
    模块与包的导入
    递归
    day04
    装饰器2_根据认证来源判断用户和计算登录时间
    装饰器1_统计时间函数装饰欢迎登录函数
    tail -f a.txt | grep 'python'
    函数
    内置函数1
    python模块整理
    VBS恶作剧代码
  • 原文地址:https://www.cnblogs.com/freezone/p/5056836.html
Copyright © 2020-2023  润新知