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.
This commit is contained in:
connorc@orbitnode.net 2023-03-18 11:59:58 -06:00
parent 2c960727d6
commit b746eedac6

View File

@ -57,11 +57,12 @@ class MysqlDatabaseBackup
existing_files = `b2 ls #{@b2_bucket_name}` existing_files = `b2 ls #{@b2_bucket_name}`
existing_files.each_line do |line| 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] file_id = line.match(/"fileId": "([^"]+)"/)[1]
`b2 delete-file-version #{@b2_bucket_name} #{b2_file_name} #{file_id}` `b2 delete-file-version #{@b2_bucket_name} #{file_name} #{file_id}`
puts "Deleted existing backup file from B2 bucket: #{b2_file_url}" puts "Deleted existing backup file from B2 bucket: #{file_name}"
end end
# Upload the backup file to the B2 bucket # Upload the backup file to the B2 bucket