Add code to find and delete an existing backup file in a Backblaze B2 bucket before uploading a new backup file
This commit modifies the upload_to_b2 method in the MysqlDatabaseBackup class to search for an existing backup file with the same name as the backup file being uploaded in a specified Backblaze B2 bucket. The implementation retrieves a list of all files in the bucket using the b2 ls command and then iterates over each line to search for a file with the same name as the backup file. If a matching file is found, 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. Note that this implementation assumes that the backup file has a unique name.
This commit is contained in:
parent
0bd8a1b932
commit
2c960727d6
|
@ -55,10 +55,11 @@ 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 list-file-names #{@b2_bucket_name} --prefix #{b2_file_name}`
|
existing_files = `b2 ls #{@b2_bucket_name}`
|
||||||
if existing_file.include?(b2_file_name)
|
existing_files.each_line do |line|
|
||||||
# Delete the existing backup file from the B2 bucket
|
next unless line.include?(b2_file_name)
|
||||||
file_id = existing_file.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} #{b2_file_name} #{file_id}`
|
||||||
puts "Deleted existing backup file from B2 bucket: #{b2_file_url}"
|
puts "Deleted existing backup file from B2 bucket: #{b2_file_url}"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user