Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dl/backup email size #72

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions app/jobs/backup_database_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,60 @@ 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
backup_url = upload_to_spaces(compressed_file)


# Calculate backup file size before deleting it
backup_size = File.size?(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)
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 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
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

# 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

Expand All @@ -54,20 +70,6 @@ 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
6 changes: 1 addition & 5 deletions app/views/backup_report_mailer/backup_completed.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
<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 File:</strong> The backup is available in your account. Please log in to access and download the backup.</p>

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