Skip to content

Commit 1720736

Browse files
authored
Merge pull request #487 from github/juruen/fix-auditlog-flag
Fix audit log import to MySQL flag removal for old snapshots and skip rsync'ed indices
2 parents a9a277b + e46f8c9 commit 1720736

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

share/github-backup-utils/ghe-backup-es-rsync

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ fi
2727
# Make sure root backup dir exists if this is the first run
2828
mkdir -p "$GHE_SNAPSHOT_DIR/elasticsearch"
2929

30+
# Create exclude file
31+
exclude_file="$(mktemp)"
32+
echo elasticsearch.yml >"$exclude_file"
33+
34+
# Exclude audit log indices when configuration says so and import to MySQL is complete
35+
# as those indices will be rebuilt from MySQL during a restore
36+
if [ "$GHE_BACKUP_ES_AUDIT_LOGS" = "no" ] && ghe-ssh "$host" test -e "/data/user/common/audit-log-import/complete"; then
37+
ghe_verbose "* Excluding Audit Log indices"
38+
ghe-ssh "$host" curl -s 'http://localhost:9201/_cat/indices/audit_log?h=uuid' >>$exclude_file 2>&3
39+
fi
40+
3041
# Verify that the /data/elasticsearch directory exists.
3142
if ! ghe-ssh "$host" -- "[ -d '$GHE_REMOTE_DATA_USER_DIR/elasticsearch' ]"; then
3243
ghe_verbose "* The '$GHE_REMOTE_DATA_USER_DIR/elasticsearch' directory doesn't exist."
@@ -47,15 +58,16 @@ ghe-rsync -avz \
4758
-e "ghe-ssh -p $(ssh_port_part "$host")" \
4859
--rsync-path="sudo -u elasticsearch rsync" \
4960
$link_dest \
50-
--exclude='elasticsearch.yml' \
61+
--exclude-from="$exclude_file" \
5162
"$(ssh_host_part "$host"):$GHE_REMOTE_DATA_USER_DIR/elasticsearch/" \
5263
"$GHE_SNAPSHOT_DIR/elasticsearch" 1>&3
5364

54-
# Set up a trap to re-enable flushing on exit
65+
# Set up a trap to re-enable flushing on exit and remove temp file
5566
cleanup () {
5667
ghe_verbose "* Enabling ES index flushing ..."
5768
echo '{"index":{"translog.disable_flush":false}}' |
5869
ghe-ssh "$host" -- curl -s -XPUT "localhost:9200/_settings" -d @- >/dev/null
70+
ghe-ssh "$host" rm -rf "$exclude_file"
5971
}
6072
trap 'cleanup' EXIT
6173
trap 'exit $?' INT # ^C always terminate
@@ -72,7 +84,7 @@ ghe-rsync -avz \
7284
-e "ghe-ssh -p $(ssh_port_part "$host")" \
7385
--rsync-path="sudo -u elasticsearch rsync" \
7486
$link_dest \
75-
--exclude='elasticsearch.yml' \
87+
--exclude-from="$exclude_file" \
7688
"$(ssh_host_part "$host"):$GHE_REMOTE_DATA_USER_DIR/elasticsearch/" \
7789
"$GHE_SNAPSHOT_DIR/elasticsearch" 1>&3
7890

share/github-backup-utils/ghe-restore-audit-log

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ mysql_restored_enabled(){
3434
test -e "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/audit-log-mysql"
3535
}
3636

37+
remove_complete_flag(){
38+
ghe_verbose "Setting instance(s) as pending for audit log import to MySQL"
39+
ghe-ssh "$GHE_HOSTNAME" -- "sudo rm -rf $GHE_REMOTE_ROOT_DIR/data/user/common/audit-log-import/complete" 1>&3 2>&3
40+
41+
if $CLUSTER; then
42+
if ! ghe-ssh "$GHE_HOSTNAME" -- "ghe-cluster-each -- sudo rm -rf /data/user/common/audit-log-import/complete" 1>&3 2>&3; then
43+
ghe_verbose "Failed to set as pending for audit log import to MySQL all instances in cluster"
44+
fi
45+
fi
46+
}
47+
3748
# Use `ghe-backup-mysql-audit-log` to dump the audit entries.
3849
# If the import to MySQL is complete, add a flag in the snapshot to indicate so.
3950
restore_mysql(){
@@ -42,8 +53,7 @@ restore_mysql(){
4253
"${base_path}/ghe-restore-mysql-audit-log" "$GHE_HOSTNAME"
4354

4455
if ! is_import_complete; then
45-
ghe_verbose "Audit log import to MySQL is not complete"
46-
ghe-ssh "$GHE_HOSTNAME" -- "sudo rm -rf $GHE_REMOTE_ROOT_DIR/data/user/common/audit-log-import/complete" 1>&3 2>&3
56+
remove_complete_flag
4757
return
4858
fi
4959

@@ -88,6 +98,7 @@ do_restore(){
8898
restore_mysql
8999
else
90100
ghe_verbose "MySQL audit log restore is not enabled"
101+
remove_complete_flag
91102
fi
92103

93104
if es_restore_enabled; then

test/test-ghe-restore.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,32 @@ begin_test "ghe-restore with no pages backup"
246246
)
247247
end_test
248248

249+
begin_test "ghe-restore removes audit log import to MySQL flag when is a < 2.17 snapshot"
250+
(
251+
set -e
252+
253+
rm -rf "$GHE_REMOTE_ROOT_DIR"
254+
setup_remote_metadata
255+
256+
# set as configured, enable maintenance mode and create required directories
257+
setup_maintenance_mode "configured"
258+
259+
flag="$GHE_REMOTE_ROOT_DIR/data/user/common/audit-log-import/complete"
260+
mkdir -p "$(dirname $flag)"
261+
touch "$flag"
262+
263+
if ! output=$(ghe-restore -v -f localhost 2>&1); then
264+
echo "Error: failed to restore $output" >&2
265+
exit 1
266+
fi
267+
268+
! test -e "$flag" || {
269+
echo "Error: the restore process should've removed $flag" >&2
270+
exit 1
271+
}
272+
)
273+
end_test
274+
249275
begin_test "ghe-restore cluster backup to non-cluster appliance"
250276
(
251277
set -e

0 commit comments

Comments
 (0)