-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreplicator-functions.sh
More file actions
136 lines (123 loc) · 5.02 KB
/
replicator-functions.sh
File metadata and controls
136 lines (123 loc) · 5.02 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/dumb-init /bin/bash
LOCAL_PATH=/replicator_kuma/current
RESTORE_PATH=/replicator_kuma/restored
MYSQL_PID_FILE="/app/data/run/mysqld.pid"
LOCAL_SHA256SUM="X"
PID=-1
function stop_kuma {
echo "[replicator kuma] [control module] stopping uptime-kuma"
kill $PID
# wait for kuma to stop
while ps -p $PID >/dev/null 2>&1
do
sleep 1;
done
}
function start_kuma {
echo "[replicator kuma] [control module] starting uptime-kuma"
node server/server.js &
PID=$!
while ! nc -z localhost 3001; do
sleep 1
done
echo "[replicator kuma] [control module] uptime-kuma is up and running"
}
function kill_embedded_db {
echo "[replicator kuma] [control module] stopping embedded mariadb"
if [ -f "$MYSQL_PID_FILE" ]; then
PID=$(cat "$MYSQL_PID_FILE")
kill -9 "$PID"
fi
}
function notify_backup {
echo "[replicator kuma] [control module] backup successful"
[ -n "$NTFY_URL" ] && curl -d "[$HOSTNAME] Replicator Kuma Backup Successful. Time: $(date)" $NTFY_URL
}
function notify_restore {
echo "[replicator kuma] [control module] restore successful"
[ -n "$NTFY_URL" ] && curl -d "[$HOSTNAME] Replicator Kuma Restored Successfully. Time: $(date)" $NTFY_URL
}
function restic_restore {
# check SHA256s to see if backup is new, restic creates snapshots in cases of no change too
# see: https://github.com/restic/restic/issues/662
restic restore latest --target $RESTORE_PATH
if [ "$(ls -A $RESTORE_PATH)" ]; then
restic snapshots --json | jq '.[-1]' > /replicator_kuma/latest.json
SNAPSHOT_SHA256SUM=$(sha256sum /replicator_kuma/latest.json | awk '{ print $1 }' )
echo "[restore] Snapshot: $SNAPSHOT_SHA256SUM Local: $LOCAL_SHA256SUM"
if [ $LOCAL_SHA256SUM != $SNAPSHOT_SHA256SUM ]
then
echo 'restoring remote backup'
stop_kuma
ln -s $RESTORE_PATH $LOCAL_PATH
# Restore DB Data
node /app/replicator-kuma/database-importer.js
# echo 'Starting services'
kill_embedded_db # uptime kuma will fail to start up if mariadb is already running
start_kuma
# clone latest checksum to local
cp /replicator_kuma/{latest.json,local.json}
LOCAL_SHA256SUM=$SNAPSHOT_SHA256SUM
notify_restore
else
echo 'remote and local are in sync'
fi
rm -rf $RESTORE_PATH
else
echo 'backups are not available or cannot be restored'
fi
}
function restic_backup {
# check SHA256s to see if backup is new, restic creates snapshots in cases of no change too
# see: https://github.com/restic/restic/issues/662
restic restore latest --target $RESTORE_PATH
if [ "$(ls -A $RESTORE_PATH)" ]; then
# create a sha sum of every file in restore and local path and sum this checksum output for change detection
SNAPSHOT_FILES_CHECKSUM=$(find $RESTORE_PATH -type f -exec sha256sum {} + | sort -k 2)
echo "$SNAPSHOT_FILES_CHECKSUM" > /replicator_kuma/snapshot_files_checksum
CURRENT_FILES_CHECKSUM=$(find $LOCAL_PATH -type f -exec sha256sum {} + | sort -k 2)
echo "$SNAPSHOT_FILES_CHECKSUM" > /replicator_kuma/current_files_checksum
diff /replicator_kuma/snapshot_files_checksum /replicator_kuma/current_files_checksum
SNAPSHOT_SHA256SUM=$(echo "$SNAPSHOT_FILES_CHECKSUM" | awk '{ print $1 }' | sha256sum | awk '{ print $1 }')
LOCAL_SHA256SUM=$(echo "$CURRENT_FILES_CHECKSUM" | awk '{ print $1 }' | sha256sum | awk '{ print $1 }')
echo "[backup] Snapshot: $SNAPSHOT_SHA256SUM Local: $LOCAL_SHA256SUM"
if [ $LOCAL_SHA256SUM != $SNAPSHOT_SHA256SUM ]
then
echo '[replicator kuma] [control module] running restic backup'
cd $LOCAL_PATH
restic backup .
cd /app
notify_backup
else
echo '[replicator kuma] [control module] file is the same - skipping backup'
fi
else
echo '[replicator kuma] [control module] backups are not available or cannot be restored - trying to initiate seed backup anyway'
restic backup $LOCAL_PATH
fi
rm -rf $RESTORE_PATH
}
function wait_init {
# This prevents replicator kuma from interrupting a new instance creation
while [ ! -s "/app/data/db-config.json" ]; do
echo "[replicator kuma] [control module] DB Config file not found or is empty. Waiting for initilisation..."
sleep 5
done
echo "[replicator kuma] [control module] uptime-kuma is initialised, starting replicator kuma..."
# Finally, wait for 5 more secs to give uptime-kuma time to initialise the DB
sleep 5
}
function restorecon {
if [ "$REPLICATOR_MODE" != 'RESTORE_LOCAL_ENTITY_REPLICATION' ]
then
export REPLICATOR_MODE='RESTORE'
fi
restic_restore
}
function backupcon {
# remove previous backup (if any)
rm -rf /replicator_kuma/current
node /app/replicator-kuma/database-exporter.js
node /app/replicator-kuma/csv2sql.js
restic_backup
}