Class: PgUtils::Restore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRestore

Returns a new instance of Restore.



8
9
10
11
12
# File 'lib/pg_utils/restore.rb', line 8

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/restore.rb', line 6

def filename
  @filename
end

#local_databaseObject

Returns the value of attribute local_database.



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

def local_database
  @local_database
end

#local_database_passwordObject

Returns the value of attribute local_database_password.



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

def local_database_password
  @local_database_password
end

#local_database_usernameObject

Returns the value of attribute local_database_username.



6
7
8
# File 'lib/pg_utils/restore.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/restore.rb', line 6

def local_folder
  @local_folder
end

Instance Method Details

#run(filename = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pg_utils/restore.rb', line 14

def run(filename = nil)
  if filename && filename.include?(".sql.gz")
    backup_file = "#{local_folder}/#{filename}"
  elsif !filename.nil?
    backup_file = "#{local_folder}/#{filename}.sql.gz"
  else
    backup_file = Dir.glob("#{local_folder}/*").max_by {|f| File.mtime(f)}
  end
  puts "restoring backup: #{backup_file}"
  exec_cmd =  "PGPASSWORD=\"#{local_database_password}\" pg_restore -c -h localhost -U #{local_database_username} -Fc -d #{local_database} #{backup_file} --verbose"
  system(exec_cmd)
end