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



54
55
56
57
58
59
60
61
62
# File 'lib/boring_backup/configuration.rb', line 54

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

#ignore_tablesObject



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

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

#db_configObject



100
101
102
103
# File 'lib/boring_backup/configuration.rb', line 100

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

#notifier(type) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/boring_backup/configuration.rb', line 78

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



90
91
92
93
94
95
96
97
98
# File 'lib/boring_backup/configuration.rb', line 90

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)

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/boring_backup/configuration.rb', line 64

def register(store_type)
  raise BoringBackup::ConfigurationError, "`config.register` requires a block" unless block_given?

  store =
    case store_type.to_sym
    when :s3 then BoringBackup::Stores::S3.new
    else raise BoringBackup::ConfigurationError, "unknown store type: #{store_type}"
    end

  @stores << store

  yield 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