diff --git a/lib/backy/configuration.rb b/lib/backy/configuration.rb index 8aa7b7e..ec8ccf9 100644 --- a/lib/backy/configuration.rb +++ b/lib/backy/configuration.rb @@ -10,6 +10,7 @@ class Configuration :s3_access_key, :s3_secret, :s3_bucket, + :s3_folder, :app_name, :environment, :log_file @@ -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 diff --git a/lib/backy/db.rb b/lib/backy/db.rb index f30732d..e87428c 100644 --- a/lib/backy/db.rb +++ b/lib/backy/db.rb @@ -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 diff --git a/lib/backy/pg_dump.rb b/lib/backy/pg_dump.rb index 2cf5b02..d810b1c 100644 --- a/lib/backy/pg_dump.rb +++ b/lib/backy/pg_dump.rb @@ -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) diff --git a/lib/backy/railtie.rb b/lib/backy/railtie.rb index 3940823..0ec5103 100644 --- a/lib/backy/railtie.rb +++ b/lib/backy/railtie.rb @@ -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 diff --git a/lib/backy/s3.rb b/lib/backy/s3.rb index cbfd28f..ee98b6c 100644 --- a/lib/backy/s3.rb +++ b/lib/backy/s3.rb @@ -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) diff --git a/lib/backy/s3_list.rb b/lib/backy/s3_list.rb index 90e2ee1..6659e7b 100644 --- a/lib/backy/s3_list.rb +++ b/lib/backy/s3_list.rb @@ -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) @@ -23,9 +17,5 @@ def call result.sort end - - private - - attr_reader :prefix end end diff --git a/lib/backy/s3_load.rb b/lib/backy/s3_load.rb index 422ae51..6f288fe 100644 --- a/lib/backy/s3_load.rb +++ b/lib/backy/s3_load.rb @@ -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 diff --git a/lib/backy/s3_save.rb b/lib/backy/s3_save.rb index 0474ebb..d1b5065 100644 --- a/lib/backy/s3_save.rb +++ b/lib/backy/s3_save.rb @@ -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