11 changed files with 132 additions and 6 deletions
@ -1,9 +1,14 @@ |
|||
class ApplicationController < ActionController::Base |
|||
before_action :configure_permitted_parameters, if: :devise_controller? |
|||
|
|||
|
|||
def authenticate_admin |
|||
redirect_to root_path, alert: "Kein Zugriff!" unless current_user.email =="christoph@marzell.net" |
|||
end |
|||
def configure_permitted_parameters |
|||
devise_parameter_sanitizer.permit(:account_update, keys: [:total_required_hours, :weekly_target_hours, weekly_target_matrix: {}, required_hours_matrix: {}]) |
|||
end |
|||
def is_admin? |
|||
current_user&.email =="christoph@marzell.net" |
|||
end |
|||
|
|||
end |
|||
@ -0,0 +1,66 @@ |
|||
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 |
|||
@ -0,0 +1,2 @@ |
|||
module DbDumpHelper |
|||
end |
|||
@ -0,0 +1,30 @@ |
|||
<!-- app/views/db_dump/index.html.erb --> |
|||
<div class="container mt-5"> |
|||
<h2 class="mb-4">📦 Datenbank-Backup & Wiederherstellung</h2> |
|||
|
|||
<!-- Dump erstellen --> |
|||
<div class="card mb-4"> |
|||
<div class="card-body"> |
|||
<h5 class="card-title">🧾 Dump erstellen</h5> |
|||
<p class="card-text">Hier kannst du einen aktuellen Dump der Datenbank herunterladen.</p> |
|||
<a href="/db_dump/dump" class="btn btn-primary"> |
|||
Dump jetzt herunterladen |
|||
</a> |
|||
</div> |
|||
</div> |
|||
|
|||
<!-- Dump wiederherstellen --> |
|||
<div class="card"> |
|||
<div class="card-body"> |
|||
<h5 class="card-title">📤 Dump wiederherstellen</h5> |
|||
<p class="card-text">Wähle eine SQL-Dump-Datei, um die Datenbank wiederherzustellen.</p> |
|||
|
|||
<%= form_with url: "/db_dump/restore", method: :post, local: true, html: { multipart: true, class: "needs-validation" } do |form| %> |
|||
<div class="mb-3"> |
|||
<%= form.file_field :dump_file, class: "form-control", required: true %> |
|||
</div> |
|||
<%= form.submit "Dump einspielen", class: "btn btn-danger" %> |
|||
<% end %> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
Loading…
Reference in new issue