improve backup
This commit is contained in:
parent
db9755cad5
commit
fc71cd4a09
4 changed files with 45 additions and 17 deletions
|
|
@ -4,7 +4,14 @@ module Admin
|
||||||
class BackupsController < ApplicationController
|
class BackupsController < ApplicationController
|
||||||
# GET /admin/backups/1
|
# GET /admin/backups/1
|
||||||
def show
|
def show
|
||||||
|
respond_to do |format|
|
||||||
|
format.xlsx do
|
||||||
send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment
|
send_file Backup.db_xlsx, filename: "backup.xlsx", disposition: :attachment
|
||||||
end
|
end
|
||||||
|
format.zip do
|
||||||
|
send_file Backup.zip, filename: "backup.zip", disposition: :attachment
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,29 +4,39 @@ module Admin
|
||||||
class Backup
|
class Backup
|
||||||
class << self
|
class << self
|
||||||
def zip
|
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
|
ZipFileGenerator.new(Rails.root.join("storage")).write
|
||||||
end
|
end
|
||||||
|
|
||||||
def db_xlsx
|
def db_xlsx
|
||||||
xlsx = Axlsx::Package.new
|
xlsx = Axlsx::Package.new
|
||||||
workbook = xlsx.workbook
|
workbook = xlsx.workbook
|
||||||
|
|
||||||
ActiveRecord::Base.connection.tables.each do |table|
|
ActiveRecord::Base.connection.tables.each do |table|
|
||||||
klass = table.classify.safe_constantize
|
|
||||||
next unless klass
|
|
||||||
|
|
||||||
workbook.add_worksheet(name: table) do |sheet|
|
workbook.add_worksheet(name: table) do |sheet|
|
||||||
|
klass = table.classify.safe_constantize
|
||||||
|
if klass
|
||||||
attributes = klass.attribute_names
|
attributes = klass.attribute_names
|
||||||
attributes += klass.rich_text_association_names if klass.respond_to?(:rich_text_association_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_", "") }
|
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|
|
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) }
|
sheet << attributes.map { _1.starts_with?("rich_text") ? record.send(_1.to_s.sub("rich_text_", "")).body&.to_html : record.send(_1) }
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
# TODO: Add "raw table data"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
path = Rails.root.join("storage/data.xls")
|
end
|
||||||
|
|
||||||
|
path = file_path("data.xls")
|
||||||
xlsx.serialize(path)
|
xlsx.serialize(path)
|
||||||
path
|
path
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,12 @@
|
||||||
<%= link_to Link.model_name.human(count: Link.count), :links %>
|
<%= link_to Link.model_name.human(count: Link.count), :links %>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<h2 class="mt-3">Backup</h2>
|
||||||
<%= link_to "Backup herunterladen", admin_backup_url, class: "btn btn-secondary", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
<ul class="ps-0" style="list-style-type: none">
|
||||||
</p>
|
<li>
|
||||||
|
<%= link_to "XLSX Backup herunterladen", admin_backup_url(format: :xlsx), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<%= link_to "ZIP Backup herunterladen", admin_backup_url(format: :zip), class: " ", data: { turbo_prefetch: false, frame: "_top", turbo: false } %>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
@ -23,8 +23,13 @@ module Admin
|
||||||
login("test@example.com", "password")
|
login("test@example.com", "password")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "should show admin_backup" do
|
test "should create admin_backup xlsx" do
|
||||||
get admin_backup_url(@admin_backup)
|
get admin_backup_url(@admin_backup, format: :xlsx)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should create admin_backup zip" do
|
||||||
|
get admin_backup_url(@admin_backup, format: :zip)
|
||||||
assert_response :success
|
assert_response :success
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue