Ru-b2-SQL-Backups/mysql_database_config.rb
connorc@orbitnode.net 496be85eaa Fix B2 bucket upload issue and store b2_enabled in config
This commit addresses two main issues:

    The 'b2_enabled' value was not being stored in the config JSON. Added it to the config JSON to enable or disable B2 bucket uploads as needed.
    Corrected the execution of b2-cli commands in the MysqlDatabaseBackup class using backticks for proper shell command execution in Ruby.

These changes should resolve the issue of the database dump file not being uploaded to the B2 bucket.
2023-03-17 10:23:31 -06:00

68 lines
1.7 KiB
Ruby

# frozen_string_literal: true
# class for generating the mysql config if it doesn't exist
require 'json'
# class for generating our config
class MysqlDatabaseConfig
def initialize(config_file)
@config_file = config_file
end
def generate
if File.exist?(@config_file)
puts 'Config file already exists, skipping generation.'
else
mysql_host = prompt('MySQL Host')
mysql_username = prompt('MySQL Username')
mysql_password = prompt('MySQL Password')
backup_dir = prompt('Backup Directory', default: '.')
b2_enabled = prompt_bool('Enable Backblaze B2?', default: false)
config['b2_enabled'] = b2_enabled
if b2_enabled
config['b2'] = {
'key_id' => @b2_key_id,
'application_key' => @b2_application_key,
'bucket_name' => @b2_bucket_name
}
end
config = {
'mysql' => {
'host' => mysql_host,
'username' => mysql_username,
'password' => mysql_password
},
'backup_dir' => backup_dir
}
if b2_enabled
config['b2'] = {
'key_id' => @b2_key_id,
'application_key' => @b2_application_key,
'bucket_name' => @b2_bucket_name
}
end
File.write(@config_file, JSON.pretty_generate(config))
puts "Config file generated: #{@config_file}"
end
end
private
def prompt(message, default: nil)
print message.to_s
print " [#{default}]" if default
print ': '
value = gets.chomp
value.empty? ? default : value
end
def prompt_bool(message, default: false)
prompt("#{message} (y/n)", default: default) =~ /y|yes/i
end
end