2014-02-21 19:41:01 -05:00
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class BackupChunksMerger < Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
filename = args[:filename]
|
|
|
|
identifier = args[:identifier]
|
|
|
|
chunks = args[:chunks].to_i
|
|
|
|
|
2014-03-18 20:05:47 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
2014-02-21 19:41:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:identifier) if identifier.blank?
|
2014-03-18 20:05:47 -04:00
|
|
|
raise Discourse::InvalidParameters.new(:chunks) if chunks <= 0
|
2014-02-21 19:41:01 -05:00
|
|
|
|
2014-03-18 20:05:47 -04:00
|
|
|
backup_path = "#{Backup.base_directory}/#{filename}"
|
|
|
|
tmp_backup_path = "#{backup_path}.tmp"
|
2014-02-21 19:41:01 -05:00
|
|
|
|
2014-03-18 20:05:47 -04:00
|
|
|
# delete destination files
|
|
|
|
File.delete(backup_path) rescue nil
|
|
|
|
File.delete(tmp_backup_path) rescue nil
|
2014-02-21 19:41:01 -05:00
|
|
|
|
|
|
|
# merge all the chunks
|
2014-03-18 20:05:47 -04:00
|
|
|
File.open(tmp_backup_path, "a") do |backup|
|
2014-02-21 19:41:01 -05:00
|
|
|
(1..chunks).each do |chunk_number|
|
|
|
|
# path to chunk
|
2014-03-18 20:05:47 -04:00
|
|
|
chunk_path = Backup.chunk_path(identifier, filename, chunk_number)
|
2014-02-21 19:41:01 -05:00
|
|
|
# add chunk to backup
|
2014-03-18 20:05:47 -04:00
|
|
|
backup << File.open(chunk_path).read
|
2014-02-21 19:41:01 -05:00
|
|
|
end
|
|
|
|
end
|
2014-03-18 20:05:47 -04:00
|
|
|
|
|
|
|
# rename tmp backup to final backup name
|
|
|
|
FileUtils.mv(tmp_backup_path, backup_path, force: true)
|
2014-02-21 19:41:01 -05:00
|
|
|
|
|
|
|
# remove tmp directory
|
2014-03-18 20:05:47 -04:00
|
|
|
tmp_directory = File.dirname(Backup.chunk_path(identifier, filename, 0))
|
|
|
|
FileUtils.rm_rf(tmp_directory) rescue nil
|
2014-02-21 19:41:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|