BigW Consortium Gitlab

uploads.rb 877 Bytes
Newer Older
1 2 3 4 5
module Backup
  class Uploads
    attr_reader :app_uploads_dir, :backup_uploads_dir, :backup_dir

    def initialize
6
      @app_uploads_dir = File.realpath(Rails.root.join('public', 'uploads'))
7 8 9 10 11 12 13 14 15 16 17
      @backup_dir = Gitlab.config.backup.path
      @backup_uploads_dir = File.join(Gitlab.config.backup.path, 'uploads')
    end

    # Copy uploads from public/uploads to backup/uploads
    def dump
      FileUtils.mkdir_p(backup_uploads_dir)
      FileUtils.cp_r(app_uploads_dir, backup_dir)
    end

    def restore
Ben Bodenmiller committed
18 19 20 21
      backup_existing_uploads_dir

      FileUtils.cp_r(backup_uploads_dir, app_uploads_dir)
    end
22

Ben Bodenmiller committed
23
    def backup_existing_uploads_dir
24
      timestamped_uploads_path = File.join(app_uploads_dir, '..', "uploads.#{Time.now.to_i}")
25
      if File.exists?(app_uploads_dir)
26
        FileUtils.mv(app_uploads_dir, timestamped_uploads_path)
27 28 29 30
      end
    end
  end
end