From 9596a576433812ef1699ab0a27ea935b14ffecd2 Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sun, 1 Sep 2024 13:59:54 +1000 Subject: [PATCH 1/2] 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 From 9c51e8767da1005ed398a547f138b212460882a8 Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sun, 1 Sep 2024 14:02:44 +1000 Subject: [PATCH 2/2] make gooder --- app/jobs/backup_database_job.rb | 14 +++++--------- .../backup_report_mailer/backup_completed.html.erb | 6 +----- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/jobs/backup_database_job.rb b/app/jobs/backup_database_job.rb index e7f860c..eec630c 100644 --- a/app/jobs/backup_database_job.rb +++ b/app/jobs/backup_database_job.rb @@ -22,8 +22,8 @@ def perform # 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) + # Upload to DigitalOcean Spaces (URL not needed anymore) + upload_to_spaces(compressed_file) # Optionally, delete the local backup files after upload File.delete(backup_file) if File.exist?(backup_file) @@ -31,8 +31,8 @@ def perform Rails.logger.info "BackupDatabaseJob: Backup created, compressed, and uploaded successfully: #{compressed_file}" - # Generate a report with the backup size - report = generate_backup_report(environment, compressed_file, "Success", backup_url, nil, backup_size) + # Generate a report without the URL + report = generate_backup_report(environment, compressed_file, "Success", nil, nil, backup_size) # Send an email with the report BackupReportMailer.backup_completed(report).deliver_now @@ -70,10 +70,6 @@ def upload_to_spaces(file_path) obj = S3_CLIENT.bucket(bucket_name).object("backups/#{file_name}") obj.upload_file(file_path) - - # Generate a signed URL that expires in 24 hours - signed_url = obj.presigned_url(:get, expires_in: 24 * 60 * 60) - - signed_url + end end diff --git a/app/views/backup_report_mailer/backup_completed.html.erb b/app/views/backup_report_mailer/backup_completed.html.erb index c0f9fb7..83d37cb 100644 --- a/app/views/backup_report_mailer/backup_completed.html.erb +++ b/app/views/backup_report_mailer/backup_completed.html.erb @@ -5,11 +5,7 @@

Environment: <%= @report[:environment] %>

Timestamp: <%= @report[:timestamp] %>

-<% if @report[:backup_url].present? %> -

Backup File: Download Backup

-<% else %> -

Backup File: <%= @report[:backup_file] %>

-<% end %> +

Backup File: The backup is available in your account. Please log in to access and download the backup.

Backup Size: <%= @report[:backup_size] %> bytes

Status: <%= @report[:status] %>