Skip to content
5 changes: 5 additions & 0 deletions lib/backy/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Configuration
:s3_access_key,
:s3_secret,
:s3_bucket,
:s3_folder,
:app_name,
:environment,
:log_file
Expand Down Expand Up @@ -51,6 +52,10 @@ def s3_bucket
@s3_bucket ||= ENV["S3_BUCKET"]
end

def s3_folder
@s3_folder ||= "db/dump/"
end

def app_name
@app_name ||= "backy"
end
Expand Down
6 changes: 6 additions & 0 deletions lib/backy/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Backy
module Db
extend Forwardable

def self.included(base)
base.define_singleton_method :use_pg_dump_option_if_supported do |option|
system("pg_dump --help | grep -q -- '#{option}'") ? option : ""
end
end

private

def_delegator "Backy.configuration", :pg_host, :host
Expand Down
2 changes: 1 addition & 1 deletion lib/backy/pg_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PgDump
include AppConfig

DUMP_DIR = "db/dump"
DUMP_CMD_OPTS = "--no-acl --no-owner --no-subscriptions --no-publications --exclude-table=awsdms_ddl_audit"
DUMP_CMD_OPTS = "--no-acl --no-owner --exclude-table=awsdms_ddl_audit #{use_pg_dump_option_if_supported('--no-subscriptions')} #{use_pg_dump_option_if_supported('--no-publications')}"

def call
FileUtils.mkdir_p(DUMP_DIR)
Expand Down
26 changes: 18 additions & 8 deletions lib/backy/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
module Backy
class Railtie < Rails::Railtie
initializer "railtie.configure_rails_initialization" do
Backy.configure do |config|
config.pg_host = ActiveRecord::Base.connection_db_config.configuration_hash[:host]
config.pg_port = ActiveRecord::Base.connection_db_config.configuration_hash[:port]
config.pg_database = ActiveRecord::Base.connection_db_config.configuration_hash[:database]
config.pg_username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]
config.pg_password = ActiveRecord::Base.connection_db_config.configuration_hash[:password]
config.app_name = Rails.application.class.name.split("::").first.underscore
config.environment = Rails.env
begin
Backy.configure do |config|
config.pg_host = ActiveRecord::Base.connection_db_config.configuration_hash[:host]
config.pg_port = ActiveRecord::Base.connection_db_config.configuration_hash[:port]
config.pg_database = ActiveRecord::Base.connection_db_config.configuration_hash[:database]
config.pg_username = ActiveRecord::Base.connection_db_config.configuration_hash[:username]
config.pg_password = ActiveRecord::Base.connection_db_config.configuration_hash[:password]
config.app_name = Rails.application.class.name.split("::").first.underscore
config.environment = Rails.env
end
rescue NoMethodError # Older rails version does not have the connection_db_config method
config.pg_host = ActiveRecord::Base.connection_config[:host]
config.pg_port = ActiveRecord::Base.connection_config[:port]
config.pg_database = ActiveRecord::Base.connection_config[:database]
config.pg_username = ActiveRecord::Base.connection_config[:username] || ActiveRecord::Base.connection_config[:user]
config.pg_password = ActiveRecord::Base.connection_config[:password]
config.app_name = Rails.application.class.name.split("::").first.underscore
config.environment = Rails.env
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/backy/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module S3
def_delegator "Backy.configuration", :s3_secret, :secret
def_delegator "Backy.configuration", :s3_bucket, :bucket
def_delegator "Backy.configuration", :s3_access_key, :access_key
def_delegator "Backy.configuration", :s3_folder, :folder

def s3
@s3 ||= Aws::S3::Client.new(region: region, credentials: s3_credentials)
Expand Down
12 changes: 1 addition & 11 deletions lib/backy/s3_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ module Backy
class S3List
include S3

DEFAULT_PREFIX = "db/dump/"

def initialize(prefix: nil)
@prefix = prefix || DEFAULT_PREFIX
end

def call
return [] unless s3_configured?

response = s3.list_objects(prefix: prefix, bucket: bucket)
response = s3.list_objects(prefix: folder, bucket: bucket)

result = response.contents.map(&:key)

Expand All @@ -23,9 +17,5 @@ def call

result.sort
end

private

attr_reader :prefix
end
end
8 changes: 2 additions & 6 deletions lib/backy/s3_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ def call

print "Loading #{key} from S3 ... "

FileUtils.mkdir_p("/tmp/#{File.dirname(file_name)}")
Tempfile.create(file_name) do |tempfile|
response_target = tempfile.path

begin
s3.get_object(response_target: response_target, key: key, bucket: bucket)
FileUtils.mkdir_p(File.dirname(file_name))
FileUtils.mv(response_target, file_name)
FileUtils.cp(response_target, file_name)
rescue Aws::S3::Errors::NoSuchKey
puts "error. No such key #{key}"
ensure
if File.exist?(tempfile.path)
tempfile.close
File.delete(tempfile.path)
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/backy/s3_save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def call
return
end

unless (File.file?(file_name) && File.size(file_name) > 25)
puts "error. #{file_name} seems to be more or less empty"

return
end

File.open(file_name, "rb") do |body|
s3.put_object(key: key, body: body, bucket: bucket, expires: expires)
end
Expand Down