Skip to content

Commit 337cc8d

Browse files
committed
Merge pull request #22 from sund/develop
dryrun
2 parents 783bca1 + 9558d51 commit 337cc8d

3 files changed

Lines changed: 208 additions & 65 deletions

File tree

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
## AutoGITBackup
1+
## auto gitlab Backup
22

33
http://sund.la/glup
44

55
----
66

7-
A collection of scripts to use omnibus-gitlab's own backup ```gitlab-rake``` command on a cron schedule and rsync to another server, if wanted, or to restore a backup.
7+
A script to use omnibus-gitlab's own backup ```gitlab-rake``` command on a cron schedule and rsync to another server, if wanted. There is also a restore script available (see below.)
88

9-
Also will backup and copy the Gitlab-CI DB if configured.
9+
It can backup and copy the Gitlab-CI DB, if configured.
1010

11-
This script is now more omnibus-gitlab centric. Compare your config fiile with the template! Usage with a source install is possible but not expressly shown here.
11+
This script is now more omnibus-gitlab centric. Compare your config file with the template! Usage with a source install is possible but not expressly shown here.
1212

1313
#### Clone
1414

@@ -23,7 +23,7 @@ Change ```/etc/gitlab/gitlab.rb``` to expire backups
2323
gitlab_rails['backup_keep_time'] = 604800
2424
```
2525

26-
If you use the CI server, enable Ci Backup expiration
26+
If you use the CI server, enable CI Backup expiration
2727

2828
```
2929
## Backup settings
@@ -42,21 +42,45 @@ cp auto-gitlab-backup.conf.sample auto-gitlab-backup.conf
4242
edit ```auto-gitlab-backup.conf```
4343

4444
```bash
45-
remoteUser="" #user account on remote server
46-
remoteServer="" #remote host
47-
remoteDest="" #remote path
48-
sshKeyPath="" #path to an alternate ssh key, if needed.
49-
remotePort=22 # ssh port
50-
## Only change the below setting if you have git's home in a different location or are installing gitlab from source
45+
## user account on remote server
46+
# likely 'git' user
47+
remoteUser=""
48+
49+
## remote host
50+
# a backup gitlab server?
51+
remoteServer=""
52+
53+
## path to an alternate ssh key, if needed.
54+
sshKeyPath=""
55+
56+
## $remoteServer path for gitlab backups
57+
remoteDest=""
58+
59+
## Using the CI server?
60+
# change to true or 1 to enable CI backups
61+
enableCIBackup="0"
62+
63+
## $remoteServer dest for CI backups on remote
64+
ciRemoteDest="/var/opt/gitlab/ci-backups"
65+
66+
## ssh port or 873 for rsyncd port
67+
remotePort=22
68+
69+
## git user home.
70+
# Only change the below setting if you have git's home in a different location
5171
gitHome="/var/opt/gitlab"
72+
5273
## only set below if rvm is in use and you need to source the rvm env file
5374
# echo $(rvm env --path)
5475
RVM_envPath=""
76+
5577
## only use the below settings if your destination is using rsync in daemon mode
5678
remoteModule=""
5779
rsync_password_file=""
58-
checkQuota="0" #change to true or 1 to enable
59-
enableCIBackup="0" #change to true or 1 to enable CI backups
80+
81+
## Check remote quota
82+
# change to true or 1 to enable
83+
checkQuota="0"
6084
```
6185

6286
#### cron settings

auto-gitlab-backup.conf.sample

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
remoteUser="" #user account on remote server
2-
remoteServer="" #remote host
3-
remoteDest="" #remote path for gitlab backups
4-
ciRemoteDest="/var/opt/gitlab/ci-backups" # diff dest for CI backups on remote
5-
sshKeyPath="" #path to an alternate ssh key, if needed.
6-
remotePort=22 # ssh port or 873 for rsyncd port
7-
## Only change the below setting if you have git's home in a different location
1+
## user account on remote server
2+
# likely 'git' user
3+
remoteUser=""
4+
5+
## remote host
6+
# a backup gitlab server?
7+
remoteServer=""
8+
9+
## path to an alternate ssh key, if needed.
10+
sshKeyPath=""
11+
12+
## $remoteServer path for gitlab backups
13+
remoteDest="/var/opt/gitlab/backups"
14+
15+
## Using the CI server?
16+
# change to true or 1 to enable CI backups
17+
enableCIBackup="0"
18+
19+
## $remoteServer dest for CI backups on remote
20+
ciRemoteDest="/var/opt/gitlab/ci-backups"
21+
22+
## ssh port or 873 for rsyncd port
23+
remotePort=22
24+
25+
## git user home.
26+
# Only change the below setting if you have git's home in a different location
827
gitHome="/var/opt/gitlab"
28+
929
## only set below if rvm is in use and you need to source the rvm env file
1030
# echo $(rvm env --path)
1131
RVM_envPath=""
32+
1233
## only use the below settings if your destination is using rsync in daemon mode
1334
remoteModule=""
1435
rsync_password_file=""
15-
checkQuota="0" #change to true or 1 to enable
16-
enableCIBackup="0" #change to true or 1 to enable CI backups
36+
37+
## Check remote quota
38+
# change to true or 1 to enable
39+
checkQuota="0"

auto-gitlab-backup.sh

Lines changed: 139 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ rsyncUp() {
105105
fi
106106
}
107107

