Class: BoringBackup::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/boring_backup/configuration.rb

Constant Summary collapse

DEFAULT_SENTINEL_HOST =
"https://boringbackup.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



17
18
19
20
21
22
23
24
25
26
# File 'lib/boring_backup/configuration.rb', line 17

def initialize
  @prefix = ENV.fetch("BB_PREFIX", "database")
  @stores = []
  @min_size = ENV.fetch("BB_MIN_SIZE", 2048).to_i
  @notifiers = [BoringBackup::Notifiers::Stdout.new]
  @report = true
  @ignore_tables = ENV.fetch("BB_IGNORE_TABLES", "").split(",")
  @sentinel_key = ENV["BB_SENTINEL_KEY"]
  @sentinel_host = ENV.fetch("BB_SENTINEL_HOST", DEFAULT_SENTINEL_HOST)
end

Instance Attribute Details

#dump_commandObject



58
59
60
61
62
63
64
65
66
# File 'lib/boring_backup/configuration.rb', line 58

def dump_command
  @dump_command || [
    pg_env,
    "pg_dump",
    "--format=custom",
    "--no-owner",
    *ignore_tables.map { |table| "--exclude-table-data=#{table}" }
  ]
end

#ignore_tablesObject



54
55
56
# File 'lib/boring_backup/configuration.rb', line 54

def ignore_tables
  Array(@ignore_tables).map { |table| table.to_s.strip }.reject(&:empty?).uniq
end

#min_sizeObject

Returns the value of attribute min_size.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def min_size
  @min_size
end

#pg_databaseObject

Returns the value of attribute pg_database.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def pg_database
  @pg_database
end

#pg_hostObject

Returns the value of attribute pg_host.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def pg_host
  @pg_host
end

#pg_passwordObject

Returns the value of attribute pg_password.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def pg_password
  @pg_password
end

#pg_portObject

Returns the value of attribute pg_port.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def pg_port
  @pg_port
end

#pg_userObject

Returns the value of attribute pg_user.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def pg_user
  @pg_user
end

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def prefix
  @prefix
end

#report=(value) ⇒ Object (writeonly)

Sets the attribute report

Parameters:

  • value

    the value to set the attribute report to.



15
16
17
# File 'lib/boring_backup/configuration.rb', line 15

def report=(value)
  @report = value
end

#sentinel_hostObject



32
33
34
# File 'lib/boring_backup/configuration.rb', line 32

def sentinel_host
  @sentinel_host || DEFAULT_SENTINEL_HOST
end

#sentinel_keyObject

Returns the value of attribute sentinel_key.



5
6
7
# File 'lib/boring_backup/configuration.rb', line 5

def sentinel_key
  @sentinel_key
end

#storesObject (readonly)

Returns the value of attribute stores.



14
15
16
# File 'lib/boring_backup/configuration.rb', line 14

def stores
  @stores
end

Instance Method Details

#build_s3_storeObject



94
95
96
97
98
99
100
101
# File 'lib/boring_backup/configuration.rb', line 94

def build_s3_store
  require_relative "stores/s3"

  BoringBackup::Stores::S3.new
rescue LoadError
  raise BoringBackup::ConfigurationError,
    "the :s3 store needs the aws-sdk-s3 gem. Add `gem \"aws-sdk-s3\"` to your Gemfile."
end

#db_configObject



113
114
115
116
# File 'lib/boring_backup/configuration.rb', line 113

def db_config
  @db_config ||= defined?(::ActiveRecord) ?
    ::ActiveRecord::Base.connection_db_config.configuration_hash : {}
end

#notifier(type) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/boring_backup/configuration.rb', line 82

def notifier(type)
  case type
  when :slack
    notifier = BoringBackup::Notifiers::Slack.new

    yield notifier

    @notifiers << notifier
  else raise "Unknown notifier: #{type}"
  end
end

#notifiersObject



46
47
48
# File 'lib/boring_backup/configuration.rb', line 46

def notifiers
  [*@notifiers, sentinel].compact
end

#pg_envObject



103
104
105
106
107
108
109
110
111
# File 'lib/boring_backup/configuration.rb', line 103

def pg_env
  {
    "PGHOST" => (pg_host || db_config[:host])&.to_s,
    "PGPORT" => (pg_port || db_config[:port])&.to_s,
    "PGUSER" => (pg_user || db_config[:username])&.to_s,
    "PGPASSWORD" => (pg_password || db_config[:password])&.to_s,
    "PGDATABASE" => (pg_database || db_config[:database])&.to_s
  }.compact
end

#register(store_type) {|store| ... } ⇒ Object

Yields:

  • (store)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/boring_backup/configuration.rb', line 68

def register(store_type)
  store =
    case store_type.to_sym
    when :s3 then build_s3_store
    else raise BoringBackup::ConfigurationError, "unknown store type: #{store_type}"
    end

  @stores << store

  yield store if block_given?

  store
end

#report?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/boring_backup/configuration.rb', line 28

def report?
  @report
end

#sentinelObject



40
41
42
43
44
# File 'lib/boring_backup/configuration.rb', line 40

def sentinel
  return unless sentinel?

  @sentinel ||= BoringBackup::Notifiers::Sentinel.new(key: sentinel_key, host: sentinel_host)
end

#sentinel?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/boring_backup/configuration.rb', line 36

def sentinel?
  !sentinel_key.to_s.empty?
end

#silence_stdout!Object



50
51
52
# File 'lib/boring_backup/configuration.rb', line 50

def silence_stdout!
  @notifiers.reject! { |notifier| notifier.is_a?(BoringBackup::Notifiers::Stdout) }
end