From 9596a576433812ef1699ab0a27ea935b14ffecd2 Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sun, 1 Sep 2024 13:59:54 +1000 Subject: [PATCH] fix job --- app/jobs/backup_database_job.rb | 56 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/app/jobs/backup_database_job.rb b/app/jobs/backup_database_job.rb index 49fe181..e7f860c 100644 --- a/app/jobs/backup_database_job.rb +++ b/app/jobs/backup_database_job.rb @@ -7,30 +7,33 @@ def perform timestamp = Time.now.strftime('%Y%m%d%H%M%S') backup_file = "db/backups/#{environment}_backup_#{timestamp}.sqlite3" compressed_file = "db/backups/#{environment}_backup_#{timestamp}.tar.gz" - + begin # Ensure the backup directory exists FileUtils.mkdir_p("db/backups") - + # Dump the SQLite database for the current environment database_path = Rails.configuration.database_configuration[environment]["database"] `sqlite3 #{database_path} .dump > #{backup_file}` - + # Compress the backup file `tar -czf #{compressed_file} -C db/backups #{File.basename(backup_file)}` - - # Upload to DigitalOcean Spaces and get the public URL + + # Calculate backup file size before deleting it + backup_size = File.size?(compressed_file) + + # Upload to DigitalOcean Spaces and get the signed URL backup_url = upload_to_spaces(compressed_file) - + # Optionally, delete the local backup files after upload File.delete(backup_file) if File.exist?(backup_file) File.delete(compressed_file) if File.exist?(compressed_file) - + Rails.logger.info "BackupDatabaseJob: Backup created, compressed, and uploaded successfully: #{compressed_file}" - - # Generate a report - report = generate_backup_report(environment, compressed_file, "Success", backup_url) - + + # Generate a report with the backup size + report = generate_backup_report(environment, compressed_file, "Success", backup_url, nil, backup_size) + # Send an email with the report BackupReportMailer.backup_completed(report).deliver_now rescue => e @@ -38,13 +41,26 @@ def perform # Generate a failure report without URL report = generate_backup_report(environment, compressed_file, "Failed", nil, e.message) - + # Send an email with the failure report BackupReportMailer.backup_completed(report).deliver_now raise end end + + # Updated generate_backup_report to accept backup_size as a parameter + def generate_backup_report(environment, backup_file, status, backup_url = nil, error_message = nil, backup_size = nil) + { + environment: environment, + timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S'), + backup_file: backup_file, + backup_url: backup_url, + backup_size: backup_size || File.size?(backup_file), # Fallback if size wasn't passed + status: status, + error_message: error_message + } + end private @@ -55,19 +71,9 @@ def upload_to_spaces(file_path) obj = S3_CLIENT.bucket(bucket_name).object("backups/#{file_name}") obj.upload_file(file_path) - # Return the public URL or generate a signed URL if necessary - obj.public_url - end + # Generate a signed URL that expires in 24 hours + signed_url = obj.presigned_url(:get, expires_in: 24 * 60 * 60) - def generate_backup_report(environment, backup_file, status, backup_url = nil, error_message = nil) - { - environment: environment, - timestamp: Time.now.strftime('%Y-%m-%d %H:%M:%S'), - backup_file: backup_file, - backup_url: backup_url, - backup_size: File.size?(backup_file), - status: status, - error_message: error_message - } + signed_url end end