3342da13c6
customizable retention and logging Refactor backup method to dump each database individually Update delete_old_backups method to remove local backups based on user-defined retention days Add Loggman class for logging program actions to a logfile and implement log deletion for logs older than a week Modify MysqlDatabaseConfig class to ask the user for local and B2 backup retention days Update MysqlDatabaseBackup class to use user-defined retention days for local backups Refactor upload_to_b2 method to delete B2 backups based on user-defined retention days Implement various code improvements and refactoring for better readability and maintainability
45 lines
814 B
Ruby
45 lines
814 B
Ruby
# frozen_string_literal: true
|
|
|
|
require 'time'
|
|
require 'fileutils'
|
|
|
|
# Loggman class
|
|
class Loggman
|
|
def initialize(logfile)
|
|
@logfile = logfile
|
|
end
|
|
|
|
def log(message, level = :info)
|
|
delete_old_logs
|
|
timestamp = Time.now.strftime('%Y-%m-%d %H:%M:%S')
|
|
File.open(@logfile, 'a') do |file|
|
|
file.puts "[#{timestamp}] [#{level.to_s.upcase}] #{message}"
|
|
end
|
|
end
|
|
|
|
def info(message)
|
|
log(message, :info)
|
|
end
|
|
|
|
def warn(message)
|
|
log(message, :warn)
|
|
end
|
|
|
|
def error(message)
|
|
log(message, :error)
|
|
end
|
|
|
|
def debug(message)
|
|
log(message, :debug)
|
|
end
|
|
|
|
def delete_old_logs
|
|
max_age_days = 7
|
|
max_age_seconds = max_age_days * 24 * 60 * 60
|
|
|
|
if File.exist?(@logfile) && Time.now - File.mtime(@logfile) > max_age_seconds
|
|
FileUtils.rm(@logfile)
|
|
end
|
|
end
|
|
end
|