Class: PgUtils::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_utils/backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackup

Returns a new instance of Backup.



10
11
12
13
14
# 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

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def filename
  @filename
end

#local_databaseObject

Returns the value of attribute local_database.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def local_database
  @local_database
end

#local_database_usernameObject

Returns the value of attribute local_database_username.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def local_database_username
  @local_database_username
end

#local_folderObject

Returns the value of attribute local_folder.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def local_folder
  @local_folder
end

#remote_backup_folderObject

Returns the value of attribute remote_backup_folder.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def remote_backup_folder
  @remote_backup_folder
end

#remote_database_nameObject

Returns the value of attribute remote_database_name.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def remote_database_name
  @remote_database_name
end

#remote_database_passwordObject

Returns the value of attribute remote_database_password.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def remote_database_password
  @remote_database_password
end

#remote_database_usernameObject

Returns the value of attribute remote_database_username.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def remote_database_username
  @remote_database_username
end

#ssh_hostObject

Returns the value of attribute ssh_host.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def ssh_host
  @ssh_host
end

#ssh_passwordObject

Returns the value of attribute ssh_password.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def ssh_password
  @ssh_password
end

#ssh_portObject

Returns the value of attribute ssh_port.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def ssh_port
  @ssh_port
end

#ssh_userObject

Returns the value of attribute ssh_user.



6
7
8
# File 'lib/pg_utils/backup.rb', line 6

def ssh_user
  @ssh_user
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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