@@ -10,13 +10,87 @@ set -e
10
10
cd $( dirname " $0 " ) /..
11
11
. libexec/ghe-backup-config
12
12
13
+ # Set up remote host and root elastic backup directory based on config
14
+ host=" $GHE_HOSTNAME "
15
+
13
16
# Perform a host-check and establish GHE_REMOTE_XXX variables.
14
- ghe_remote_version_required " $GHE_HOSTNAME "
17
+ ghe_remote_version_required " $host "
18
+
19
+ # Verify rsync is available.
20
+ if ! rsync --version 1> /dev/null 2>&1 ; then
21
+ echo " Error: rsync not found." 1>&2
22
+ exit 1
23
+ fi
24
+
25
+ # Make sure root backup dir exists if this is the first run
26
+ mkdir -p " $GHE_SNAPSHOT_DIR /elasticsearch"
27
+
28
+ # Verify that the /data/elasticsearch directory exists.
29
+ if ! ghe-ssh " $host " -- " [ -d '$GHE_REMOTE_DATA_USER_DIR /elasticsearch' ]" ; then
30
+ echo " * The '$GHE_REMOTE_DATA_USER_DIR /elasticsearch' directory doesn't exist." 1>&3
31
+ exit 0
32
+ fi
33
+
34
+ # Grab the elasticsearch.yml file which is root owned and mode -rw------- so
35
+ # can't be read via rsync or cat. We use the root allowed grep -F as a cat
36
+ # replacement. This is necessary on v11.10.x appliances only.
37
+ if [ " $GHE_VERSION_MAJOR " -lt 2 ]; then
38
+ echo " * Retrieving elasticsearch.yml config file ..." 1>&3
39
+ ghe-ssh " $host " -- " sudo grep -F '' '$GHE_REMOTE_DATA_USER_DIR /elasticsearch/elasticsearch.yml'" \
40
+ > " $GHE_SNAPSHOT_DIR /elasticsearch/elasticsearch.yml"
41
+ chmod 0600 " $GHE_SNAPSHOT_DIR /elasticsearch/elasticsearch.yml"
42
+ fi
43
+
44
+ # If we have a previous increment, avoid transferring existing files via rsync's
45
+ # --link-dest support. This also decreases physical space usage considerably.
46
+ if [ -d " $GHE_DATA_DIR /current/elasticsearch" ]; then
47
+ link_dest=" --link-dest=../../current/elasticsearch"
48
+ fi
15
49
16
- # Determine which version of ES we're backing up based on appliance version and
17
- # run the appropriate command .
50
+ # Determine which user to run the rsync operation under. This is the git user on
51
+ # v11.10.34x appliances and the elasticsearch user under >= v2.x appliances .
18
52
if [ " $GHE_VERSION_MAJOR " -eq 1 ]; then
19
- exec ghe-backup-es-v0.9-rsync " $@ "
53
+ rsync_user=git
54
+ elif [ " $GHE_VERSION_MAJOR " -ge 2 ]; then
55
+ rsync_user=elasticsearch
20
56
else
21
- exec ghe-backup-es-v1.x-rsync " $@ "
57
+ echo " Error: invalid remote version: $GHE_REMOTE_VERSION " 1>&2
58
+ exit 1
22
59
fi
60
+
61
+ # Transfer ES indices from a GitHub instance to the current snapshot
62
+ # directory, using a previous snapshot to avoid transferring files that have
63
+ # already been transferred.
64
+ echo " * Performing initial sync of ES indices ..." 1>&3
65
+ ghe-rsync -avz \
66
+ -e " ghe-ssh -p $( ssh_port_part " $host " ) " \
67
+ --rsync-path=" sudo -u $rsync_user rsync" \
68
+ $link_dest \
69
+ --exclude=' elasticsearch.yml' \
70
+ " $( ssh_host_part " $host " ) :$GHE_REMOTE_DATA_USER_DIR /elasticsearch/" \
71
+ " $GHE_SNAPSHOT_DIR /elasticsearch" 1>&3
72
+
73
+ # Set up a trap to re-enable flushing on exit
74
+ cleanup () {
75
+ echo " * Enabling ES index flushing ..." 1>&3
76
+ echo ' {"index":{"translog.disable_flush":false}}' |
77
+ ghe-ssh " $host " -- curl -s -XPUT " localhost:9200/_settings" -d @- > /dev/null
78
+ }
79
+ trap ' cleanup' EXIT
80
+ trap ' exit $?' INT # ^C always terminate
81
+
82
+ # Disable ES flushing and force a flush right now
83
+ echo " * Disabling ES index flushing ..." 1>&3
84
+ echo ' {"index":{"translog.disable_flush":true}}' |
85
+ ghe-ssh " $host " -- curl -s -XPUT " localhost:9200/_settings" -d @- > /dev/null
86
+ ghe-ssh " $host " -- curl -s -XPOST " localhost:9200/_flush" > /dev/null
87
+
88
+ # Transfer all ES indices again
89
+ echo " * Performing follow-up sync of ES indices ..." 1>&3
90
+ ghe-rsync -avz \
91
+ -e " ghe-ssh -p $( ssh_port_part " $host " ) " \
92
+ --rsync-path=" sudo -u $rsync_user rsync" \
93
+ $link_dest \
94
+ --exclude=' elasticsearch.yml' \
95
+ " $( ssh_host_part " $host " ) :$GHE_REMOTE_DATA_USER_DIR /elasticsearch/" \
96
+ " $GHE_SNAPSHOT_DIR /elasticsearch" 1>&3
0 commit comments