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.
This commit is contained in:
connorc@orbitnode.net 2023-03-17 10:23:31 -06:00
parent e73eef1064
commit 496be85eaa
2 changed files with 14 additions and 12 deletions

View File

@ -27,7 +27,6 @@ class MysqlDatabaseBackup
return unless @b2_enabled return unless @b2_enabled
upload_to_b2(backup_file) upload_to_b2(backup_file)
end end
def delete_old_backups def delete_old_backups
@ -52,16 +51,16 @@ class MysqlDatabaseBackup
b2_file_url = "b2://#{@b2_bucket_name}/#{b2_file_name}" b2_file_url = "b2://#{@b2_bucket_name}/#{b2_file_name}"
# Check if a backup file with the same name already exists in the B2 bucket # Check if a backup file with the same name already exists in the B2 bucket
existing_file = b2-cli list-file-names #{@b2_bucket_name} --prefix #{b2_file_name} existing_file = `b2-cli list-file-names #{@b2_bucket_name} --prefix #{b2_file_name}`
if existing_file.include?(b2_file_name) if existing_file.include?(b2_file_name)
# Delete the existing backup file from the B2 bucket # Delete the existing backup file from the B2 bucket
file_version = existing_file.match(/"fileId": "([^"]+)"/)[1] file_version = existing_file.match(/"fileId": "([^"]+)"/)[1]
b2-cli delete-file-version #{@b2_bucket_name} #{b2_file_name} #{file_version} `b2-cli delete-file-version #{@b2_bucket_name} #{b2_file_name} #{file_version}`
puts "Deleted existing backup file from B2 bucket: #{b2_file_url}" puts "Deleted existing backup file from B2 bucket: #{b2_file_url}"
end end
Upload the backup file to the B2 bucket # Upload the backup file to the B2 bucket
b2-cli upload-file #{@b2_bucket_name} #{backup_file} #{b2_file_name} `b2-cli upload-file #{@b2_bucket_name} #{backup_file} #{b2_file_name}`
puts "Uploaded backup file to B2 bucket: #{b2_file_url}" puts "Uploaded backup file to B2 bucket: #{b2_file_url}"
end end
end end

View File

@ -20,10 +20,13 @@ class MysqlDatabaseConfig
backup_dir = prompt('Backup Directory', default: '.') backup_dir = prompt('Backup Directory', default: '.')
b2_enabled = prompt_bool('Enable Backblaze B2?', default: false) b2_enabled = prompt_bool('Enable Backblaze B2?', default: false)
config['b2_enabled'] = b2_enabled
if b2_enabled if b2_enabled
@b2_key_id = prompt('Backblaze B2 Key ID') config['b2'] = {
@b2_application_key = prompt('Backblaze B2 Application Key') 'key_id' => @b2_key_id,
@b2_bucket_name = prompt('Backblaze B2 Bucket Name') 'application_key' => @b2_application_key,
'bucket_name' => @b2_bucket_name
}
end end
config = { config = {