backup.sh 1.1 KB

1234567891011121314151617181920
  1. #!/usr/bin/env bash
  2. # Daily backup of the LYNX SHARE service data (run by lynx-backup.timer as root).
  3. # Keeps LYNX_BACKUP_KEEP (default 14) daily archives in /var/backups/lynx (chmod 700, archives 600).
  4. # Share blobs (/var/lib/lynx/shares/*/blob) are NOT backed up: they are temporary (they expire in days), can be up to
  5. # 60 GB in total and the host re-uploads a link that was lost. Their metadata is backed up.
  6. set -euo pipefail
  7. DEST=/var/backups/lynx
  8. KEEP=${LYNX_BACKUP_KEEP:-14}
  9. install -d -m 700 "$DEST"
  10. STAMP=$(date -u +%Y%m%d-%H%M)
  11. OUT="$DEST/lynx-$STAMP.tar.gz"
  12. umask 077
  13. paths=()
  14. for p in /etc/lynx /etc/caddy /var/lib/caddy /var/lib/lynx; do [ -e "$p" ] && paths+=("$p"); done
  15. tar --create --gzip --file "$OUT.part" --warning=no-file-changed --exclude='/var/lib/lynx/shares/*/blob' \
  16. --exclude='*.tmp' "${paths[@]}" 2>/dev/null || [ $? -eq 1 ]
  17. mv "$OUT.part" "$OUT"
  18. # rotation: newest $KEEP archives stay
  19. ls -1t "$DEST"/lynx-*.tar.gz 2>/dev/null | tail -n +"$((KEEP + 1))" | xargs -r rm -f
  20. echo "$(date -u +%FT%TZ) backup $OUT $(stat -c %s "$OUT") bytes, $(ls -1 "$DEST"/lynx-*.tar.gz | wc -l) kept" >> /var/log/lynx/backup.log