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.
 
 
 
 
 
 

66 lines
1.5 KiB

class DbDumpController < ApplicationController
before_action :authenticate_admin # Optional: Schutz davor
def index
end
def dump
filename = "dump_#{Time.now.strftime('%Y%m%d_%H%M%S')}.sql"
filepath = Rails.root.join("tmp", filename)
db_config = ActiveRecord::Base.connection_db_config.configuration_hash
cmd = [
"pg_dump",
"-U", db_config[:username],
"-h", db_config[:host] || "localhost",
"-p", (db_config[:port] || 5432).to_s,
db_config[:database],
"-f", filepath.to_s
]
ENV["PGPASSWORD"] = db_config[:password].to_s
success = system(*cmd)
ENV["PGPASSWORD"] = nil
if File.exist?(filepath)
send_file filepath, type: "application/sql", filename: filename
else
render plain: "Dump fehlgeschlagen ❌", status: 500
end
end
def restore
uploaded = params[:dump_file]
return render plain: "Keine Datei", status: 400 unless uploaded
filepath = Rails.root.join("tmp", "upload_restore.sql")
File.binwrite(filepath, uploaded.read)
db_config = ActiveRecord::Base.connection_db_config.configuration_hash
cmd = [
"psql",
"-U", db_config[:username],
"-h", db_config[:host] || "localhost",
"-p", (db_config[:port] || 5432).to_s,
db_config[:database],
"-f", filepath.to_s
]
ENV["PGPASSWORD"] = db_config[:password].to_s
result = system(*cmd)
ENV["PGPASSWORD"] = nil
if result
render plain: "Restore erfolgreich ✅"
else
render plain: "Restore fehlgeschlagen ❌", status: 500
end
end
end