Class: ServerBackups::Config
- Inherits:
-
Object
- Object
- ServerBackups::Config
- 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
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#logging ⇒ Object
readonly
Returns the value of attribute logging.
Class Method Summary collapse
Instance Method Summary collapse
-
#access_key_id ⇒ Object
S3.
- #bin_log ⇒ Object
- #bin_path ⇒ Object
- #bucket ⇒ Object
- #database ⇒ Object
-
#db_host ⇒ Object
MySQL.
- #get_retention_threshold(backup_type) ⇒ Object
-
#initialize(config_file = nil) ⇒ Config
constructor
A new instance of Config.
- #log_device ⇒ Object
- #logfile_count ⇒ Object
- #logfile_size ⇒ Object
- #mysql_bin ⇒ Object
- #mysqlbinlog_bin ⇒ Object
- #mysqldump_bin ⇒ Object
- #notify_on_success ⇒ Object
- #password ⇒ Object
- #prefix ⇒ Object
- #region ⇒ Object
- #retain_dailies_after ⇒ Object
- #retain_incrementals_after ⇒ Object
- #retain_monthlies_after ⇒ Object
- #retain_weeklies_after ⇒ Object
- #secret_access_key ⇒ Object
- #slack_mention_on_failure ⇒ Object
- #slack_mention_on_success ⇒ Object
- #slack_webhook ⇒ Object
- #system_time_zone ⇒ Object
- #time_zone ⇒ Object
- #user ⇒ Object
- #web_root ⇒ Object
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.(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_file ⇒ Object (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 |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/server_backups/config.rb', line 10 def logger @logger end |
#logging ⇒ Object (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_id ⇒ Object
S3
175 176 177 |
# File 'lib/server_backups/config.rb', line 175 def access_key_id s3['access_key_id'] end |
#bin_log ⇒ Object
151 152 153 |
# File 'lib/server_backups/config.rb', line 151 def bin_log mysql['bin_log'] end |
#bin_path ⇒ Object
147 148 149 |
# File 'lib/server_backups/config.rb', line 147 def bin_path mysql['bin_path'] end |
#bucket ⇒ Object
183 184 185 |
# File 'lib/server_backups/config.rb', line 183 def bucket s3['bucket'] end |
#database ⇒ Object
143 144 145 |
# File 'lib/server_backups/config.rb', line 143 def database mysql['database'] end |
#db_host ⇒ Object
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_device ⇒ Object
83 84 85 |
# File 'lib/server_backups/config.rb', line 83 def log_device logging['use_stdout'] == true ? STDOUT : logging['logfile_path'] end |
#logfile_count ⇒ Object
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_size ⇒ Object
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_bin ⇒ Object
167 168 169 |
# File 'lib/server_backups/config.rb', line 167 def mysql_bin File.join(bin_path, MYSQL) end |
#mysqlbinlog_bin ⇒ Object
163 164 165 |
# File 'lib/server_backups/config.rb', line 163 def mysqlbinlog_bin File.join(bin_path, MYSQLBINLOG) end |
#mysqldump_bin ⇒ Object
159 160 161 |
# File 'lib/server_backups/config.rb', line 159 def mysqldump_bin File.join(bin_path, MYSQLDUMP) end |
#notify_on_success ⇒ Object
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 |
#password ⇒ Object
139 140 141 |
# File 'lib/server_backups/config.rb', line 139 def password mysql['password'] end |
#prefix ⇒ Object
191 192 193 |
# File 'lib/server_backups/config.rb', line 191 def prefix s3['prefix'] end |
#region ⇒ Object
187 188 189 |
# File 'lib/server_backups/config.rb', line 187 def region s3['region'] end |
#retain_dailies_after ⇒ Object
111 112 113 |
# File 'lib/server_backups/config.rb', line 111 def retain_dailies_after get_retention_threshold(:daily) end |
#retain_incrementals_after ⇒ Object
115 116 117 |
# File 'lib/server_backups/config.rb', line 115 def retain_incrementals_after get_retention_threshold(:incremental) end |
#retain_monthlies_after ⇒ Object
119 120 121 |
# File 'lib/server_backups/config.rb', line 119 def retain_monthlies_after get_retention_threshold(:monthly) end |
#retain_weeklies_after ⇒ Object
123 124 125 |
# File 'lib/server_backups/config.rb', line 123 def retain_weeklies_after get_retention_threshold(:weekly) end |
#secret_access_key ⇒ Object
179 180 181 |
# File 'lib/server_backups/config.rb', line 179 def secret_access_key s3['secret_access_key'] end |
#slack_mention_on_failure ⇒ Object
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_success ⇒ Object
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_webhook ⇒ Object
41 42 43 |
# File 'lib/server_backups/config.rb', line 41 def slack_webhook @config.fetch('slack', nil)&.fetch('webhook') end |
#system_time_zone ⇒ Object
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_zone ⇒ Object
66 67 68 |
# File 'lib/server_backups/config.rb', line 66 def time_zone @config['time_zone'] || 'UTC' end |
#user ⇒ Object
135 136 137 |
# File 'lib/server_backups/config.rb', line 135 def user mysql['user'] end |
#web_root ⇒ Object
62 63 64 |
# File 'lib/server_backups/config.rb', line 62 def web_root File.absolute_path(@config['file_location'] || FILE_LOCATION) end |