User Tools

Site Tools


pg-sample.rb
#!/usr/bin/env ruby
#
#  Author, Copyright: Oleg Borodin <onborodin@gmail.com>
#
 
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