Commit 5689d89d authored by Aakriti Gupta's avatar Aakriti Gupta Committed by Douglas Barbosa Alexandre

Raise custom errors when backup creation fails

Changelog: other
parent ba1be7e3
...@@ -16,19 +16,6 @@ module Backup ...@@ -16,19 +16,6 @@ module Backup
end end
end end
class RepositoryBackupError < Backup::Error
attr_reader :container, :backup_repos_path
def initialize(container, backup_repos_path)
@container = container
@backup_repos_path = backup_repos_path
end
def message
"Failed to create compressed file '#{backup_repos_path}' when trying to backup the following paths: '#{container.disk_path}'"
end
end
class DatabaseBackupError < Backup::Error class DatabaseBackupError < Backup::Error
attr_reader :config, :db_file_name attr_reader :config, :db_file_name
......
...@@ -61,7 +61,7 @@ module Backup ...@@ -61,7 +61,7 @@ module Backup
report_success(success) report_success(success)
progress.flush progress.flush
raise Backup::Error, 'Backup failed' unless success raise DatabaseBackupError.new(config, db_file_name) unless success
end end
def restore def restore
......
...@@ -37,7 +37,7 @@ module Backup ...@@ -37,7 +37,7 @@ module Backup
unless status == 0 unless status == 0
puts output puts output
raise Backup::Error, 'Backup failed' raise_custom_error
end end
tar_cmd = [tar, exclude_dirs(:tar), %W[-C #{@backup_files_dir} -cf - .]].flatten tar_cmd = [tar, exclude_dirs(:tar), %W[-C #{@backup_files_dir} -cf - .]].flatten
...@@ -49,7 +49,7 @@ module Backup ...@@ -49,7 +49,7 @@ module Backup
end end
unless pipeline_succeeded?(tar_status: status_list[0], gzip_status: status_list[1], output: output) unless pipeline_succeeded?(tar_status: status_list[0], gzip_status: status_list[1], output: output)
raise Backup::Error, "Backup operation failed: #{output}" raise_custom_error
end end
end end
...@@ -143,5 +143,9 @@ module Backup ...@@ -143,5 +143,9 @@ module Backup
end end
end end
end end
def raise_custom_error
raise FileBackupError.new(app_files_dir, backup_tarball)
end
end end
end end
...@@ -135,8 +135,12 @@ namespace :gitlab do ...@@ -135,8 +135,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("db") if ENV["SKIP"] && ENV["SKIP"].include?("db")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Database.new(progress).dump Backup::Database.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::DatabaseBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -168,8 +172,12 @@ namespace :gitlab do ...@@ -168,8 +172,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("builds") if ENV["SKIP"] && ENV["SKIP"].include?("builds")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Builds.new(progress).dump Backup::Builds.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -187,8 +195,12 @@ namespace :gitlab do ...@@ -187,8 +195,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("uploads") if ENV["SKIP"] && ENV["SKIP"].include?("uploads")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Uploads.new(progress).dump Backup::Uploads.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -206,8 +218,12 @@ namespace :gitlab do ...@@ -206,8 +218,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("artifacts") if ENV["SKIP"] && ENV["SKIP"].include?("artifacts")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Artifacts.new(progress).dump Backup::Artifacts.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -225,8 +241,12 @@ namespace :gitlab do ...@@ -225,8 +241,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("pages") if ENV["SKIP"] && ENV["SKIP"].include?("pages")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Pages.new(progress).dump Backup::Pages.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -244,8 +264,12 @@ namespace :gitlab do ...@@ -244,8 +264,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("lfs") if ENV["SKIP"] && ENV["SKIP"].include?("lfs")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Lfs.new(progress).dump Backup::Lfs.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
end end
...@@ -283,8 +307,12 @@ namespace :gitlab do ...@@ -283,8 +307,12 @@ namespace :gitlab do
if ENV["SKIP"] && ENV["SKIP"].include?("registry") if ENV["SKIP"] && ENV["SKIP"].include?("registry")
puts_time "[SKIPPED]".color(:cyan) puts_time "[SKIPPED]".color(:cyan)
else else
begin
Backup::Registry.new(progress).dump Backup::Registry.new(progress).dump
puts_time "done".color(:green) puts_time "done".color(:green)
rescue Backup::FileBackupError => e
progress.puts "#{e.message}"
end
end end
else else
puts_time "[DISABLED]".color(:cyan) puts_time "[DISABLED]".color(:cyan)
......
...@@ -134,7 +134,7 @@ RSpec.describe Backup::Files do ...@@ -134,7 +134,7 @@ RSpec.describe Backup::Files do
expect do expect do
subject.dump subject.dump
end.to raise_error(/Backup operation failed:/) end.to raise_error(/Failed to create compressed file/)
end end
describe 'with STRATEGY=copy' do describe 'with STRATEGY=copy' do
...@@ -170,7 +170,7 @@ RSpec.describe Backup::Files do ...@@ -170,7 +170,7 @@ RSpec.describe Backup::Files do
expect do expect do
subject.dump subject.dump
end.to output(/rsync failed/).to_stdout end.to output(/rsync failed/).to_stdout
.and raise_error(/Backup failed/) .and raise_error(/Failed to create compressed file/)
end end
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Backup::RepositoryBackupError do
let_it_be(:snippet) { create(:snippet, content: 'foo', file_name: 'foo') }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:wiki) { ProjectWiki.new(project, nil ) }
let(:backup_repos_path) { '/tmp/backup/repositories' }
shared_examples 'includes backup path' do
it { is_expected.to respond_to :container }
it { is_expected.to respond_to :backup_repos_path }
it 'expects exception message to include repo backup path location' do
expect(subject.message).to include("#{subject.backup_repos_path}")
end
it 'expects exception message to include container being back-up' do
expect(subject.message).to include("#{subject.container.disk_path}")
end
end
context 'with snippet repository' do
subject { described_class.new(snippet, backup_repos_path) }
it_behaves_like 'includes backup path'
end
context 'with project repository' do
subject { described_class.new(project, backup_repos_path) }
it_behaves_like 'includes backup path'
end
context 'with wiki repository' do
subject { described_class.new(wiki, backup_repos_path) }
it_behaves_like 'includes backup path'
end
end
...@@ -222,6 +222,37 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do ...@@ -222,6 +222,37 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
end end
end end
describe 'backup create fails' do
using RSpec::Parameterized::TableSyntax
file_backup_error = Backup::FileBackupError.new('/tmp', '/tmp/backup/uploads')
config = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash
db_file_name = File.join(Gitlab.config.backup.path, 'db', 'database.sql.gz')
db_backup_error = Backup::DatabaseBackupError.new(config, db_file_name)
where(:backup_class, :rake_task, :error) do
Backup::Database | 'gitlab:backup:db:create' | db_backup_error
Backup::Builds | 'gitlab:backup:builds:create' | file_backup_error
Backup::Uploads | 'gitlab:backup:uploads:create' | file_backup_error
Backup::Artifacts | 'gitlab:backup:artifacts:create' | file_backup_error
Backup::Pages | 'gitlab:backup:pages:create' | file_backup_error
Backup::Lfs | 'gitlab:backup:lfs:create' | file_backup_error
Backup::Registry | 'gitlab:backup:registry:create' | file_backup_error
end
with_them do
before do
expect_next_instance_of(backup_class) do |instance|
expect(instance).to receive(:dump).and_raise(error)
end
end
it "raises an error with message" do
expect { run_rake_task(rake_task) }.to output(Regexp.new(error.message)).to_stdout_from_any_process
end
end
end
context 'tar creation' do context 'tar creation' do
context 'archive file permissions' do context 'archive file permissions' do
it 'sets correct permissions on the tar file' do it 'sets correct permissions on the tar file' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment