-
Notifications
You must be signed in to change notification settings - Fork 74
/
auto-gitlab-backup.sh
executable file
·486 lines (431 loc) · 13.2 KB
/
auto-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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/bin/bash
################################
#
# autogitbackup.sh
# -----------------------------
#
# this script backups
# /var/opt/gitlab/backups to
# another host
################################
## GPL v2 License
# auto-gitlab-backup
# Copyright (C) 2013 Shaun Sundquist
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
###
## Settings/Variables
#
### in cron job , the path may be just /bin and /usr/bin
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
#
gitHome="$(awk -F: -v v="git" '{if ($1==v) print $6}' /etc/passwd)"
gitlabHome="$gitHome/gitlab"
gitlab_rails="/opt/gitlab/embedded/service/gitlab-rails"
PDIR=$(dirname $(readlink -f $0))
dateStamp=`date +"%F %H:%m:%S"`
confFile="$PDIR/auto-gitlab-backup.conf"
###
## Functions
#
rvm_ENV() {
## environment for rvm
#
# rvm env --path -- ruby-version[@gemset-name]
if [[ "$RVM_envPath" != "" ]]
then
echo "Using RVM environment file:"
echo $RVM_envPath
source $RVM_envPath
fi
}
checkSize() {
echo ===== Sizing =====
echo "Total disk space used for backup storage.."
echo "Size - Location"
echo `du -hs "$gitRakeBackups"`
echo
}
archiveConfig() {
echo ===== Archiving Configs =====
if [[ $backupConfigs = 1 ]]
then
if [[ -w $localConfDir ]]
then
tar -czf "$localConfDir/gitlabConf-$dateStamp.tgz" $localConfig $localsshkeys
# remove files not within 3 days
find $localConfDir -type f -mtime +3 -exec rm -v {} \;
else
echo "$localConfDir is not writable."
fi
else
echo "Local config backups aren't enabled!"
fi
}
rakeBackup() {
echo ===== raking a backup =====
cd $gitRakeBackups
if [[ $quietRake == 1 ]]
then
rakeBackup="gitlab-rake gitlab:backup:create CRON=1"
else
rakeBackup="gitlab-rake gitlab:backup:create"
fi
$rakeBackup
}
rsyncUp() {
# rsync up with default key
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
rsyncUp_dryrun() {
# rsync up with default key
echo =============================================================
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start dry run rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
rsyncKey() {
# rsync up with specific key
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteDest\nwith specific key\n"
rsync -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
rsyncKey_dryrun() {
# rsync up with specific key
echo =============================================================
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\nwith specific key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
rsyncDaemon() {
# rsync up with specific key
echo =============================================================
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n"
rsync -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
rsyncDaemon_dryrun() {
# rsync up with specific key
echo =============================================================
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n"
rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}
sshQuotaKey() {
#quota check: with a key remoteServer, run the quota command
if [[ $checkQuota == "true" || $checkQuota = 1 ]]
then
echo =============================================================
echo -e "Quota check: \n$remoteUser@$remoteServer:$remoteModule\nwith key\n"
ssh -p $remotePort -i $sshKeyPath $remoteUser@$remoteServer "quota"
echo =============================================================
fi
}
sshQuota() {
#quota check: assuming we can ssh into remoteServer, run the quota command
if [[ $checkQuota == "true" || $checkQuota = 1 ]]
then
echo =============================================================
echo -e "Quota check: \n$remoteUser@$remoteServer:$remoteModule\n"
ssh -p $remotePort $remoteUser@$remoteServer "quota"
echo =============================================================
fi
}
printScriptver() {
# print the most recent tag
echo "This is $0"
echo "Version $(git describe --abbrev=0 --tags --always), commit #$(git log --pretty=format:'%h' -n 1)."
}
usage() {
echo ""
echo "Usage:"
echo "$0 -h | --help this help page"
echo "$0 -d | --dry-run test rsync operations; no data transmitted."
echo "$0 no options, perform backup, rsync or b2 operations."
echo ""
}
areWeRoot() {
## test for running as root
if [[ "$UID" -ne "$ROOT_UID" ]];
then
echo "You must run this script as root to run."
if [[ $1 == -d ]] || [[ $1 == --dry-run ]]
then
echo "...even to dryrun as we need to access the backup dir."
fi
usage
exit 1
fi
}
b2Sync() {
# b2 sync
echo =============================================================
echo -e "Start b2 sync of $gitRakeBackups to bucket $b2Bucketname \n"
if [[ $b2blaze == 0 ]]
then
echo "Backblaze b2 file operations not enabled!"
else
# test for b2 command
if type b2 > /dev/null 2>&1
then
# bucketname set and readable
if [ ! -z $b2Bucketname ]
then
if test -r "$gitRakeBackups" -a -d "$gitRakeBackups"
then
b2 sync --noProgress --keepDays $b2keepDays --replaceNewer $gitRakeBackups/ b2://$b2Bucketname/backups/
else
echo " gitRakeBackups ($gitRakeBackups) not readable."
fi
else
echo " b2Bucketname not set."
fi
else
echo " b2 command not found!"
fi
fi
echo ""
}
b2SyncProgress() {
# b2 sync
echo =============================================================
echo -e "Start b2 sync of $gitRakeBackups to bucket $b2Bucketname \n"
if [[ $b2blaze == 0 ]]
then
echo "Backblaze b2 file operations not enabled!"
else
# test for b2 command
if type b2 > /dev/null 2>&1
then
# bucketname set and readable
if [ ! -z $b2Bucketname ]
then
if test -r "$gitRakeBackups" -a -d "$gitRakeBackups"
then
b2 sync --keepDays $b2keepDays --replaceNewer $gitRakeBackups/ b2://$b2Bucketname/backups/
else
echo " gitRakeBackups ($gitRakeBackups) not readable."
fi
else
echo " b2Bucketname not set."
fi
else
echo " b2 command not found!"
fi
fi
echo ""
}
b2SyncConf() {
# b2 sync
echo =============================================================
echo -e "Start b2 sync of /etc/gitlab to bucket $b2Bucketname/configs/ \n"
if [[ $backupConfigs == 1 ]]
then
if [[ $b2blaze == 0 ]]
then
echo "Backblaze b2 file operations not enabled!"
else
# test for b2 command
if type b2 > /dev/null 2>&1
then
# bucketname set and readable
if [ ! -z $b2Bucketname ]
then
if test -r "$gitRakeBackups" -a -d "$gitRakeBackups"
then
b2 sync --noProgress --keepDays $b2keepDays --replaceNewer /etc/gitlab/ b2://$b2Bucketname/configs/
else
echo " gitRakeBackups ($gitRakeBackups) not readable."
fi
else
echo " b2Bucketname not set."
fi
else
echo " b2 command not found!"
fi
fi
fi
echo ""
}
b2SyncConfProgress() {
# b2 sync
echo =============================================================
echo -e "Start b2 sync of /etc/gitlab to bucket $b2Bucketname/configs/ \n"
if [[ $backupConfigs == 1 ]]
then
if [[ $b2blaze == 0 ]]
then
echo "Backblaze b2 file operations not enabled!"
else
# test for b2 command
if type b2 > /dev/null 2>&1
then
# bucketname set and readable
if [ ! -z $b2Bucketname ]
then
if test -r "$gitRakeBackups" -a -d "$gitRakeBackups"
then
b2 sync --keepDays $b2keepDays --replaceNewer /etc/gitlab/ b2://$b2Bucketname/configs/
else
echo " gitRakeBackups ($gitRakeBackups) not readable."
fi
else
echo " b2Bucketname not set."
fi
else
echo " b2 command not found!"
fi
fi
fi
echo ""
}
confFileExist() {
# read the conffile
if [ -e $confFile -a -r $confFile ]
then
source $confFile
echo "Parsing config file..."
rvm_ENV
else
echo "No confFile found; Remote copy DISABLED."
fi
}
###
## Git'r done
#
case $1 in
-h|--help )
usage
;;
-d|--dry-run )
areWeRoot $1
confFileExist
archiveConfig
##test ssh and rsync functions
if [[ $remoteModule != "" ]]
then
rsyncDaemon_dryrun
b2SyncProgress
# no Daemon so lets see if we are using a special key
else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]]
then
rsyncKey_dryrun
b2SyncProgress
sshQuotaKey
else if [[ $remoteServer != "" ]]
then
# use the default
rsyncUp_dryrun
b2SyncProgress
sshQuota
fi
fi
fi
;;
* )
areWeRoot $1
confFileExist
# perform backup
rakeBackup
archiveConfig
checkSize
# go back to where we came from
cd $PDIR
# if the $remoteModule is set run rsyncDaemo
## here we assume variables are set right and only check when needed.
if [[ $remoteModule != "" ]]
then
rsyncDaemon
b2Sync
# no Daemon so lets see if we are using a special key
else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]]
then
rsyncKey
b2Sync
sshQuotaKey
else if [[ $remoteServer != "" ]]
then
# use the default
rsyncUp
b2Sync
sshQuota
else if [[ $b2blaze == "1" ]]
then
# use b2Sync only
b2Sync
fi
fi
fi
fi
;;
esac
## temp
if [[ $b2blaze != 0 ]]
then
b2 get-account-info
b2 list-buckets
fi
# Print version
printScriptver
###
## Exit gracefully
#
exit 0