# 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