diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb index 6b501e0..fa26ee3 100644 --- a/app/controllers/admin/backups_controller.rb +++ b/app/controllers/admin/backups_controller.rb @@ -4,7 +4,14 @@ module Admin class BackupsController < ApplicationController # GET /admin/backups/1 def show - send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment + respond_to do |format| + format.xlsx do + send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment + end + format.zip do + send_file Backup.zip, filename: "backup.zip", disposition: :attachment + end + end end end end diff --git a/app/models/admin/backup.rb b/app/models/admin/backup.rb index f3b1c45..72649d4 100644 --- a/app/models/admin/backup.rb +++ b/app/models/admin/backup.rb @@ -4,29 +4,39 @@ module Admin class Backup class << self def zip - create_records_xlsx - + # create_records_xlsx + Dir.glob(Rails.root.join("storage", "*.sqlite3")).each do |entry| + `sqlite3 #{entry} .dump > #{entry.sub(".sqlite3", ".dump.sql")}` + `sqlite3 #{entry} .schema > #{entry.sub(".sqlite3", ".schema.sql")}` + end ZipFileGenerator.new(Rails.root.join("storage")).write end def db_xlsx xlsx = Axlsx::Package.new workbook = xlsx.workbook + ActiveRecord::Base.connection.tables.each do |table| - klass = table.classify.safe_constantize - next unless klass - workbook.add_worksheet(name: table) do |sheet| - attributes = klass.attribute_names - attributes += klass.rich_text_association_names if klass.respond_to?(:rich_text_association_names) + klass = table.classify.safe_constantize + if klass + attributes = klass.attribute_names + attributes += klass.rich_text_association_names.map(&:to_s) if klass.respond_to?(:rich_text_association_names) - sheet << attributes.map { _1.to_s.sub("rich_text_", "") } - klass.find_each do |record| - sheet << attributes.map { _1.starts_with?("rich_text") ? record.send(_1.to_s.sub("rich_text_", "")).body&.to_html : record.send(_1) } + attributes = ["id"] + attributes.reject{ _1 == "id"} + .sort { _1.sub("rich_text_", "") <=> _2.sub("rich_text_", "") } + + sheet << attributes.map { _1.sub("rich_text_", "") } + klass.find_each do |record| + sheet << attributes.map { _1.starts_with?("rich_text") ? record.send(_1.to_s.sub("rich_text_", "")).body&.to_html : record.send(_1) } + end + else + # TODO: Add "raw table data" end end end - path = Rails.root.join("storage/data.xls") + + path = file_path("data.xls") xlsx.serialize(path) path end diff --git a/app/views/backoffice/show.html.erb b/app/views/backoffice/show.html.erb index 936bdb1..a2e52d1 100644 --- a/app/views/backoffice/show.html.erb +++ b/app/views/backoffice/show.html.erb @@ -19,6 +19,12 @@ <%= link_to Link.model_name.human(count: Link.count), :links %>
-- <%= link_to "Backup herunterladen", admin_backup_url, class: "btn btn-secondary", data: { turbo_prefetch: false, frame: "_top", turbo: false } %> -
\ No newline at end of file +