Sha256: 01a10d78b57ee03934afba63dd9868203759af4fb86d2408bb3f95e704248d7a

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

#!/usr/bin/env ruby

require 'yaml'
require 'erb'
require 'optparse'
require_relative "#{Dir.pwd}/config/environment"

begin
  db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)
rescue
  puts "Database config couldn't be read, please make sure there is a config/database.yml"
end

env = nil
backup_location = nil
OptionParser.new do |opts|
  opts.banner = 'Usage: rails_pg_restore --backup=path/to/backup.dump'

  opts.on('-b', '--backup BACKUP_LOCATION',
          'Require the BACKUP_LOCATION before restoring') do |backup_loc|
    backup_location = backup_loc
  end

  opts.on('-e', '--environment [ENVIRONMENT]',
          'Specify the rails environment, defaults to development') do |e|
    env = e
  end
end.parse!

unless backup_location && File.exist?(backup_location)
  abort("Couldn't read a backup location at #{backup_location.inspect}")
end

env ||= 'development'

config_for_env = db_config[env] || {}

username = config_for_env['username']
db_name = config_for_env['database']
hostname = config_for_env['hostname'] || 'localhost'

unless username && db_name && hostname
  abort('Please ensure you config/database.yml contains a username and database name')
end

puts "Restoring #{db_name} from #{backup_location}.\nThis could take a few minutes"

command = <<SH
pg_restore --verbose --clean --no-acl --no-owner \
  -h #{hostname} \
  -U #{username} \
  -d #{db_name} \
  #{backup_location}
SH

puts "Executing #{command}"

result = exec(command)
puts result

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_pg_restore-0.3.0 bin/rails_pg_restore