class ServerBackups::Config

Constants

DEFAULT_LOGFILE_COUNT
DEFAULT_LOGFILE_SIZE
FILE_LOCATION

General

MYSQL
MYSQLBINLOG
MYSQLDUMP

Attributes

config_file[R]
logger[R]
logging[R]

Public Class Methods

get_time_zone(name) click to toggle source
# 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
new(config_file = nil) click to toggle source
# 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

Public Instance Methods

access_key_id() click to toggle source

S3

# File lib/server_backups/config.rb, line 175
def access_key_id
    s3['access_key_id']
end
bin_log() click to toggle source
# File lib/server_backups/config.rb, line 151
def bin_log
    mysql['bin_log']
end
bin_path() click to toggle source
# File lib/server_backups/config.rb, line 147
def bin_path
    mysql['bin_path']
end
bucket() click to toggle source
# File lib/server_backups/config.rb, line 183
def bucket
    s3['bucket']
end
database() click to toggle source
# File lib/server_backups/config.rb, line 143
def database
    mysql['database']
end
db_host() click to toggle source

MySQL

# File lib/server_backups/config.rb, line 131
def db_host
    mysql['host'] || '127.0.0.1'
end
get_retention_threshold(backup_type) click to toggle source
# 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_device() click to toggle source
# File lib/server_backups/config.rb, line 83
def log_device
    logging['use_stdout'] == true ? STDOUT : logging['logfile_path']
end
logfile_count() click to toggle source
# 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_size() click to toggle source
# 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_bin() click to toggle source
# File lib/server_backups/config.rb, line 167
def mysql_bin
    File.join(bin_path, MYSQL)
end
mysqlbinlog_bin() click to toggle source
# File lib/server_backups/config.rb, line 163
def mysqlbinlog_bin
    File.join(bin_path, MYSQLBINLOG)
end
mysqldump_bin() click to toggle source
# File lib/server_backups/config.rb, line 159
def mysqldump_bin
    File.join(bin_path, MYSQLDUMP)
end
notify_on_success() click to toggle source
# File lib/server_backups/config.rb, line 45
def notify_on_success
    @config.fetch('slack', nil)&.fetch('notify_on_success', false)
end
password() click to toggle source
# File lib/server_backups/config.rb, line 139
def password
    mysql['password']
end
prefix() click to toggle source
# File lib/server_backups/config.rb, line 191
def prefix
    s3['prefix']
end
region() click to toggle source
# File lib/server_backups/config.rb, line 187
def region
    s3['region']
end
retain_dailies_after() click to toggle source
# File lib/server_backups/config.rb, line 111
def retain_dailies_after
    get_retention_threshold(:daily)
end
retain_incrementals_after() click to toggle source
# File lib/server_backups/config.rb, line 115
def retain_incrementals_after
    get_retention_threshold(:incremental)
end
retain_monthlies_after() click to toggle source
# File lib/server_backups/config.rb, line 119
def retain_monthlies_after
    get_retention_threshold(:monthly)
end
retain_weeklies_after() click to toggle source
# File lib/server_backups/config.rb, line 123
def retain_weeklies_after
    get_retention_threshold(:weekly)
end
secret_access_key() click to toggle source
# File lib/server_backups/config.rb, line 179
def secret_access_key
    s3['secret_access_key']
end
slack_mention_on_failure() click to toggle source
# 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_success() click to toggle source
# File lib/server_backups/config.rb, line 53
def slack_mention_on_success
    @config.fetch('slack', nil)&.fetch('mention_users_on_success', [])
end
slack_webhook() click to toggle source
# File lib/server_backups/config.rb, line 41
def slack_webhook
    @config.fetch('slack', nil)&.fetch('webhook')
end
system_time_zone() click to toggle source
# 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_zone() click to toggle source
# File lib/server_backups/config.rb, line 66
def time_zone
    @config['time_zone'] || 'UTC'
end
user() click to toggle source
# File lib/server_backups/config.rb, line 135
def user
    mysql['user']
end
web_root() click to toggle source
# File lib/server_backups/config.rb, line 62
def web_root
    File.absolute_path(@config['file_location'] || FILE_LOCATION)
end

Private Instance Methods

default_config_file() click to toggle source
# File lib/server_backups/config.rb, line 205
def default_config_file
    File.join (ENV['HOME']).to_s, '.mys3ql'
end
mysql() click to toggle source
# File lib/server_backups/config.rb, line 197
def mysql
    @config['mysql']
end
s3() click to toggle source
# File lib/server_backups/config.rb, line 201
def s3
    @config['s3']
end