Class: Admin::BackupsController

Inherits:
AdminController
  • Object
show all
Includes:
ExternalUploadHelpers
Defined in:
app/controllers/admin/backups_controller.rb

Instance Method Summary collapse

Methods included from ExternalUploadHelpers

#abort_multipart, #batch_presign_multipart_parts, #complete_external_upload, #complete_multipart, #create_multipart, #generate_presigned_put, #multipart_upload_exists?

Instance Method Details

#cancelObject



58
59
60
61
62
63
64
# File 'app/controllers/admin/backups_controller.rb', line 58

def cancel
  BackupRestore.cancel!
rescue BackupRestore::OperationRunningError
  render_error("backup.operation_already_running")
else
  render json: success_json
end

#check_backup_chunkObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/admin/backups_controller.rb', line 159

def check_backup_chunk
  identifier = params.fetch(:resumableIdentifier)
  filename = params.fetch(:resumableFilename)
  chunk_number = params.fetch(:resumableChunkNumber)
  current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i

  raise Discourse::InvalidParameters.new(:resumableIdentifier) unless valid_filename?(identifier)

  # path to chunk file
  chunk = BackupRestore::LocalBackupStore.chunk_path(identifier, filename, chunk_number)
  # check chunk upload status
  status = HandleChunkUpload.check_chunk(chunk, current_chunk_size: current_chunk_size)

  render body: nil, status: status
end

#createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/admin/backups_controller.rb', line 36

def create
  RateLimiter.new(
    current_user,
    "max-backups-per-minute",
    1,
    1.minute,
    apply_limit_to_staff: true,
  ).performed!

  opts = {
    publish_to_message_bus: true,
    with_uploads: params.fetch(:with_uploads) == "true",
    client_id: params[:client_id],
  }
  BackupRestore.backup!(current_user.id, opts)
rescue BackupRestore::OperationRunningError
  render_error("backup.operation_already_running")
else
  StaffActionLogger.new(current_user).log_backup_create
  render json: success_json
end

#create_upload_urlObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/controllers/admin/backups_controller.rb', line 216

def create_upload_url
  params.require(:filename)
  filename = params.fetch(:filename)

  unless valid_extension?(filename)
    return render_json_error(I18n.t("backup.backup_file_should_be_tar_gz"))
  end
  return render_json_error(I18n.t("backup.invalid_filename")) unless valid_filename?(filename)

  store = BackupRestore::BackupStore.create

  begin
    upload_url = store.generate_upload_url(filename)
  rescue BackupRestore::BackupStore::BackupFileExists
    return render_json_error(I18n.t("backup.file_exists"))
  rescue BackupRestore::BackupStore::StorageError => e
    return render_json_error(e)
  end

  render json: success_json.merge(url: upload_url)
end

#destroyObject



105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/admin/backups_controller.rb', line 105

def destroy
  store = BackupRestore::BackupStore.create

  if backup = store.file(params.fetch(:id))
    StaffActionLogger.new(current_user).log_backup_destroy(backup)
    store.delete_file(backup.filename)
    render body: nil
  else
    render body: nil, status: 404
  end
end

#emailObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/admin/backups_controller.rb', line 66

def email
  store = BackupRestore::BackupStore.create

  if store.file(params.fetch(:id)).present?
    Jobs.enqueue(
      :download_backup_email,
      user_id: current_user.id,
      backup_file_path: url_for(controller: "backups", action: "show"),
    )

    render body: nil
  else
    render body: nil, status: 404
  end
end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/admin/backups_controller.rb', line 12

def index
  respond_to do |format|
    format.html do
      store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
      store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
      render "default/empty"
    end

    format.json do
      store = BackupRestore::BackupStore.create

      begin
        render_serialized(store.files, BackupFileSerializer)
      rescue BackupRestore::BackupStore::StorageError => e
        render_json_error(e)
      end
    end
  end
end

#logsObject



