From fdc9e804ce1db3f5c4fd39da153d430fa7736737 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 2 Apr 2024 16:30:53 +0200 Subject: [PATCH] new: [vps] add ``check backup`` command --- bin/vps | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/bin/vps b/bin/vps index d8f9a4a..504f385 100755 --- a/bin/vps +++ b/bin/vps @@ -2615,4 +2615,73 @@ cmdline.spec:monujo:cmd:set-version:run() { } + +cmdline.spec::cmd:check:run() { + : +} + +cmdline.spec.gnu check +cmdline.spec:check:cmd:backup:run() { + + : :optfla: --notify,-n "Send result through notify" + : :optval: --service,-s "The backup service name (defaults to 'rsync-backup')" + + + ## Check on daily bases if backup exist in config and when is the last backup done : + ## ALERT if backup is set and last backup is older than 24h + + local STATE_FILE="/var/run/myc-manage/backup.state" + mkdir -p "${STATE_FILE%/*}" + + service=${opt_service:-rsync-backup} + project_name=$(compose:project_name) || exit 1 + + ## check if service exists in compose.yml + if ! compose:service:exists "$project_name" "$service"; then + warn "no service ${DARKYELLOW}$service${NORMAL}. Ignoring." + return 0 + fi + + last_backup_datetime=$( + cat /srv/datastore/data/cron/var/log/cron/*rsync-backup_script{_*,}.log | grep "total size is" | sort | tail -n 1 | cut -f -2 -d " ") + last_backup_ts=$(date -d "$last_backup_datetime" +%s) + max_ts=$(date -d "24 hours ago" +%s) + + state="ok" + if [ "$last_backup_ts" -lt "$max_ts" ]; then + state="ko" + fi + + if [ -z "$opt_notify" ]; then + if [ "$state" == "ok" ]; then + info "Everything is ${GREEN}ok${NORMAL}. (last backup: ${DARKCYAN}$last_backup_datetime${NORMAL})" + return 0 + fi + warn "Last backup older than 1 day. (last backup: ${DARKCYAN}$last_backup_datetime${NORMAL})" + return 1 + fi + + ## notify + + last_state=$(cat "$STATE_FILE" 2>/dev/null) || true + if [ "$state" == "$last_state" ]; then + [ "$state" == "ko" ] || return 0 + is_old=$(find "$STATE_FILE" -type f -mtime +2) || return 1 + [ -n "$is_old" ] || return 0 + fi + + echo "$state" > "$STATE_FILE" + + message="[$(hostname)]: WARNING no backup done in the last 24h (No backup since $days days and $hours hours)" + + timestamp=$(date +%s) + time_difference=$((timestamp - last_backup_ts)) + days=$((time_difference / 86400)) + hours=$((time_difference % 86400 / 3600)) + message="WARNING: no backup done in the last 24h (No backup since $days days and $hours hours)" + send -t "ALERT Backup" "$message" + +} + + cmdline::parse "$@"