Skip to content

Commit

Permalink
fix job
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Sep 1, 2024
1 parent b7822ba commit 9596a57
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 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

# 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
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 @@ -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

0 comments on commit 9596a57

Please sign in to comment.