Skip to content

Commit

Permalink
support compression
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Aug 23, 2024
1 parent 71eb024 commit 6555f8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
16 changes: 11 additions & 5 deletions app/jobs/backup_database_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class BackupDatabaseJob < ApplicationJob

def perform
environment = Rails.env
backup_file = "db/backups/#{environment}_backup_#{Time.now.strftime('%Y%m%d%H%M%S')}.sqlite3"
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
Expand All @@ -13,15 +15,19 @@ def perform
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
upload_to_spaces(backup_file)
upload_to_spaces(compressed_file)

# Optionally, delete the local backup file after upload
# 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 and uploaded successfully: #{backup_file}"
Rails.logger.info "BackupDatabaseJob: Backup created, compressed, and uploaded successfully: #{compressed_file}"
rescue => e
Rails.logger.error "BackupDatabaseJob: Failed to create or upload backup: #{e.message}"
Rails.logger.error "BackupDatabaseJob: Failed to create, compress, or upload backup: #{e.message}"
raise
end
end
Expand Down
31 changes: 25 additions & 6 deletions lib/tasks/restore.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@ namespace :db do

unless backup_file
puts "ERROR: You must provide the path to the backup file."
puts "Usage: rake db:restore BACKUP_FILE=path/to/your_backup_file.sql"
puts "Usage: rake db:restore BACKUP_FILE=path/to/your_backup_file.sql[.tar.gz]"
exit 1
end

begin
puts "Restoring database from #{backup_file}..."
# Check if the file is compressed
if backup_file.end_with?('.tar.gz')
# Extract the .sqlite3 file from the .tar.gz archive
puts "Extracting #{backup_file}..."
extracted_file = `tar -xzvf #{backup_file} -C db/backups`.strip
extracted_file = "db/backups/#{extracted_file}"

# Drop the current database tables
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{ActiveRecord::Base.connection.tables.join(', ')}")
# Restore from the extracted .sqlite3 file
restore_from_file(extracted_file)

# Load the backup SQL file
system("sqlite3 #{Rails.configuration.database_configuration[Rails.env]['database']} < #{backup_file}")
# Optionally, delete the extracted file after restoration
File.delete(extracted_file) if File.exist?(extracted_file)
else
# Restore from the uncompressed .sqlite3 file
restore_from_file(backup_file)
end

puts "Database restored successfully."
rescue => e
puts "ERROR: Failed to restore the database: #{e.message}"
exit 1
end
end

def restore_from_file(backup_file)
puts "Restoring database from #{backup_file}..."

# Drop the current database tables
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{ActiveRecord::Base.connection.tables.join(', ')}")

# Load the backup SQL file
system("sqlite3 #{Rails.configuration.database_configuration[Rails.env]['database']} < #{backup_file}")
end
end

0 comments on commit 6555f8e

Please sign in to comment.