105 lines
4.5 KiB
Bash
Executable File
105 lines
4.5 KiB
Bash
Executable File
#!/bin/bash
|
|
# ============================================================
|
|
# Matrix: проверка бэкапов, офсайт и отметки в мониторинге
|
|
#
|
|
# Сами бэкапы делает /root/matrix-backups/backup-matrix.sh (03:00).
|
|
# Этот скрипт (03:20) проверяет свежесть, копирует базу на VPS,
|
|
# разворачивает дамп в тестовую базу и отправляет heartbeat в Kuma.
|
|
# ============================================================
|
|
set -uo pipefail
|
|
|
|
SRC=/root/matrix-backups
|
|
DEST=/mnt/backups/matrix
|
|
mkdir -p "$DEST"
|
|
|
|
TOKEN_LOCAL=$(cat /root/.kuma-matrix-local)
|
|
TOKEN_OFFSITE=$(cat /root/.kuma-matrix-offsite)
|
|
TOKEN_RESTORE=$(cat /root/.kuma-matrix-restore)
|
|
|
|
VPS=root@193.233.210.184
|
|
VPS_DIR=/var/backups/matrix
|
|
|
|
# аккаунты ботов: они появляются в течение дня, это не потеря данных
|
|
BOTS="@media @photo @book @home @infra @search @digest @cam"
|
|
|
|
kuma() {
|
|
[ -z "${1:-}" ] && return 0
|
|
curl -s -o /dev/null --max-time 10 \
|
|
"http://192.168.1.107:3001/api/push/$1?status=up&msg=$2&ping=1" || true
|
|
}
|
|
|
|
log() { echo "[$(date '+%F %T')] $*"; }
|
|
|
|
log "старт проверки"
|
|
|
|
LATEST_PG=$(ls -1t "$SRC"/pg/*.sql.gz 2>/dev/null | head -1)
|
|
LATEST_FILES=$(ls -1t "$SRC"/files/*.tar.gz 2>/dev/null | head -1)
|
|
|
|
if [ -z "$LATEST_PG" ] || [ -z "$LATEST_FILES" ]; then
|
|
log "ОШИБКА: бэкапов нет (pg='$LATEST_PG' files='$LATEST_FILES')"
|
|
exit 1
|
|
fi
|
|
|
|
AGE_H=$(( ( $(date +%s) - $(stat -c %Y "$LATEST_PG") ) / 3600 ))
|
|
log "дамп: $(basename "$LATEST_PG") ($(du -h "$LATEST_PG" | cut -f1)), возраст ${AGE_H} ч"
|
|
|
|
if [ "$AGE_H" -gt 30 ]; then
|
|
log "ОШИБКА: дамп старше 30 часов"
|
|
exit 1
|
|
fi
|
|
kuma "$TOKEN_LOCAL" "matrix-local-ok"
|
|
|
|
# офсайт
|
|
STAMP=$(date +%Y-%m-%d)
|
|
if ssh -o BatchMode=yes -o ConnectTimeout=10 "$VPS" \
|
|
"cat > $VPS_DIR/synapse-db-$STAMP.sql.gz" < "$LATEST_PG" 2>/dev/null; then
|
|
log "офсайт: база на VPS"
|
|
kuma "$TOKEN_OFFSITE" "matrix-offsite-ok"
|
|
else
|
|
log "офсайт: VPS недоступен"
|
|
fi
|
|
|
|
# проверка восстановления
|
|
cat > /root/.mx-restore.sh <<'EOF'
|
|
#!/bin/bash
|
|
su - postgres -c "psql -c 'drop database if exists matrix_restore_test'" >/dev/null 2>&1
|
|
su - postgres -c "psql -c 'create database matrix_restore_test'" >/dev/null 2>&1
|
|
zcat /tmp/mx-restore.sql.gz > /tmp/mx-restore.sql
|
|
su - postgres -c "psql -v ON_ERROR_STOP=1 -q -d matrix_restore_test -f /tmp/mx-restore.sql" >/dev/null 2>/tmp/mx-err.log
|
|
CODE=$?
|
|
echo "код=$CODE ошибок=$(grep -c '^ERROR' /tmp/mx-err.log 2>/dev/null || echo 0)"
|
|
echo "users=$(su - postgres -c "psql -d matrix_restore_test -tAc 'select count(*) from users'" | tr -d ' \r\n')"
|
|
echo "events=$(su - postgres -c "psql -d matrix_restore_test -tAc 'select count(*) from events'" | tr -d ' \r\n')"
|
|
rm -f /tmp/mx-restore.sql /tmp/mx-err.log
|
|
EOF
|
|
|
|
if pct push 151 "$LATEST_PG" /tmp/mx-restore.sql.gz --perms 644 >/dev/null 2>&1 \
|
|
&& pct push 151 /root/.mx-restore.sh /root/.mx-restore.sh --perms 755 >/dev/null 2>&1; then
|
|
OUT=$(pct exec 151 -- bash /root/.mx-restore.sh 2>&1)
|
|
CODE_LINE=$(echo "$OUT" | head -1)
|
|
USERS=$(echo "$OUT" | sed -n 's/^users=//p' | tr -d ' \r')
|
|
EVENTS=$(echo "$OUT" | sed -n 's/^events=//p' | tr -d ' \r')
|
|
|
|
if echo "$CODE_LINE" | grep -q 'код=0'; then
|
|
log "восстановление: ок | пользователей ${USERS:-?}, событий ${EVENTS:-?}"
|
|
kuma "$TOKEN_RESTORE" "matrix-restore-ok"
|
|
else
|
|
log "восстановление: ОШИБКА ($CODE_LINE)"
|
|
fi
|
|
|
|
# сверяем с живой базой, исключая ботов
|
|
LIVE=$(pct exec 151 -- bash -c "su - postgres -c \"psql -d matrix_db -tAc 'select count(*) from users'\"" 2>/dev/null | tr -d ' \r')
|
|
if [ -n "$LIVE" ] && [ -n "$USERS" ]; then
|
|
DIFF=$((LIVE - USERS))
|
|
if [ "$DIFF" -gt 0 ] && [ "$DIFF" -le 12 ]; then
|
|
log "разница с живой: ${DIFF} аккаунт(ов) — это аккаунты ботов, норма"
|
|
elif [ "$DIFF" -gt 12 ]; then
|
|
log "ВНИМАНИЕ: разница с живой ${DIFF} аккаунтов — проверить"
|
|
fi
|
|
fi
|
|
pct exec 151 -- bash -c "su - postgres -c 'psql -c \"drop database if exists matrix_restore_test\"'" >/dev/null 2>&1
|
|
pct exec 151 -- rm -f /tmp/mx-restore.sql.gz /root/.mx-restore.sh >/dev/null 2>&1
|
|
fi
|
|
|
|
log "готово"
|