2014-02-12 23:31:17 -05:00
|
|
|
class Backup
|
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
attr_reader :filename
|
|
|
|
attr_accessor :size, :path, :link
|
2014-02-12 23:31:17 -05:00
|
|
|
|
|
|
|
def initialize(filename)
|
|
|
|
@filename = filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
2015-04-07 09:26:47 -04:00
|
|
|
Dir.glob(File.join(Backup.base_directory, "*.{gz,tgz}"))
|
2017-07-27 21:20:09 -04:00
|
|
|
.sort_by { |file| File.mtime(file) }
|
|
|
|
.reverse
|
|
|
|
.map { |backup| Backup.create_from_filename(File.basename(backup)) }
|
2014-02-12 23:31:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](filename)
|
|
|
|
path = File.join(Backup.base_directory, filename)
|
|
|
|
if File.exists?(path)
|
2014-03-11 17:28:12 -04:00
|
|
|
Backup.create_from_filename(filename)
|
2014-02-12 23:31:17 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
def remove
|
|
|
|
File.delete(@path) if File.exists?(path)
|
2014-03-12 16:23:47 -04:00
|
|
|
after_remove_hook
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_create_hook
|
2017-08-17 10:24:56 -04:00
|
|
|
upload_to_s3 if SiteSetting.enable_s3_backups?
|
|
|
|
DiscourseEvent.trigger(:backup_created)
|
2014-03-12 16:23:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_remove_hook
|
2016-03-01 14:01:51 -05:00
|
|
|
remove_from_s3 if SiteSetting.enable_s3_backups? && !SiteSetting.s3_disable_cleanup?
|
2014-03-12 16:23:47 -04:00
|
|
|
end
|
|
|
|
|
2014-09-24 16:52:09 -04:00
|
|
|
def s3_bucket
|
|
|
|
return @s3_bucket if @s3_bucket
|
|
|
|
raise Discourse::SiteSettingMissing.new("s3_backup_bucket") if SiteSetting.s3_backup_bucket.blank?
|
|
|
|
@s3_bucket = SiteSetting.s3_backup_bucket.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def s3
|
2015-02-10 18:55:04 -05:00
|
|
|
require "s3_helper" unless defined? S3Helper
|
2017-10-06 01:20:01 -04:00
|
|
|
@s3_helper ||= S3Helper.new(s3_bucket, '', S3Helper.s3_options(SiteSetting))
|
2014-09-24 16:52:09 -04:00
|
|
|
end
|
|
|
|
|
2014-03-12 16:23:47 -04:00
|
|
|
def upload_to_s3
|
2014-09-24 16:52:09 -04:00
|
|
|
return unless s3
|
2014-09-25 12:28:51 -04:00
|
|
|
File.open(@path) do |file|
|
|
|
|
s3.upload(file, @filename)
|
|
|
|
end
|
2014-03-12 16:23:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_from_s3
|
2014-09-24 16:52:09 -04:00
|
|
|
return unless s3
|
|
|
|
s3.remove(@filename)
|
2014-02-12 23:31:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
2017-01-16 13:53:31 -05:00
|
|
|
base_directory = File.join(Rails.root, "public", "backups", RailsMultisite::ConnectionManagement.current_db)
|
|
|
|
FileUtils.mkdir_p(base_directory) unless Dir.exists?(base_directory)
|
|
|
|
base_directory
|
2014-02-12 23:31:17 -05:00
|
|
|
end
|
|
|
|
|
2014-02-21 19:41:01 -05:00
|
|
|
def self.chunk_path(identifier, filename, chunk_number)
|
|
|
|
File.join(Backup.base_directory, "tmp", identifier, "#{filename}.part#{chunk_number}")
|
|
|
|
end
|
|
|
|
|
2014-03-11 17:28:12 -04:00
|
|
|
def self.create_from_filename(filename)
|
|
|
|
Backup.new(filename).tap do |b|
|
|
|
|
b.path = File.join(Backup.base_directory, b.filename)
|
2015-06-12 06:02:36 -04:00
|
|
|
b.link = UrlHelper.schemaless "#{Discourse.base_url}/admin/backups/#{b.filename}"
|
2014-03-11 17:28:12 -04:00
|
|
|
b.size = File.size(b.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove_old
|
2014-08-20 12:58:58 -04:00
|
|
|
return if Rails.env.development?
|
2014-03-11 17:28:12 -04:00
|
|
|
all_backups = Backup.all
|
2014-09-24 16:52:09 -04:00
|
|
|
return if all_backups.size <= SiteSetting.maximum_backups
|
2014-07-08 14:46:08 -04:00
|
|
|
all_backups[SiteSetting.maximum_backups..-1].each(&:remove)
|
2014-03-11 17:28:12 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:31:17 -05:00
|
|
|
end
|