-
Notifications
You must be signed in to change notification settings - Fork 1
/
replicator-snapshots.sh
124 lines (108 loc) · 3.83 KB
/
replicator-snapshots.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
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
#!/usr/bin/dumb-init /bin/bash
LOCAL_PATH=/backup/current
RESTORE_PATH=/backup/restored
BACKUP_FILE_NAME=backup.sql
node server/server.js &
PID=$!
declare -a tables=("monitor" "monitor_tag" "tag" "monitor_notification" "notification" "monitor_group" "group" "status_page" "monitor_maintenance" "maintenance" "maintenance_status_page" "incident")
function notify_backup {
[ -n "$NTFY_URL" ] && curl -d "[$HOSTNAME] Replicator Kuma Backup Successful. Time: $(date)" $NTFY_URL
}
function notify_restore {
[ -n "$NTFY_URL" ] && curl -d "[$HOSTNAME] Replicator Kuma Restored Successfully. Time: $(date)" $NTFY_URL
}
function dump_tables {
echo 'dump_tables'
# remove previous backup (if any)
rm -rf $LOCAL_PATH
mkdir $LOCAL_PATH
# append table dumps
for table in "${tables[@]}"
do
sqlite3 /app/data/kuma.db '.dump "'"$table"'"' >> $LOCAL_PATH/$BACKUP_FILE_NAME
done
}
function stop_kuma {
kill $PID
# wait for kuma to stop
while ps -p $PID >/dev/null 2>&1
do
sleep 1;
done
}
function start_kuma {
node server/server.js &
PID=$!
}
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 test -f "$RESTORE_PATH/$LOCAL_PATH/$BACKUP_FILE_NAME"; then
restic snapshots --json | jq '.[-1]' > /backup/latest.json
SNAPSHOT_SHA256SUM=$(sha256sum /backup/latest.json | awk '{ print $1 }' )
# If file does not exist, create an empty file
if ! [ -f "/backup/local.json" ]; then
touch /backup/local.json
fi
LOCAL_SHA256SUM=$(sha256sum /backup/local.json | awk '{ print $1 }' )
if [ $LOCAL_SHA256SUM != $SNAPSHOT_SHA256SUM ]
then
echo 'restoring remote backup'
stop_kuma
# echo 'Dropping tables'
for table in "${tables[@]}"
do
# echo 'DROP TABLE "'"$table"'";'
sqlite3 /app/data/kuma.db 'DROP TABLE "'"$table"'";' # && echo 'Table "'"$table"'" dropped'
done
# Restore DB Data
# echo 'Restoring data'
cat $RESTORE_PATH/$LOCAL_PATH/$BACKUP_FILE_NAME | sqlite3 /app/data/kuma.db # && echo 'tables restored'
# echo 'Starting services'
start_kuma
# clone latest to local
cp /backup/{latest.json,local.json}
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 test -f "$RESTORE_PATH/$LOCAL_PATH/$BACKUP_FILE_NAME"; then
SNAPSHOT_SHA256SUM=$(sha256sum $RESTORE_PATH/$LOCAL_PATH/$BACKUP_FILE_NAME | awk '{ print $1 }' )
LOCAL_SHA256SUM=$(sha256sum $LOCAL_PATH/$BACKUP_FILE_NAME | awk '{ print $1 }' )
if [ $LOCAL_SHA256SUM != $SNAPSHOT_SHA256SUM ]
then
echo 'running restic backup'
restic backup $LOCAL_PATH
notify_backup
else
echo 'file is the same - skipping backup'
fi
else
echo 'backups are not available or cannot be restored - trying to initiate seed backup anyway'
restic backup $LOCAL_PATH
fi
rm -rf $RESTORE_PATH
}
function restorecon {
restic_restore
}
function backupcon {
dump_tables
restic_backup
}
if [ $REPLICATOR_MODE = 'BACKUP' ]
then
while true; do sleep 300 && backupcon; done # Backup every 5 mins
fi
# Restore every 6 mins
while true; do sleep 360 && restorecon; done