Skip to content

Commit

Permalink
add report for backup email
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Aug 30, 2024
1 parent 197e9d6 commit 672e8a6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
33 changes: 31 additions & 2 deletions app/jobs/backup_database_job.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# app/jobs/backup_database_job.rb
class BackupDatabaseJob < ApplicationJob
queue_as :default

Expand All @@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions app/mailers/backup_report_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# app/mailers/backup_report_mailer.rb
class BackupReportMailer < ApplicationMailer
default to: '[email protected]'

def backup_completed(report)
@report = report
mail(subject: 'Backup Completed Successfully')
end
end
27 changes: 27 additions & 0 deletions app/views/backup_report_mailer/backup_completed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<p>Backup Report for <strong>Linkarooie Project</strong></p>

<p>We have successfully completed a database backup for the Linkarooie project. Below are the details of the backup:</p>

<p><strong>Environment:</strong> <%= @report[:environment] %></p>
<p><strong>Timestamp:</strong> <%= @report[:timestamp] %></p>

<% if @report[:backup_url].present? %>
<p><strong>Backup File:</strong> <a href="<%= @report[:backup_url] %>">Download Backup</a></p>
<% else %>
<p><strong>Backup File:</strong> <%= @report[:backup_file] %></p>
<% end %>

<p><strong>Backup Size:</strong> <%= @report[:backup_size] %> bytes</p>
<p><strong>Status:</strong> <%= @report[:status] %></p>

<% if @report[:error_message].present? %>
<p><strong>Error Message:</strong> <%= @report[:error_message] %></p>
<% end %>

<p>Backup Frequency: <strong>Daily</strong></p>
<p>Backup Storage Location: <strong>DigitalOcean Spaces</strong></p>

<p>Please review the details above and take any necessary action if the backup status indicates a failure.</p>

<p>Thank you,</p>
<p>Your Backup System</p>

0 comments on commit 672e8a6

Please sign in to comment.