-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-backup.sh
279 lines (242 loc) · 7.55 KB
/
gitlab-backup.sh
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash -e
# vim: st=2 sts=2 sw=2 et ai
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-w|--work-dir|--workdir)
WORK="$2"
shift
shift
;;
-d|--destination|--dest)
FINAL="$2"
shift
shift
;;
-u|--user|--remote-user)
REMOTE_USER="$2"
shift
shift
;;
-t|--target|--host)
REMOTE_HOST="$2"
shift
shift
;;
-h|--help)
HELP=1
shift
;;
*)
POSITIONAL_ARGS+=("$1")
esac
done
set -- "${POSITIONAL_ARGS[@]}"
# Ensure required arguments are specified
if [[ -z $WORK ]] || [[ -z $FINAL ]] || [[ -z $REMOTE_USER ]] || [[ -z $REMOTE_HOST ]]; then
HELP=1
fi
latest=
R=$(tput setaf 1 2> /dev/null || echo "")
G=$(tput setaf 2 2> /dev/null || echo "")
H=$(tput setaf 6 2> /dev/null || echo "")
Y=$(tput setaf 3 2> /dev/null || echo "")
B=$(tput bold || echo "")
CL=$(tput sgr0 || echo "")
HCID="1505d3ba-1183-4de1-8f7e-c3ea6ecba4c6"
#KEYFILE=/root/.ssh/id_rsa
messages=""
Debug() {
printf "$(date +%Y-%m-%d\ %H:%M:%S) ${H}${B}[ INFO ]${CL} - $1\n" $*
messages="${messages}$(date +%Y-%m-%d\ %H:%M:%S) INFO: $*"$'\n'
}
Fail() {
printf "$(date +%Y-%m-%d\ %H:%M:%S) ${R}${B}[ FAIL ]${CL} - $1\n" $*
messages="${messages}$(date +%Y-%m-%d\ %H:%M:%S) FAIL: $*"$'\n'
}
Warn() {
printf "$(date +%Y-%m-%d\ %H:%M:%S) ${Y}${B}[ WARN ]${CL} - $1\n" $*
messages="${messages}$(date +%Y-%m-%d\ %H:%M:%S) WARN: $*"$'\n'
}
Done() {
printf "$(date +%Y-%m-%d\ %H:%M:%S) ${G}${B}[ DONE ]${CL} - $1\n" $*
messages="${messages}$(date +%Y-%m-%d\ %H:%M:%S) DONE: $*"$'\n'
}
do_clone() {
# Try to rsync
Debug "Cloning down: ${1##*/}"
rsync \
-vaHAXxr \
--numeric-ids \
--delete \
--progress \
--stats \
--rsync-path="sudo rsync" \
--whole-file \
--exclude +gitaly \
${REMOTE_USER}@${REMOTE_HOST}:${1} ${2};
#-e "ssh -i $KEYFILE -T -o Compression=no -x" \
RESULT=$?
# Check the results
if [ $RESULT -eq 30 ]; then
Fail "rsync failed, got result: $RESULT"
if [ $1 -le 5 ]; then
Warn "retrying... attempt $1/5";
do_backup $(($1+1));
else
Warn "failed... attempt $1/5";
fi
return 1
elif [ $RESULT -ne 0 ]; then
Debug "rsync failed, got result: $RESULT"
Fail "unhanlded error, exiting..."
return $RESULT
fi
Done "Completed clone of: ${B}${G}${1##*/}${CL}"
}
do_archive() {
Debug "Archiving: $1"
tar -C $1 -cf $2 .
Done "Created: $2"
}
db_backup() {
# Try to rsync
Debug "Pulling latest database backups"
rsync \
-aHAXxr \
--numeric-ids \
--rsync-path="sudo rsync" \
--remove-source-files \
--whole-file \
--include '*.tar' \
--exclude '*' \
${REMOTE_USER}@${REMOTE_HOST}:/var/opt/gitlab/backups/ \
$WORK/db/;
#-e "ssh -i $KEYFILE -T -o Compression=no -x" \
RESULT=$?
# Check the results
if [ $RESULT -eq 30 ]; then
Fail "rsync failed, got result: %d" "$RESULT"
if [ $1 -le 5 ]; then
Warn "retrying... attempt $1/5";
do_backup $(($1+1));
else
Warn "failed... attempt $1/5";
fi
return 1
elif [ $RESULT -ne 0 ]; then
Fail "rsync failed, got result: %d" "$RESULT"
Fail "unhandled error, exiting..."
return $RESULT
fi
# With the file cloned, find latest and re-archive
latest=$(basename $(ls -t $WORK/db/*.tar | cat | head -n1))
Debug "Found latest as ${B}${H}${latest}${CL}"
Debug "Extracting database backup to $WORK/intake/"
tar xf $WORK/db/$latest -C $WORK/intake/
Done "Finished preparing database backup"
}
pack_backup() {
local result=
Debug "Packing backup to $FINAL/$1"
tar cf $FINAL/$1 -C $WORK/intake .
result=$?
if [ $result -ne 0 ]; then
Fail "Failed packaging backup, got RC: $result";
return 1;
fi
Done "Packed backup to $FINAL/$1"
}
raise() {
echo "BACKUP FAILED at $1"
echo $messages
exit 1
}
main() {
if ! [[ -z $HELP ]] && [[ $HELP -eq 1 ]]; then
echo "$0 - GitLab Remote Backup
Utilizes rsync and persistent work directories to synchronize content to the
local filesystem, then packs and archives everything to a gitlab-compatible
format.
Args:
--work-dir | -w
Specify the persistent working directory. This should be hot, fast storage.
--destination | -d
Specify the directory for the final output archive. This should be cold,
archival storage.
--user | -u
Specify the remote SSH user for rsync to use for connection. Authentication
will rely on you having an active SSH Agent with the private key loaded.
--target | -t
Specify the remote SSH Server running your gitlab instance.
"
exit 0
fi
# STage our directory structure
mkdir -p $WORK/{intake,artifacts,uploads,builds,lfs,pages}
if [ -z $1 ] || [[ $1 == "repositories" ]]; then
do_clone /srv/gitlab/git-data/repositories $WORK/intake || raise "Clone: Repos"
fi
if [ -z $1 ] || [[ $1 == "uploads" ]]; then
do_clone /var/opt/gitlab/gitlab-rails/uploads $WORK/uploads || raise "Clone: Uploads"
fi
if [ -z $1 ] || [[ $1 == "builds" ]]; then
do_clone /var/opt/gitlab/gitlab-ci/builds $WORK/builds || raise "Clone: Builds"
fi
if [ -z $1 ] || [[ $1 == "artifacts" ]]; then
do_clone /srv/gitlab/gitlab-rails/shared/artifacts $WORK/artifacts || raise "Clone: Artifacts"
fi
if [ -z $1 ] || [[ $1 == "registry" ]]; then
do_clone /srv/gitlab/gitlab-rails/shared/registry $WORK/registry || raise "Clone: Registry"
fi
if [ -z $1 ] || [[ $1 == "pages" ]]; then
do_clone /srv/gitlab/gitlab-rails/shared/pages $WORK/pages || raise "Clone: Pages"
fi
if [ -z $1 ] || [[ $1 == "lfs" ]]; then
do_clone /srv/gitlab/gitlab-rails/shared/lfs-objects $WORK/lfs || raise "Clone: LFS"
fi
if [ -z $1 ] || [[ $1 == "archive" ]]; then
do_archive $WORK/uploads/uploads $WORK/intake/uploads.tar.gz || raise "Archive: Uploads"
do_archive $WORK/builds/builds $WORK/intake/builds.tar.gz || raise "Archive: Builds"
do_archive $WORK/artifacts/artifacts $WORK/intake/artifacts.tar.gz || raise "Archive: Artifacts"
do_archive $WORK/registry/registry $WORK/intake/registry.tar.gz || raise "Archive: Registry"
do_archive $WORK/pages/pages $WORK/intake/pages.tar.gz || raise "Archive: Pages"
do_archive $WORK/lfs $WORK/intake/lfs.tar.gz || raise "Archive: LFS"
fi
if [ -z $1 ] || [[ $1 == "pack" ]]; then
db_backup || raise "Archive: DB"
pack_backup "$latest" || raise "Pack Backup"
fi
if [ -z $1 ] || [[ $1 == "cleanup" ]]; then
if find "$FINAL" -maxdepth 1 -name '*_gitlab_backup.tar' -size 0 -o -empty | grep -q '.'; then
FAil "Partial or incomplete backups found:"
while read line; do
Fail $line;
done < <(find "$FINAL" -maxdepth 1 -name '*_gitlab_backup.tar' -size 0 -o -empty)
exit 1
fi
if find "$FINAL" -type f -maxdepth 1 -name '*_gitlab_backup.tar' -size -40G | grep -q '.'; then
Fail "Backups too small:"
while read line; do
Fail $line;
done < <(find "$FINAL" -maxdepth 1 -size -40G)
exit 1
fi
count=$(find "$FINAL" \
-maxdepth 1 \
-type f \
-mtime -30 \
-name '*gitlab_backup.tar' \
| wc -l);
if [ $count -lt 25 ]; then
Warn "Not enough backups, have $count"
exit 0
fi
Warn "Removing the following backups:"
while read line; do
Debug $line;
done < <(find "$FINAL" -maxdepth 1 -type f -mtime +30 -name '*gitlab_backup.tar' | sort)
find "$FINAL" -maxdepth 1 -type f -mtime +30 -name '*gitlab_backup.tar' -delete
fi
}
main $@