Class: Backup::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/manager.rb

Defined Under Namespace

Classes: TaskDefinition

Constant Summary collapse

FILE_NAME_SUFFIX =
'_gitlab_backup.tar'
MANIFEST_NAME =
'backup_information.yml'
LEGACY_PAGES_TMP_PATH =

pages used to deploy tmp files to this path if some of these files are still there, we don't need them in the backup

'@pages.tmp'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(progress, definitions: nil) ⇒ Manager

Returns a new instance of Manager.


28
29
30
31
32
33
34
35
36
# File 'lib/backup/manager.rb', line 28

def initialize(progress, definitions: nil)
  @progress = progress

  @incremental = Feature.feature_flags_available? &&
    Feature.enabled?(:incremental_repository_backup) &&
    Gitlab::Utils.to_boolean(ENV['INCREMENTAL'], default: false)

  @definitions = definitions || build_definitions
end

Instance Attribute Details

#progressObject (readonly)

Returns the value of attribute progress.


26
27
28
# File 'lib/backup/manager.rb', line 26

def progress
  @progress
end

Instance Method Details

#createObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/backup/manager.rb', line 38

def create
  if incremental?
    unpack(ENV.fetch('PREVIOUS_BACKUP', ENV['BACKUP']))
    read_backup_information
    verify_backup_version
    update_backup_information
  end

  @definitions.keys.each do |task_name|
    run_create_task(task_name)
  end

  write_backup_information

  if skipped?('tar')
    upload
  else
    pack
    upload
    cleanup
    remove_old
  end

  puts_time "Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data \n" \
       "and are not included in this backup. You will need these files to restore a backup.\n" \
       "Please back them up manually.".color(:red)
  puts_time "Backup #{backup_id} is done."
end

#restoreObject


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/backup/manager.rb', line 90

def restore
  cleanup_required = unpack(ENV['BACKUP'])
  read_backup_information
  verify_backup_version

  @definitions.keys.each do |task_name|
    run_restore_task(task_name) if !skipped?(task_name) && enabled_task?(task_name)
  end

  Rake::Task['gitlab:shell:setup'].invoke
  Rake::Task['cache:clear'].invoke

  if cleanup_required
    cleanup
  end

  remove_tmp

  puts_time "Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data \n" \
    "and are not included in this backup. You will need to restore these files manually.".color(:red)
  puts_time "Restore task is done."
end

#run_create_task(task_name) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/backup/manager.rb', line 67

def run_create_task(task_name)
  definition = @definitions[task_name]

  build_backup_information

  unless definition.enabled?
    puts_time "Dumping #{definition.human_name} ... ".color(:blue) + "[DISABLED]".color(:cyan)
    return
  end

  if skipped?(task_name)
    puts_time "Dumping #{definition.human_name} ... ".color(:blue) + "[SKIPPED]".color(:cyan)
    return
  end

  puts_time "Dumping #{definition.human_name} ... ".color(:blue)
  definition.task.dump(File.join(Gitlab.config.backup.path, definition.destination_path), full_backup_id)
  puts_time "Dumping #{definition.human_name} ... ".color(:blue) + "done".color(:green)

rescue Backup::DatabaseBackupError, Backup::FileBackupError => e
  puts_time "Dumping #{definition.human_name} failed: #{e.message}".color(:red)
end

#run_restore_task(task_name) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/backup/manager.rb', line 113

def run_restore_task(task_name)
  definition = @definitions[task_name]

  unless definition.enabled?
    puts_time "Restoring #{definition.human_name} ... ".color(:blue) + "[DISABLED]".color(:cyan)
    return
  end

  puts_time "Restoring #{definition.human_name} ... ".color(:blue)

  warning = definition.task.pre_restore_warning
  if warning.present?
    puts_time warning.color(:red)
    Gitlab::TaskHelpers.ask_to_continue
  end

  definition.task.restore(File.join(Gitlab.config.backup.path, definition.destination_path))

  puts_time "Restoring #{definition.human_name} ... ".color(:blue) + "done".color(:green)

  warning = definition.task.post_restore_warning
  if warning.present?
    puts_time warning.color(:red)
    Gitlab::TaskHelpers.ask_to_continue
  end

rescue Gitlab::TaskAbortedByUserError
  puts_time "Quitting...".color(:red)
  exit 1
end