From b746eedac6d25a6dafaa87b14331509df97635e5 Mon Sep 17 00:00:00 2001 From: "connorc@orbitnode.net" Date: Sat, 18 Mar 2023 11:59:58 -0600 Subject: [PATCH] Add code to delete all non-matching files in a Backblaze B2 bucket before uploading a new backup file This commit modifies the upload_to_b2 method in the MysqlDatabaseBackup class to delete all files in the specified Backblaze B2 bucket that do not match the name of the backup file being uploaded. The implementation retrieves a list of all files in the bucket using the b2 ls command and then iterates over each line to extract the name of each file. If the name of a file does not match the name of the backup file, the implementation extracts its file ID using a regular expression and then uses the b2 delete-file-version command to delete the file from the bucket. Finally, the implementation uploads the backup file to the B2 bucket. --- mysql_database_backup.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mysql_database_backup.rb b/mysql_database_backup.rb index cb9d6c3..19ff693 100644 --- a/mysql_database_backup.rb +++ b/mysql_database_backup.rb @@ -57,11 +57,12 @@ class MysqlDatabaseBackup existing_files = `b2 ls #{@b2_bucket_name}` existing_files.each_line do |line| - next unless line.include?(b2_file_name) + file_name = line.split(" ").last.strip + next unless file_name != b2_file_name file_id = line.match(/"fileId": "([^"]+)"/)[1] - `b2 delete-file-version #{@b2_bucket_name} #{b2_file_name} #{file_id}` - puts "Deleted existing backup file from B2 bucket: #{b2_file_url}" + `b2 delete-file-version #{@b2_bucket_name} #{file_name} #{file_id}` + puts "Deleted existing backup file from B2 bucket: #{file_name}" end # Upload the backup file to the B2 bucket