You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

31 lines
764 B

# app/services/dump_service.rb
class DumpService
def self.perform
timestamp = Time.current.strftime("%Y%m%d_%H%M%S")
filename = "dump_#{timestamp}.sql"
filepath = Rails.root.join("tmp", filename)
db_config = ActiveRecord::Base.connection_db_config.configuration_hash
ENV["PGPASSWORD"] = db_config[:password].to_s
cmd = [
"pg_dump",
"-U", db_config[:username].to_s,
"-h", (db_config[:host] || "localhost").to_s,
"-p", (db_config[:port] || 5432).to_s,
db_config[:database].to_s,
"-f", filepath.to_s
]
success = system(*cmd)
ENV["PGPASSWORD"] = nil
return nil unless success
zip_path = "#{filepath}.zip"
`zip -j #{zip_path} #{filepath}`
File.delete(filepath) if File.exist?(filepath)
zip_path
end
end