diff --git a/app/jobs/backup_database_job.rb b/app/jobs/backup_database_job.rb index b0b69bc..49fe181 100644 --- a/app/jobs/backup_database_job.rb +++ b/app/jobs/backup_database_job.rb @@ -1,3 +1,4 @@ +# app/jobs/backup_database_job.rb class BackupDatabaseJob < ApplicationJob queue_as :default @@ -18,16 +19,29 @@ def perform # Compress the backup file `tar -czf #{compressed_file} -C db/backups #{File.basename(backup_file)}` - # Upload to DigitalOcean Spaces - upload_to_spaces(compressed_file) + # Upload to DigitalOcean Spaces and get the public 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) + + # Send an email with the report + BackupReportMailer.backup_completed(report).deliver_now rescue => e Rails.logger.error "BackupDatabaseJob: Failed to create, compress, or upload backup: #{e.message}" + + # 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 @@ -40,5 +54,20 @@ 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 + + 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 + } end end diff --git a/app/mailers/backup_report_mailer.rb b/app/mailers/backup_report_mailer.rb new file mode 100644 index 0000000..08d30d5 --- /dev/null +++ b/app/mailers/backup_report_mailer.rb @@ -0,0 +1,9 @@ +# app/mailers/backup_report_mailer.rb +class BackupReportMailer < ApplicationMailer + default to: 'dean+linkarooie@deanlofts.xyz' + + def backup_completed(report) + @report = report + mail(subject: 'Backup Completed Successfully') + end +end diff --git a/app/views/backup_report_mailer/backup_completed.html.erb b/app/views/backup_report_mailer/backup_completed.html.erb new file mode 100644 index 0000000..c0f9fb7 --- /dev/null +++ b/app/views/backup_report_mailer/backup_completed.html.erb @@ -0,0 +1,27 @@ +
Backup Report for Linkarooie Project
+ +We have successfully completed a database backup for the Linkarooie project. Below are the details of the backup:
+ +Environment: <%= @report[:environment] %>
+Timestamp: <%= @report[:timestamp] %>
+ +<% if @report[:backup_url].present? %> +Backup File: Download Backup
+<% else %> +Backup File: <%= @report[:backup_file] %>
+<% end %> + +Backup Size: <%= @report[:backup_size] %> bytes
+Status: <%= @report[:status] %>
+ +<% if @report[:error_message].present? %> +Error Message: <%= @report[:error_message] %>
+<% end %> + +Backup Frequency: Daily
+Backup Storage Location: DigitalOcean Spaces
+ +Please review the details above and take any necessary action if the backup status indicates a failure.
+ +Thank you,
+Your Backup System