-
Notifications
You must be signed in to change notification settings - Fork 74
/
restoreGitLab.sh
executable file
·382 lines (319 loc) · 8.52 KB
/
restoreGitLab.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
#!/bin/bash
################################
#
# restoreGitLab.sh
# -----------------------------
#
# this script restores
# /var/opt/gitlab/backups to
# current 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"
gitRakeBackups="/var/opt/gitlab/backups"
PDIR=$(dirname $(readlink -f $0))
confFile="$PDIR/auto-gitlab-backup.conf"
rakeRestore="gitlab-rake gitlab:backup:restore"
stopUnicorn="gitlab-ctl stop unicorn"
stopsidekiq="gitlab-ctl stop sidekiq"
restartGitLab="gitlab-ctl restart"
###
## Functions
#
usage() {
echo "${0} will restore a gitlab backup"
echo "This script should be run as root or"
echo "as a user that can read the backups directory"
echo "in $gitRakeBackups"
echo ""
echo "USAGE:"
echo "${0} -l #list backups found in $gitRakeBackups"
echo ""
echo "${0} -r #restore a backup"
exit 0
}
runAs() {
## test for running as root
if [[ "$UID" -ne "$ROOT_UID" ]];
then
echo "You must be logged in as root to run this script."
exit 1
fi
}
sanityCheck() {
echo "git's home is found to be : $gitHome"
echo "gitlab backups are found in : $gitRakeBackups"
}
getFileList() {
echo "File list in : $1"
# best not to use ls to get a listing of a dir
local e=1
shopt -s nullglob
for f in $1/*; do
[[ -e $f ]] && [[ -f $f ]] || continue
fileArray[$e]=$(basename "$f")
((e++))
done
shopt -u nullglob
#echo "fileArray is : ${fileArray[@]}"
#echo "fileArray size is : ${#fileArray[@]}"
## sort array so newest is first
local c=1
for ((i=${#fileArray[@]}; i>=1; i--)); do
#echo " sort i : $i & c $c"
REVfileArray[$c]="${fileArray[$i]}"
((c++))
done
#echo "REVfileArray is : ${REVfileArray[@]}"
#echo "REVfileArray size is : ${#REVfileArray[@]}"
## copy REVfileArray to fileArray
unset fileArray
for ((i=${#REVfileArray[@]}; i>=1; i--)); do
fileArray[$i]="${REVfileArray[$i]}"
done
#echo "did we reasign? : ${fileArray[@]}"
#echo "reset fileArray size is : ${#fileArray[@]}"
}
printFileList() {
#echo "Array is ${fileArray[@]}"
# print out each element with while based
# on # of elements of array
e=1
while [ $e -le ${#fileArray[@]} ]
do
#echo "Element $e is : ${fileArray[$e]}"
((e++))
done
#echo "Reversed is : ${REVfileArray[@]}"
# print each element
local z=1
while [ $z -le ${#fileArray[@]} ]
do
#echo "Element $z of REVfileArray is : ${REVfileArray[$z]}"
echo "${REVfileArray[$z]}"
((z++))
done
}
verifyRestore() {
case "${#fileArray[@]}" in
0)
# if the file list is 0 then exit with message
echo "ERROR: I didn't find any backup files in $gitRakeBackups."
echo "Copy or restore backups to $gitRakeBackups."
exit 1
;;
1)
# if the file list is 1 skip the menu and ask if ready to restore
verifySingle
;;
*)
# if the file list is grather than 1, menu time
## if whiptale installed, use
if `command -v whiptail > /dev/null`
then
chooseBackupWhiped
else
chooseBackup
fi
rakeRestore
;;
esac
}
verifySingle() {
echo "${fileArray[1]} was the only backup file found to restore from."
if [ -f ${fileArray[1]} ]
then
read -p " Proceed with restore? (Y/n)" yesorno
case $yesorno in
y*)
rakeRestoreSingle
;;
n*)
echo "${fileArray[1]} has not been restored"
exit 1
;;
esac
fi
}
chooseBackup() {
echo "Choose from below which backup to restore from:"
echo "** The newest will be at the bottom of the list! **"
## TO DO: add option to exit in array at this point
## TO DO: invert array, newest on top
fileArray=("${fileArray[@]}" "Abort")
select chosen in ${fileArray[@]};
do
echo "you picked $chosen "
break;
done
if [[ "$chosen" == "Abort" ]]
then
echo "Aborting backup!"
exit 1
fi
}
chooseBackupWhiped() {
# make sure we don't have an empty list
WC=`echo ${fileArray[@]} | wc -l`
if [[ "${WC}" -ne 0 ]]
then
# create the whippedList
i=0
s=1 # decimal ASCII "A"
for f in ${fileArray[@]}
do
# convert to octal then ASCII character for selection tag
whippedListArray[i]="$f"
whippedListArray[i+1]=" " # save file name
((i+=2))
((s++))
done
## calc screen height and width
whipHeight=20 # find away to calc a pleasing height
whipWidth=42 # find away to calc a pleasing height
# whiptail uses stdin and stdout to draw boxes, have to reassign with 3>&1 1>&2 2>&3
chosen=$(whiptail --backtitle "Restore a GitLab Backup" --title "Restore from backup" \
--menu "Please select the file to restore" "$whipHeight" "$whipWidth" \
"${#fileArray[@]}" "${whippedListArray[@]}" 3>&1 1>&2 2>&3)
fi
echo " you chose : $chosen"
}
rakeRestore() {
cd $gitlabDir
# if chosen is null then abort
if [ -z "$chosen" ]
then
echo "Cancelling restore..."
exit 1
fi
backupfilename=$(basename "$chosen")
echo $backupfilename
timeStamp=${backupfilename%_gitlab_backup.tar}
echo "timestamp is : $timeStamp"
# restore the chosen backup
#$rakeRestore BACKUP=$timeStamp
echo " rake gitlab:backup:restore returned : $?"
}
rakeRestoreSingle() {
cd $gitlabDir
# restore the only backup available; will complain if it finds more than one
$rakeRestore RAILS_ENV=production
echo " rake gitlab:backup:restore returned : $?"
}
permsFixBase() {
cd $gitlabDir
# Fix the permissions on the repository base
# repositories ahould be found in git's home
sudo chmod -R ug+rwX,o-rwx $gitHome/repositories/
sudo chmod -R ug-s $gitHome/repositories/
sudo find $gitHome/repositories/ -type d -print0 | sudo xargs -0 chmod g+s
#
}
postRestoreLink() {
cd $gitlabDir
# # find a list of repos put into array
local folderFound=("`sudo -u git -H find $gitHome/repositories -name *.git -type d -print`")
declare -a gitfolderArray
gitfolderArray=(${folderFound// / })
echo "I found ${#gitfolderArray[*]} git repos."
#and loop through the array
for i in ${gitfolderArray[@]}; do
echo "Creating symlink to $i/hooks/post-receive"
sudo -u git ln -sf $gitHome/gitlab-shell/hooks/post-receive $i/hooks/post-receive
done
}
#
rakeInfo() {
cd $gitlabDir
# echo "function: rakeInfo"
}
#
#
rakeCheck() {
cd $gitlabDir
gitlab-rake gitlab:check
}
###
## Git'r done
#
case "$1" in
"-l") ## gitlab in place other than /home/git
# find the backup and make a list
runAs
sanityCheck
getFileList $gitRakeBackups
printFileList
verifyRestore
#chooseBackupWhiped
# fix things
## if the rake fails (it will with different git home
# run the fixes
## TO DO determine if we really need to run these
permsFixBase
postRestoreLink
rakeInfo
rakeCheck
;;
"-r") ## gitlab in /home/git
# find the backup and make a list
runAs
sanityCheck
getFileList $gitRakeBackups
#printFileList
verifyRestore
# run the fixes
## TO DO determine if we really need to run these
# permsFixBase
# postRestoreLink
rakeInfo
rakeCheck
;;
"-t")
sanityCheck
getFileList $gitlabBackups
#printFileList
verifyRestore
# fix things
permsFixBase
postRestoreLink
rakeInfo
#rakeCheck
;;
"-f")
permsFixBase
postRestoreLink
rakeInfo
rakeCheck
;;
*)
usage
;;
esac
###
## Exit gracefully
#
exit 0