User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ruby:pg-sample [2019-11-05 13:03] (current)
ziggi created
Line 1: Line 1:
 +<code ruby pg-sample.rb>​
 +#​!/​usr/​bin/​env ruby
 +#
 +#  Copyright 2019 Oleg Borodin ​ <​borodin@unix7.org>​
 +#
 +
 +require '​pg'​
 +
 +class Log
 +    def initialize(filename)
 +        @log = File.open(filename,​ '​a'​)
 +    end
 +    def puts(string)
 +        timestamp = Time.now.getlocal
 +        @log.puts "#​{timestamp} #​{string}"​
 +    end
 +end
 +
 +hosts = [ '​pgdbxx',​ '​pgdbxx',​ '​pgdbxx',​ '​pgdbxx'​ ]
 +
 +log = Log.new('​dump.log'​)
 +log.puts("​begin"​)
 +
 +hosts.each do |host|
 +    begin
 +        conn = PG.connect(host:​ host, dbname: '​postgres',​ user: '​xxxx',​ password: '​xxxxx'​)
 +    rescue
 +        log.puts "​error:​ cannot connect to #​{host}"​
 +        next
 +    end
 +
 +    conn.exec( "​SELECT datname FROM pg_database ORDER BY datname"​ ) do |result|
 +        result.each do |row|
 +            datname = row.values_at('​datname'​)[0]
 +            if datname.match(/​^media_[a-z]{2,​3}_arch[0-9]{1,​2}$/​)
 +                filename = datname + '​.sqlz'​
 +                if !File.file?​(filename)
 +                    log.puts "​create dump for #​{datname}"​
 +                end
 +            end
 +        end
 +    end
 +end
 +</​code>​