108+
rsyncUp_dryrun() {
109+
# rsync up with default key
110+
echo =============================================================
111+
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\ndefault key\n"
112+
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
113+
114+
# rsync CI backup
115+
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
116+
then
117+
echo ===== rsync a CI backup =====
118+
echo =============================================================
119+
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\ndefault key\n"
120+
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
121+
fi
122+
}
123+
108124
rsyncKey() {
109125
# rsync up with specific key
110126
echo =============================================================
@@ -120,6 +136,22 @@ rsyncKey() {
120136
fi
121137
}
122138

139+
rsyncKey_dryrun() {
140+
# rsync up with specific key
141+
echo =============================================================
142+
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\nwith specific key\n"
143+
rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest
144+
145+
# rsync CI backup
146+
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
147+
then
148+
echo ===== rsync a CI backup =====
149+
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\nwith specific key\n"
150+
rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
151+
fi
152+
}
153+
154+
123155
rsyncDaemon() {
124156
# rsync up with specific key
125157
echo =============================================================
@@ -137,6 +169,24 @@ rsyncDaemon() {
137169

138170
}
139171

172+
rsyncDaemon_dryrun() {
173+
# rsync up with specific key
174+
echo =============================================================
175+
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n"
176+
rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule
177+
178+
# rsync CI backup
179+
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
180+
then
181+
echo ===== rsync a CI backup =====
182+
echo =============================================================
183+
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteCIModule\nin daemon mode\n"
184+
rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeCIBackups/ $remoteUser@$remoteServer::$remoteCIModule
185+
fi
186+
187+
}
188+
189+
140190
sshQuotaKey() {
141191
#quota check: with a key remoteServer, run the quota command
142192
if [[ $checkQuota == "true" || $checkQuota = 1 ]]
@@ -167,53 +217,99 @@ printScriptver() {
167217
echo "Version $(git describe --abbrev=0 --tags --always), commit #$(git log --pretty=format:'%h' -n 1)."
168218
}
169219

170-
###
171-
## Git'r done
172-
#
173-
174-
## test for running as root
175-
if [[ "$UID" -ne "$ROOT_UID" ]];
176-
then
177-
echo "You must run this script as root to run."
178-
exit 1
179-
fi
180-
181-
# read the conffile
182-
if [ -e $confFile -a -r $confFile ]
183-
then
184-
source $confFile
185-
echo "Parsing config file..."
186-
rvm_ENV
187-
else
188-
echo "No confFile found; Remote copy DISABLED."
189-
fi
220+
usage() {
221+
echo ""
222+
echo "Usage:"
223+
echo "$0 -h | --help this help page"
224+
echo "$0 -d | --dry-run test rsync operations; no data transmitted."
225+
echo "$0 no options, perform backup and rsync."
226+
echo ""
227+
}
190228

191-
rakeBackup
192-
rakeCIBackup
193-
checkSize
229+
areWeRoot() {
230+
## test for running as root
231+
if [[ "$UID" -ne "$ROOT_UID" ]];
232+
then
233+
echo "You must run this script as root to run."
234+
if [[ $1 == -d ]] || [[ $1 == --dry-run ]]
235+
then
236+
echo "...even to dryrun as we need to acccess the backup dir."
237+
fi
238+
usage
239+
exit 1
240+
fi
241+
}
194242

195-
# go back to where we came from
196-
cd $PDIR
243+
confFileExist() {
244+
# read the conffile
245+
if [ -e $confFile -a -r $confFile ]
246+
then
247+
source $confFile
248+
echo "Parsing config file..."
249+
rvm_ENV
250+
else
251+
echo "No confFile found; Remote copy DISABLED."
252+
fi
253+
}
197254

198-
# if the $remoteModule is set run rsyncDaemon
199-
## here we assume variables are set right and only check when needed.
200-
if [[ $remoteModule != "" ]]
201-
then
202-
rsyncDaemon
255+
###
256+
## Git'r done
257+
#
203258

204-
# no Daemon so lets see if we are using a special key
205-
else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]]
206-
then
207-
rsyncKey
208-
sshQuotaKey
209-
else if [[ $remoteServer != "" ]]
210-
then
211-
# use the defualt
212-
rsyncUp
213-
sshQuota
214-
fi
215-
fi
216-
fi
259+
case $1 in
260+
-h|--help )
261+
usage
262+
;;
263+
-d|--dry-run )
264+
areWeRoot $1
265+
confFileExist
266+
##test ssh and rsync functions
267+
if [[ $remoteModule != "" ]]
268+
then
269+
rsyncDaemon_dryrun
270+
# no Daemon so lets see if we are using a special key
271+
else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]]
272+
then
273+
rsyncKey_dryrun
274+
sshQuotaKey
275+
else if [[ $remoteServer != "" ]]
276+
then
277+
# use the defualt
278+
rsyncUp_dryrun
279+
sshQuota
280+
fi
281+
fi
282+
fi
283+
;;
284+
* )
285+
areWeRoot $1
286+
confFileExist
287+
# perform backup
288+
rakeBackup
289+
rakeCIBackup
290+
checkSize
291+
# go back to where we came from
292+
cd $PDIR
293+
# if the $remoteModule is set run rsyncDaemo
294+
## here we assume variables are set right and only check when needed.
295+
if [[ $remoteModule != "" ]]
296+
then
297+
rsyncDaemon
298+
# no Daemon so lets see if we are using a special key
299+
else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]]
300+
then
301+
rsyncKey
302+
sshQuotaKey
303+
else if [[ $remoteServer != "" ]]
304+
then
305+
# use the defualt
306+
syncUp
307+
sshQuota
308+
fi
309+
fi
310+
fi
311+
;;
312+
esac
217313

218314
# Print version
219315
printScriptver

0 commit comments

Comments
 (0)