2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
module BackupRestore
|
|
|
|
# @abstract
|
|
|
|
class BackupStore
|
2020-01-12 18:12:27 -05:00
|
|
|
BackupFileExists = Class.new(RuntimeError)
|
|
|
|
StorageError = Class.new(RuntimeError)
|
2018-10-14 21:43:31 -04:00
|
|
|
|
|
|
|
# @return [BackupStore]
|
|
|
|
def self.create(opts = {})
|
2021-02-09 10:02:44 -05:00
|
|
|
case opts[:location] || SiteSetting.backup_location
|
2018-10-14 21:43:31 -04:00
|
|
|
when BackupLocationSiteSetting::LOCAL
|
2022-03-21 10:28:52 -04:00
|
|
|
require "backup_restore/local_backup_store"
|
2018-10-14 21:43:31 -04:00
|
|
|
BackupRestore::LocalBackupStore.new(opts)
|
|
|
|
when BackupLocationSiteSetting::S3
|
2022-03-21 10:28:52 -04:00
|
|
|
require "backup_restore/s3_backup_store"
|
2018-10-14 21:43:31 -04:00
|
|
|
BackupRestore::S3BackupStore.new(opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Array<BackupFile>]
|
|
|
|
def files
|
2018-12-14 17:14:46 -05:00
|
|
|
@files ||= unsorted_files.sort_by { |file| -file.last_modified.to_i }
|
2018-10-14 21:43:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [BackupFile]
|
|
|
|
def latest_file
|
|
|
|
files.first
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:14:46 -05:00
|
|
|
def reset_cache
|
|
|
|
@files = nil
|
|
|
|
Report.clear_cache(:storage_stats)
|
|
|
|
end
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
def delete_old
|
|
|
|
return unless cleanup_allowed?
|
|
|
|
return if (backup_files = files).size <= SiteSetting.maximum_backups
|
|
|
|
|
|
|
|
backup_files[SiteSetting.maximum_backups..-1].each do |file|
|
|
|
|
delete_file(file.filename)
|
|
|
|
end
|
2018-12-14 17:14:46 -05:00
|
|
|
|
|
|
|
reset_cache
|
2018-10-14 21:43:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remote?
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [BackupFile]
|
|
|
|
def file(filename, include_download_source: false)
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_file(filename)
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def download_file(filename, destination, failure_message = nil)
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_file(filename, source_path, content_type)
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_upload_url(filename)
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
2018-12-14 17:14:46 -05:00
|
|
|
def stats
|
|
|
|
{
|
|
|
|
used_bytes: used_bytes,
|
|
|
|
free_bytes: free_bytes,
|
|
|
|
count: files.size,
|
|
|
|
last_backup_taken_at: latest_file&.last_modified
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-10-14 21:43:31 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
# @return [Array<BackupFile>]
|
|
|
|
def unsorted_files
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup_allowed?
|
|
|
|
true
|
|
|
|
end
|
2018-12-14 17:14:46 -05:00
|
|
|
|
|
|
|
def used_bytes
|
|
|
|
files.sum { |file| file.size }
|
|
|
|
end
|
|
|
|
|
|
|
|
def free_bytes
|
|
|
|
fail NotImplementedError
|
|
|
|
end
|
2018-10-14 21:43:31 -04:00
|
|
|
end
|
|
|
|
end
|