Commit 3f59f976 authored by Andrey Filippov's avatar Andrey Filippov

Add weekly PBX backup report

parent c9d9031d
BACKUP_MOUNT=/mnt/pbx-backup BACKUP_MOUNT=/mnt/pbx-backup
BACKUP_SUBDIR=elphel-pbx BACKUP_SUBDIR=elphel-pbx
KEEP_COUNT=30 KEEP_COUNT=30
# MAILTO=andrey@elphel.com # BACKUP_MAILTO=andrey@elphel.com
# REPORT_MAILTO=andrey@elphel.com
...@@ -13,7 +13,7 @@ Reason: ...@@ -13,7 +13,7 @@ Reason:
Replace the old Sunday restart mail with: Replace the old Sunday restart mail with:
- scheduled local backup to the USB stick - scheduled local backup to the USB stick
- optional short email report from the backup job - optional short weekly email report from the backup job
## USB ## USB
...@@ -44,29 +44,37 @@ The backup script is designed to capture the live state that is harder to recons ...@@ -44,29 +44,37 @@ The backup script is designed to capture the live state that is harder to recons
- systemd timer: daily at `03:17` - systemd timer: daily at `03:17`
- retention: keep the newest `30` runs by default - retention: keep the newest `30` runs by default
- optional mail: set `MAILTO` in `/etc/default/pbx-backup` - optional weekly mail: set `REPORT_MAILTO` in `/etc/default/pbx-backup`
- optional per-run mail: set `BACKUP_MAILTO` in `/etc/default/pbx-backup`
## Installed On Live PBX ## Installed On Live PBX
Installed on `192.168.1.16`: Installed on `192.168.1.16`:
- `/usr/local/sbin/pbx-backup.sh` - `/usr/local/sbin/pbx-backup.sh`
- `/usr/local/sbin/pbx-backup-report.sh`
- `/etc/default/pbx-backup` - `/etc/default/pbx-backup`
- `/etc/systemd/system/pbx-backup.service` - `/etc/systemd/system/pbx-backup.service`
- `/etc/systemd/system/pbx-backup.timer` - `/etc/systemd/system/pbx-backup.timer`
- `/etc/systemd/system/pbx-backup-report.service`
- `/etc/systemd/system/pbx-backup-report.timer`
- `/etc/fstab` entry for `/mnt/pbx-backup` - `/etc/fstab` entry for `/mnt/pbx-backup`
Verified on `2026-03-22`: Verified on `2026-03-22`:
- USB mounted at `/mnt/pbx-backup` - USB mounted at `/mnt/pbx-backup`
- timer active and scheduled for the next daily run - timer active and scheduled for the next daily run
- weekly report timer installed; mail delivery still depends on setting `REPORT_MAILTO`
- manual backup run `20260322_125605` completed successfully - manual backup run `20260322_125605` completed successfully
- resulting backup size: about `81M` - resulting backup size: about `81M`
## Repo Artifacts ## Repo Artifacts
- `scripts/pbx-backup.sh` - `scripts/pbx-backup.sh`
- `scripts/pbx-backup-report.sh`
- `config/pbx-backup.example` - `config/pbx-backup.example`
- `systemd/pbx-backup.service` - `systemd/pbx-backup.service`
- `systemd/pbx-backup.timer` - `systemd/pbx-backup.timer`
- `systemd/pbx-backup-report.service`
- `systemd/pbx-backup-report.timer`
## Notes ## Notes
......
#!/bin/bash
set -euo pipefail
umask 077
if [[ -r /etc/default/pbx-backup ]]; then
# shellcheck disable=SC1091
. /etc/default/pbx-backup
fi
BACKUP_MOUNT="${BACKUP_MOUNT:-/mnt/pbx-backup}"
BACKUP_SUBDIR="${BACKUP_SUBDIR:-$(hostname -s)}"
REPORT_MAILTO="${REPORT_MAILTO:-}"
HOSTNAME_SHORT="$(hostname -s)"
ROOT_DIR="${BACKUP_MOUNT}/${BACKUP_SUBDIR}"
if [[ -z "$REPORT_MAILTO" ]]; then
echo "REPORT_MAILTO is not configured; skipping weekly PBX backup report."
exit 0
fi
if ! command -v mail >/dev/null 2>&1; then
echo "mail command is not available" >&2
exit 1
fi
if ! mountpoint -q "$BACKUP_MOUNT"; then
mount "$BACKUP_MOUNT"
fi
LATEST_LINK="${ROOT_DIR}/latest"
if [[ ! -L "$LATEST_LINK" ]]; then
echo "Latest backup link ${LATEST_LINK} is missing" >&2
exit 1
fi
LATEST_DIR="$(readlink -f "$LATEST_LINK")"
RUN_NAME="$(basename "$LATEST_DIR")"
BACKUP_LOG="${LATEST_DIR}/backup.log"
MANIFEST="${LATEST_DIR}/manifest.tsv"
SHA256SUMS="${LATEST_DIR}/SHA256SUMS"
if [[ ! -d "$LATEST_DIR" || ! -f "$BACKUP_LOG" || ! -f "$MANIFEST" || ! -f "$SHA256SUMS" ]]; then
echo "Backup metadata is incomplete under ${LATEST_DIR}" >&2
exit 1
fi
BACKUP_SIZE="$(du -sh "$LATEST_DIR" | awk '{print $1}')"
BACKUP_TIME="$(date -d "${RUN_NAME:0:8} ${RUN_NAME:9:2}:${RUN_NAME:11:2}:${RUN_NAME:13:2}" '+%Y-%m-%d %H:%M:%S %Z' 2>/dev/null || echo "$RUN_NAME")"
LAST_LOG_LINE="$(tail -n 1 "$BACKUP_LOG")"
HASHSUM_STATUS="OK"
if ! (cd "$LATEST_DIR" && sha256sum -c SHA256SUMS >/dev/null 2>&1); then
HASHSUM_STATUS="FAILED"
fi
SUBJECT="[PBX weekly backup] ${HOSTNAME_SHORT} ${BACKUP_TIME}"
{
echo "PBX weekly backup report"
echo
echo "Host: ${HOSTNAME_SHORT}"
echo "Latest backup: ${RUN_NAME}"
echo "Backup time: ${BACKUP_TIME}"
echo "Backup location: ${LATEST_DIR}"
echo "Backup size: ${BACKUP_SIZE}"
echo "Integrity check: ${HASHSUM_STATUS}"
echo "Last log line: ${LAST_LOG_LINE}"
echo
echo "Included data:"
echo "- MySQL dumps: asterisk, asteriskcdrdb"
echo "- /etc/asterisk and /etc/freepbx.conf"
echo "- voicemail, call recordings, custom sounds, music-on-hold"
echo "- PBX host config: fail2ban, network, firewall whitelist, postfix"
echo
echo "Quick restore outline:"
echo "1. Reinstall Debian 12 and FreePBX/Asterisk on replacement hardware."
echo "2. Clone repo: git@git.elphel.com:Elphel/elphel-pbx.git"
echo "3. Mount USB stick at ${BACKUP_MOUNT}."
echo "4. Use the latest run under ${ROOT_DIR}."
echo "5. Restore MySQL from mysql-asterisk.sql.gz and mysql-asteriskcdrdb.sql.gz."
echo "6. Restore etc-asterisk.tgz, asterisk-state.tgz, and host-config.tgz."
echo "7. Reload FreePBX/Asterisk and retest inbound/outbound calls."
echo
echo "Key files in latest backup:"
sed 's/^/- /' "$MANIFEST"
echo
echo "Recent backup log:"
tail -n 20 "$BACKUP_LOG"
} | mail -s "$SUBJECT" "$REPORT_MAILTO"
...@@ -11,7 +11,7 @@ fi ...@@ -11,7 +11,7 @@ fi
BACKUP_MOUNT="${BACKUP_MOUNT:-/mnt/pbx-backup}" BACKUP_MOUNT="${BACKUP_MOUNT:-/mnt/pbx-backup}"
BACKUP_SUBDIR="${BACKUP_SUBDIR:-$(hostname -s)}" BACKUP_SUBDIR="${BACKUP_SUBDIR:-$(hostname -s)}"
KEEP_COUNT="${KEEP_COUNT:-30}" KEEP_COUNT="${KEEP_COUNT:-30}"
MAILTO="${MAILTO:-}" BACKUP_MAILTO="${BACKUP_MAILTO:-}"
HOSTNAME_SHORT="$(hostname -s)" HOSTNAME_SHORT="$(hostname -s)"
STAMP="$(date +%Y%m%d_%H%M%S)" STAMP="$(date +%Y%m%d_%H%M%S)"
ROOT_DIR="${BACKUP_MOUNT}/${BACKUP_SUBDIR}" ROOT_DIR="${BACKUP_MOUNT}/${BACKUP_SUBDIR}"
...@@ -27,10 +27,13 @@ send_report() { ...@@ -27,10 +27,13 @@ send_report() {
finish() { finish() {
local rc=$? local rc=$?
if [[ $rc -eq 0 ]]; then if [[ -n "$BACKUP_MAILTO" ]]; then
send_report "[PBX backup] ${HOSTNAME_SHORT} OK ${STAMP}" MAILTO="$BACKUP_MAILTO"
else if [[ $rc -eq 0 ]]; then
send_report "[PBX backup] ${HOSTNAME_SHORT} FAILED ${STAMP}" send_report "[PBX backup] ${HOSTNAME_SHORT} OK ${STAMP}"
else
send_report "[PBX backup] ${HOSTNAME_SHORT} FAILED ${STAMP}"
fi
fi fi
exit "$rc" exit "$rc"
} }
......
[Unit]
Description=Weekly PBX backup summary report
Wants=network-online.target
After=network-online.target local-fs.target
[Service]
Type=oneshot
User=root
ExecStart=/usr/local/sbin/pbx-backup-report.sh
[Unit]
Description=Weekly PBX backup summary report
[Timer]
OnCalendar=Sun *-*-* 07:15:00
RandomizedDelaySec=15m
Persistent=true
Unit=pbx-backup-report.service
[Install]
WantedBy=timers.target
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment