class PgUtils::Backup

Attributes

filename[RW]
local_database[RW]
local_database_username[RW]
local_folder[RW]
remote_backup_folder[RW]
remote_database_name[RW]
remote_database_password[RW]
remote_database_username[RW]
ssh_host[RW]
ssh_password[RW]
ssh_port[RW]
ssh_user[RW]

Public Class Methods

new() click to toggle source
# File lib/pg_utils/backup.rb, line 10
def initialize()
  PgUtils.configuration.instance_variables.each do |k|
    instance_variable_set(k, PgUtils.configuration.instance_variable_get(k))
  end
end

Public Instance Methods

run() click to toggle source
# File lib/pg_utils/backup.rb, line 16
def run()
  system("mkdir -p #{local_folder}")
  datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
  @filename = "#{remote_database_name}_#{datestamp}_dump.sql.gz"
  backup_file = "#{remote_backup_folder}/#{filename}"

  Net::SSH.start(ssh_host, ssh_user, :password => remote_database_password, :port => ssh_port) do |ssh|
    puts "connected to #{ssh_host}"
    ssh.exec!("mkdir -p #{remote_backup_folder}")
    exec_cmd =  "PGPASSWORD=\"#{remote_database_password}\" pg_dump -Fc -h localhost -U #{remote_database_username} #{remote_database_name} > #{backup_file}"
    ssh.exec!(exec_cmd)
    ssh.scp.download!(backup_file, local_folder)
  end
  puts "Backup finished successfully!"
end