backups
This commit is contained in:
parent
55df5bbb40
commit
fd42a3f173
8 changed files with 123 additions and 4 deletions
89
app/models/admin/backup.rb
Normal file
89
app/models/admin/backup.rb
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
module Admin
|
||||
class Backup
|
||||
class << self
|
||||
def zip
|
||||
create_records_xlsx
|
||||
|
||||
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|
|
||||
sheet << klass.attribute_names
|
||||
klass.find_each do |record|
|
||||
sheet << record.attributes.values
|
||||
end
|
||||
end
|
||||
end
|
||||
path = Rails.root.join('storage/data.xls')
|
||||
xlsx.serialize(path)
|
||||
path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def file_path(name)
|
||||
Rails.root.join('storage', name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# This is a simple example which uses rubyzip to
|
||||
# recursively generate a zip file from the contents of
|
||||
# a specified directory. The directory itself is not
|
||||
# included in the archive, rather just its contents.
|
||||
#
|
||||
# Usage:
|
||||
# directory_to_zip = "/tmp/input"
|
||||
# output_file = "/tmp/out.zip"
|
||||
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
|
||||
# zf.write()
|
||||
class ZipFileGenerator
|
||||
# Initialize with the directory to zip and the location of the output archive.
|
||||
def initialize(input_dir)
|
||||
@input_dir = input_dir
|
||||
end
|
||||
|
||||
# Zip the input directory.
|
||||
def write
|
||||
entries = Dir.entries(@input_dir) - %w[. ..]
|
||||
path = Rails.root.join('tmp', Time.now.to_i.to_s)
|
||||
::Zip::File.open(path, create: true) do |zipfile|
|
||||
write_entries entries, '', zipfile
|
||||
end
|
||||
path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A helper method to make the recursion work.
|
||||
def write_entries(entries, path, zipfile)
|
||||
entries.each do |e|
|
||||
zipfile_path = path == '' ? e : File.join(path, e)
|
||||
disk_file_path = File.join(@input_dir, zipfile_path)
|
||||
|
||||
if File.directory? disk_file_path
|
||||
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
|
||||
else
|
||||
put_into_archive(disk_file_path, zipfile, zipfile_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
|
||||
zipfile.mkdir zipfile_path
|
||||
subdir = Dir.entries(disk_file_path) - %w[. ..]
|
||||
write_entries subdir, zipfile_path, zipfile
|
||||
end
|
||||
|
||||
def put_into_archive(disk_file_path, zipfile, zipfile_path)
|
||||
zipfile.add(zipfile_path, disk_file_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue