@@ -22,55 +22,70 @@ setup(){
22
22
ghe_remote_version_required " $GHE_HOSTNAME "
23
23
}
24
24
25
- # Check whether the snapshot comes from an instance
26
- # where the MySQL import was complete
27
- is_import_complete (){
28
- test -e " $GHE_DATA_DIR /$GHE_RESTORE_SNAPSHOT /audit-log/mysql-import-complete "
25
+ # Check whether the snapshot contains audit logs that
26
+ # were taken from Elasticsearch
27
+ es_data_available (){
28
+ ls -A " $GHE_DATA_DIR /$GHE_RESTORE_SNAPSHOT " /audit-log/* .size > /dev/null 2>&1
29
29
}
30
30
31
- # Check whether the snapshot was taken on an instance
32
- # where MySQL audit logs were enabled
33
- mysql_restored_enabled (){
34
- test -e " $GHE_DATA_DIR /$GHE_RESTORE_SNAPSHOT /audit-log-mysql"
31
+ # Check whether the snapshot contains audit logs that
32
+ # were taken from MySQL
33
+ mysql_dump_available (){
34
+ ls -A " $GHE_DATA_DIR /$GHE_RESTORE_SNAPSHOT " /audit-log-mysql/20 * .gz > /dev/null 2>&1
35
35
}
36
36
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
37
+ # Check whether the snapshot contains the audit log table schema
38
+ mysql_table_schema_available (){
39
+ ls -A " $GHE_DATA_DIR /$GHE_RESTORE_SNAPSHOT " /audit-log-mysql/schema.gz > /dev/null 2>&1
40
+ }
40
41
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
42
+ # Check whether the remote host is running a version where the MySQL backend
43
+ # is supported, i.e: < 2.19
44
+ is_mysql_supported (){
45
+ [ " $( version " $GHE_REMOTE_VERSION " ) " -lt " $( version 2.19.0) " ]
46
46
}
47
47
48
- # Use `ghe-backup-mysql-audit-log` to dump the audit entries.
49
- # If the import to MySQL is complete, add a flag in the snapshot to indicate so.
50
- restore_mysql (){
51
- ghe_verbose " Restoring MySQL audit logs ..."
48
+ # Helper function to set remote flags in `/data/user/common/audit-log-import`
49
+ # if it's supported, i.e: directory exists.
50
+ set_remote_flag (){
51
+ local flag=$1
52
+ local msg=$2
52
53
53
- " ${base_path} /ghe-restore-mysql- audit-log" " $GHE_HOSTNAME "
54
+ local dir= " /data/user/common/ audit-log-import "
54
55
55
- if ! is_import_complete ; then
56
- remove_complete_flag
56
+ if ! ghe-ssh " $GHE_HOSTNAME " -- " sudo test -d $GHE_REMOTE_ROOT_DIR / $dir " 1>&3 2>&3 ; then
57
+ ghe_verbose " Remote version doesn't support audit log import, skipping ' $msg ' "
57
58
return
58
59
fi
59
60
60
- ghe_verbose " Audit log import to MySQL is complete"
61
- ghe-ssh " $GHE_HOSTNAME " -- " sudo touch $GHE_REMOTE_ROOT_DIR /data/user/common/audit-log-import/complete"
62
- }
61
+ ghe_verbose " $msg "
62
+ ghe-ssh " $GHE_HOSTNAME " -- " sudo touch $GHE_REMOTE_ROOT_DIR /$dir /$flag " 1>&3 2>&3
63
63
64
- # Audit log indices in Elasticsearch are restored when:
65
- #
66
- # - import to MySQL is not complete
67
- # - GHE_BACKUP_ES_AUDIT_LOGS is not set to 'no'
68
- es_restore_enabled (){
69
- if ! is_import_complete; then
70
- return
64
+ if $CLUSTER ; then
65
+ if ! ghe-ssh " $GHE_HOSTNAME " -- " ghe-cluster-each -- sudo touch $dir /$flag " 1>&3 2>&3 ; then
66
+ ghe_verbose " Failed to $msg in all instances in cluster"
67
+ fi
71
68
fi
69
+ }
70
+
71
+ # Add flag to not trigger transitions from MySQL to Elasticsearch
72
+ set_skip_transition_flag (){
73
+ set_remote_flag " skip" " Add flag to skip audit log import to MySQL"
74
+ }
72
75
73
- [ -z " $GHE_BACKUP_ES_AUDIT_LOGS " ] || [ " $GHE_BACKUP_ES_AUDIT_LOGS " != " no" ]
76
+ # Add flag to not trigger the truncation of the MySQL audit log table
77
+ set_skip_truncate_flag (){
78
+ set_remote_flag " skip_truncate" " Add flag to skip truncating audit log table in MySQL"
79
+ }
80
+
81
+ # Use `ghe-backup-mysql-audit-log` to dump the audit entries.
82
+ # If the import to MySQL is complete, add a flag in the snapshot to indicate so.
83
+ restore_mysql (){
84
+ local only_schema=$1
85
+
86
+ ghe_verbose " Restoring MySQL audit logs ..."
87
+
88
+ " ${base_path} /ghe-restore-mysql-audit-log" " $GHE_HOSTNAME " " $only_schema "
74
89
}
75
90
76
91
# Use ghe-restore-es-audit-log to restore Elasticsearch indices
@@ -80,37 +95,44 @@ restore_es(){
80
95
" ${base_path} /ghe-restore-es-audit-log" " $GHE_HOSTNAME "
81
96
}
82
97
83
- # Whether or not we should trigger a reindex from MySQL into Elasticsearch
84
- should_start_reindex (){
85
- if [ -z " $GHE_BACKUP_ES_AUDIT_LOGS " ] || [ " $GHE_BACKUP_ES_AUDIT_LOGS " != " no" ]; then
86
- ghe_verbose " GHE_BACKUP_ES_AUDIT_LOGS is not set to 'no'"
87
- return 1
88
- fi
89
-
90
- if ! ghe-ssh " $GHE_HOSTNAME " -- " test -e /usr/local/share/enterprise/ghe-auditlog-repair" ; then
91
- ghe_verbose " ghe-auditlog-repiar doesn't exist"
92
- return 1
93
- fi
94
- }
95
-
96
98
do_restore (){
97
- if mysql_restored_enabled; then
98
- restore_mysql
99
- else
100
- ghe_verbose " MySQL audit log restore is not enabled"
101
- remove_complete_flag
99
+ if is_mysql_supported; then
100
+ set_skip_transition_flag
102
101
fi
103
102
104
- if es_restore_enabled; then
103
+ # ES data is available, restore it along
104
+ # with the table schema
105
+ if es_data_available; then
106
+ ghe_verbose " Elasticsearch data is available"
107
+
105
108
restore_es
109
+ restore_mysql --only-schema
106
110
return
107
111
fi
108
112
109
- ghe_verbose " Elasticsearch audit log restore is not enabled"
113
+ # Only MySQL data is available, restore it
114
+ # and trigger a reindex
115
+ if mysql_dump_available; then
116
+ ghe_verbose " Only MySQL data is available"
117
+
118
+ restore_mysql
119
+
120
+ if ! is_mysql_supported; then
121
+ ghe_verbose " Add flag to skip MySQL audit log table truncation"
122
+ set_skip_truncate_flag
123
+ fi
110
124
111
- if should_start_reindex; then
112
125
ghe_verbose " Starting audit log reindex from MySQL to Elasticsearch"
113
126
ghe-ssh " $GHE_HOSTNAME " -- " sudo systemctl --no-block restart auditlog-repair" ;
127
+ return
128
+ fi
129
+
130
+ if mysql_table_schema_available; then
131
+ # Only the table schema is available, restore it
132
+ ghe_verbose " Only audit_entries schema is available"
133
+ restore_mysql --only-schema
134
+ else
135
+ ghe_verbose " MySQL table schema is not available"
114
136
fi
115
137
}
116
138
0 commit comments