Class: PgUtils::Backup
- Inherits:
-
Object
- Object
- PgUtils::Backup
- Defined in:
- lib/pg_utils/backup.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#local_database ⇒ Object
Returns the value of attribute local_database.
-
#local_database_username ⇒ Object
Returns the value of attribute local_database_username.
-
#local_folder ⇒ Object
Returns the value of attribute local_folder.
-
#remote_backup_folder ⇒ Object
Returns the value of attribute remote_backup_folder.
-
#remote_database_name ⇒ Object
Returns the value of attribute remote_database_name.
-
#remote_database_password ⇒ Object
Returns the value of attribute remote_database_password.
-
#remote_database_username ⇒ Object
Returns the value of attribute remote_database_username.
-
#ssh_host ⇒ Object
Returns the value of attribute ssh_host.
-
#ssh_password ⇒ Object
Returns the value of attribute ssh_password.
-
#ssh_port ⇒ Object
Returns the value of attribute ssh_port.
-
#ssh_user ⇒ Object
Returns the value of attribute ssh_user.
Instance Method Summary collapse
-
#initialize ⇒ Backup
constructor
A new instance of Backup.
- #run ⇒ Object
Constructor Details
#initialize ⇒ Backup
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
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/pg_utils/backup.rb', line 6 def filename @filename end |
#local_database ⇒ Object
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_username ⇒ Object
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_folder ⇒ Object
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_folder ⇒ Object
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_name ⇒ Object
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_password ⇒ Object
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_username ⇒ Object
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_host ⇒ Object
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_password ⇒ Object
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_port ⇒ Object
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_user ⇒ Object
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
#run ⇒ Object
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 |