Class: ServerBackups::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/server_backups/config.rb

Constant Summary collapse

DEFAULT_LOGFILE_SIZE =
1.megabytes
DEFAULT_LOGFILE_COUNT =
7
FILE_LOCATION =

General

'/var/www'
MYSQLDUMP =
'mysqldump'
MYSQL =
'mysql'
MYSQLBINLOG =
'mysqlbinlog'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Config

Returns a new instance of Config.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/server_backups/config.rb', line 16

def initialize(config_file = nil)
    if config_file == '-'
        @config_file = config_file
        @config = Config::config ||= YAML::safe_load STDIN.read()
    else
        @config_file = config_file || default_config_file
        @config = YAML.load_file File.expand_path(config_file)
    end

    @logging = @config['logging']
    @logger = Logger.new(log_device, logfile_count, logfile_size)
rescue Errno::ENOENT => e
    puts e.backtrace
    warn "missing config file #{config_file}"
    exit 1
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



10
11
12
# File 'lib/server_backups/config.rb', line 10

def config_file
  @config_file
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/server_backups/config.rb', line 10

def logger
  @logger
end

#loggingObject (readonly)

Returns the value of attribute logging.



10
11
12
# File 'lib/server_backups/config.rb', line 10

def logging
  @logging
end

Class Method Details

.get_time_zone(name) ⇒ Object



34
35
36
37
38
# File 'lib/server_backups/config.rb', line 34

def get_time_zone(name)
    ActiveSupport::TimeZone.all.find do |time_zone|
        time_zone.name.casecmp(name).zero? || time_zone.tzinfo.name.casecmp(name).zero?
    end
end

Instance Method Details

#access_key_idObject

S3



175
176
177
# File 'lib/server_backups/config.rb', line 175

def access_key_id
    s3['access_key_id']
end

#bin_logObject



151
152
153
# File 'lib/server_backups/config.rb', line 151

def bin_log
    mysql['bin_log']
end

#bin_pathObject



147
148
149
# File 'lib/server_backups/config.rb', line 147

def bin_path
    mysql['bin_path']
end

#bucketObject



183
184
185
# File 'lib/server_backups/config.rb', line 183

def bucket
    s3['bucket']
end

#databaseObject



143
144
145
# File 'lib/server_backups/config.rb', line 143

def database
    mysql['database']
end

#db_hostObject

MySQL



131
132
133
# File 'lib/server_backups/config.rb', line 131

def db_host
    mysql['host'] || '127.0.0.1'
end

#get_retention_threshold(backup_type) ⇒ Object



105
106
107
108
109
# File 'lib/server_backups/config.rb', line 105

def get_retention_threshold(backup_type)
    interval, quantity = @config['retention'][backup_type.to_s].first
    quantity = quantity.to_i + 1
    quantity.send(interval).ago
end

#log_deviceObject



83
84
85
# File 'lib/server_backups/config.rb', line 83

def log_device
    logging['use_stdout'] == true ? STDOUT : logging['logfile_path']
end

#logfile_countObject



97
98
99
100
101
102
103
# File 'lib/server_backups/config.rb', line 97

def logfile_count
    if logging['keep_files']
        logging['keep_files'].to_i
    else
        DEFAULT_LOGFILE_COUNT
    end
end

#logfile_sizeObject



87
88
89
90
91
92
93
94
95
# File 'lib/server_backups/config.rb', line 87

def logfile_size
    if logging['file_size']
        unit, quantity = @logging['file_size'].first
        quantity = quantity.to_i
        quantity.send(unit)
    else
        DEFAULT_LOGFILE_SIZE
    end
end

#mysql_binObject



167
168
169
# File 'lib/server_backups/config.rb', line 167

def mysql_bin
    File.join(bin_path, MYSQL)
end

#mysqlbinlog_binObject



163
164
165
# File 'lib/server_backups/config.rb', line 163

def mysqlbinlog_bin
    File.join(bin_path, MYSQLBINLOG)
end

#mysqldump_binObject



159
160
161
# File 'lib/server_backups/config.rb', line 159

def mysqldump_bin
    File.join(bin_path, MYSQLDUMP)
end

#notify_on_successObject



45
46
47
# File 'lib/server_backups/config.rb', line 45

def notify_on_success
    @config.fetch('slack', nil)&.fetch('notify_on_success', false)
end

#passwordObject



139
140
141
# File 'lib/server_backups/config.rb', line 139

def password
    mysql['password']
end

#prefixObject



191
192
193
# File 'lib/server_backups/config.rb', line 191

def prefix
    s3['prefix']
end

#regionObject



187
188
189
# File 'lib/server_backups/config.rb', line 187

def region
    s3['region']
end

#retain_dailies_afterObject



111
112
113
# File 'lib/server_backups/config.rb', line 111

def retain_dailies_after
    get_retention_threshold(:daily)
end

#retain_incrementals_afterObject



115
116
117
# File 'lib/server_backups/config.rb', line 115

def retain_incrementals_after
    get_retention_threshold(:incremental)
end

#retain_monthlies_afterObject



119
120
121
# File 'lib/server_backups/config.rb', line 119

def retain_monthlies_after
    get_retention_threshold(:monthly)
end

#retain_weeklies_afterObject



123
124
125
# File 'lib/server_backups/config.rb', line 123

def retain_weeklies_after
    get_retention_threshold(:weekly)
end

#secret_access_keyObject



179
180
181
# File 'lib/server_backups/config.rb', line 179

def secret_access_key
    s3['secret_access_key']
end

#slack_mention_on_failureObject



49
50
51
# File 'lib/server_backups/config.rb', line 49

def slack_mention_on_failure
    @config.fetch('slack', nil)&.fetch('mention_users_on_failure', [])
end

#slack_mention_on_successObject



53
54
55
# File 'lib/server_backups/config.rb', line 53

def slack_mention_on_success
    @config.fetch('slack', nil)&.fetch('mention_users_on_success', [])
end

#slack_webhookObject



41
42
43
# File 'lib/server_backups/config.rb', line 41

def slack_webhook
    @config.fetch('slack', nil)&.fetch('webhook')
end

#system_time_zoneObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/server_backups/config.rb', line 70

def system_time_zone
    tz = if @config['system_time_zone']
             @config['system_time_zone']
         elsif File.exist?('/etc/timezone')
             File.read('/etc/timezone')
         elsif File.exist?('/etc/localtime')
             %r{([\w_]+\/[\w_]+)$}.match(`ls -l /etc/localtime`).captures.first
         else
             'UTC'
         end
    self.class.get_time_zone(tz)
end

#time_zoneObject



66
67
68
# File 'lib/server_backups/config.rb', line 66

def time_zone
    @config['time_zone'] || 'UTC'
end

#userObject



135
136
137
# File 'lib/server_backups/config.rb', line 135

def user
    mysql['user']
end

#web_rootObject



62
63
64
# File 'lib/server_backups/config.rb', line 62

def web_root
    File.absolute_path(@config['file_location'] || FILE_LOCATION)
end