117
118
119
120
121
# File 'app/controllers/admin/backups_controller.rb', line 117

def logs
  store_preloaded("operations_status", MultiJson.dump(BackupRestore.operations_status))
  store_preloaded("logs", MultiJson.dump(BackupRestore.logs))
  render "default/empty"
end

#readonlyObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/admin/backups_controller.rb', line 144

def readonly
  enable = params.fetch(:enable).to_s == "true"
  readonly_mode_key = Discourse::USER_READONLY_MODE_KEY

  if enable
    Discourse.enable_readonly_mode(readonly_mode_key)
  else
    Discourse.disable_readonly_mode(readonly_mode_key)
  end

  StaffActionLogger.new(current_user).log_change_readonly_mode(enable)

  render body: nil
end

#restoreObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/admin/backups_controller.rb', line 123

def restore
  opts = {
    filename: params.fetch(:id),
    client_id: params.fetch(:client_id),
    publish_to_message_bus: true,
  }
  BackupRestore.restore!(current_user.id, opts)
rescue BackupRestore::OperationRunningError
  render_error("backup.operation_already_running")
else
  render json: success_json
end

#rollbackObject



136
137
138
139
140
141
142
# File 'app/controllers/admin/backups_controller.rb', line 136

def rollback
  BackupRestore.rollback!
rescue BackupRestore::OperationRunningError
  render_error("backup.operation_already_running")
else
  render json: success_json
end

#showObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/admin/backups_controller.rb', line 82

def show
  if !EmailBackupToken.compare(current_user.id, params.fetch(:token))
    @error = I18n.t("download_backup_mailer.no_token")
    return render layout: "no_ember", status: 422, formats: [:html]
  end

  store = BackupRestore::BackupStore.create

  if backup = store.file(params.fetch(:id), include_download_source: true)
    EmailBackupToken.del(current_user.id)
    StaffActionLogger.new(current_user).log_backup_download(backup)

    if store.remote?
      redirect_to backup.source, allow_other_host: true
    else
      headers["Content-Length"] = File.size(backup.source).to_s
      send_file backup.source
    end
  else
    render body: nil, status: 404
  end
end

#statusObject



32
33
34
# File 'app/controllers/admin/backups_controller.rb', line 32

def status
  render_json_dump(BackupRestore.operations_status)
end

#upload_backup_chunkObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/controllers/admin/backups_controller.rb', line 175

def upload_backup_chunk
  filename = params.fetch(:resumableFilename)
  total_size = params.fetch(:resumableTotalSize).to_i
  identifier = params.fetch(:resumableIdentifier)

  raise Discourse::InvalidParameters.new(:resumableIdentifier) unless valid_filename?(identifier)
  unless valid_extension?(filename)
    return render status: 415, plain: I18n.t("backup.backup_file_should_be_tar_gz")
  end
  unless has_enough_space_on_disk?(total_size)
    return render status: 415, plain: I18n.t("backup.not_enough_space_on_disk")
  end
  unless valid_filename?(filename)
    return render status: 415, plain: I18n.t("backup.invalid_filename")
  end

  file = params.fetch(:file)
  chunk_number = params.fetch(:resumableChunkNumber).to_i
  chunk_size = params.fetch(:resumableChunkSize).to_i
  current_chunk_size = params.fetch(:resumableCurrentChunkSize).to_i
  previous_chunk_number = chunk_number - 1

  chunk = BackupRestore::LocalBackupStore.chunk_path(identifier, filename, chunk_number)
  HandleChunkUpload.upload_chunk(chunk, file: file)

  # when all chunks are uploaded
  uploaded_file_size = previous_chunk_number * chunk_size
  if uploaded_file_size + current_chunk_size >= total_size
    # merge all the chunks in a background thread
    Jobs.enqueue_in(
      5.seconds,
      :backup_chunks_merger,
      filename: filename,
      identifier: identifier,
      chunks: chunk_number,
    )
  end

  render body: nil
end