blob: 787df37a8f8c54fc4ea152a33f5f899c20050b00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
|
# frozen_string_literal: true
require 'active_record/fixtures'
namespace :gitlab do
namespace :backup do
PID = Process.pid.freeze
PID_FILE = "#{Rails.application.root}/tmp/backup_restore.pid"
# Create backup of GitLab system
desc 'GitLab | Backup | Create a backup of the GitLab system'
task create: :gitlab_environment do
lock do
warn_user_is_not_gitlab
Backup::Manager.new(progress).create
end
end
# Restore backup of GitLab system
desc 'GitLab | Backup | Restore a previously created backup'
task restore: :gitlab_environment do
lock do
warn_user_is_not_gitlab
Backup::Manager.new(progress).restore
end
end
namespace :repo do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('repositories')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('repositories')
end
end
end
namespace :db do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('db')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('db')
end
end
end
namespace :builds do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('builds')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('builds')
end
end
end
namespace :uploads do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('uploads')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('uploads')
end
end
end
namespace :artifacts do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('artifacts')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('artifacts')
end
end
end
namespace :pages do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('pages')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('pages')
end
end
end
namespace :lfs do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('lfs')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('lfs')
end
end
end
namespace :terraform_state do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('terraform_state')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('terraform_state')
end
end
end
namespace :registry do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('registry')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('registry')
end
end
end
namespace :packages do
task create: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_create_task('packages')
end
end
task restore: :gitlab_environment do
lock do
Backup::Manager.new(progress).run_restore_task('packages')
end
end
end
def progress
if ENV['CRON']
# We need an object we can say 'puts' and 'print' to; let's use a
# StringIO.
require 'stringio'
StringIO.new
else
$stdout
end
end
def lock
File.open(PID_FILE, File::RDWR | File::CREAT, 0644) do |f|
f.flock(File::LOCK_EX)
unless f.read.empty?
# There is a PID inside so the process fails
progress.puts(<<~HEREDOC.color(:red))
Backup and restore in progress:
There is a backup and restore task in progress. Please, try to run the current task once the previous one ends.
If there is no other process running, please remove the PID file manually: rm #{PID_FILE}
HEREDOC
exit 1
end
f.write(PID)
f.flush
ensure
f.flock(File::LOCK_UN)
end
begin
yield
ensure
progress.puts "#{Time.now} " + "-- Deleting backup and restore lock file".color(:blue)
File.delete(PID_FILE)
end
end
end
# namespace end: backup
end
# namespace end: gitlab
|