-
Notifications
You must be signed in to change notification settings - Fork 1
/
restore-matrix-synapse.sh
executable file
·79 lines (65 loc) · 2.01 KB
/
restore-matrix-synapse.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
#!/bin/sh
# TODO
# See https://stackoverflow.com/questions/29832037/how-to-get-script-directory-in-posix-sh
. "$(dirname "$0")/common.sh"
##################################################
# Restore Matrix Synapse Postgres database.
# Globals:
# MATRIX_SYNAPSE_BACKUP_DIR
# MATRIX_SYNAPSE_DB
# POSTGRES_LABEL
# POSTGRES_NAMESPACE
# Arguments:
# None
##################################################
restore_postgres() {
echo "Restoring Postgres ..."
pod=$(get_pod_name "${POSTGRES_LABEL}" "${POSTGRES_NAMESPACE}")
# Get latest Postgres dump
backup_source_path=$(find "${MATRIX_SYNAPSE_BACKUP_DIR}" -name "*.dump" | sort | tail -n1)
# Confirmation prompt
confirm "${backup_source_path}"
echo "Restoring database: ${MATRIX_SYNAPSE_DB} ..."
kubectl exec -i -n "${POSTGRES_NAMESPACE}" "$pod" -- \
pg_restore --clean --dbname="${MATRIX_SYNAPSE_DB}" <"${backup_source_path}"
}
##################################################
# Restore Matrix Synapse media files.
# Globals:
# MATRIX_NAMESPACE
# MATRIX_SYNAPSE_BACKUP_DIR
# MATRIX_SYNAPSE_LABEL
# Arguments:
# None
##################################################
restore_media() {
echo "Restoring media ..."
# Get latest media backup
backup_source_path=$(find "${MATRIX_SYNAPSE_BACKUP_DIR}" -name '*media.tar.gz' | sort | tail -n1)
# Confirmation prompt
confirm "${backup_source_path}"
tmp="${MATRIX_SYNAPSE_BACKUP_DIR}/media_store"
mkdir -p "${tmp}"
echo "Extracting ${backup_source_path} to ${tmp} ..."
tar -xf "${backup_source_path}" -C "${tmp}"
pod=$(get_pod_name "${MATRIX_SYNAPSE_LABEL}" "${MATRIX_NAMESPACE}")
pod_path="${MATRIX_NAMESPACE}/${pod}:/data"
echo "Copying ${tmp} to ${pod_path} ..."
kubectl cp "${tmp}" "${pod_path}"
echo "Cleaning up ..."
rm -rf "${tmp}"
}
##################################################
# Main function of script.
# Globals:
# None
# Arguments:
# None
##################################################
main() {
restore_postgres
restore_media
echo "Success."
}
# Entrypoint
main