Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Last active August 1, 2026 22:49
Show Gist options
  • Select an option

  • Save wilyJ80/62ec457067a543f17dbe60f171d3a4fc to your computer and use it in GitHub Desktop.

Select an option

Save wilyJ80/62ec457067a543f17dbe60f171d3a4fc to your computer and use it in GitHub Desktop.
backup
#!/bin/bash
# Fix paths for crontab
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TARGET_DIR="/home/rubus/backups"
# Error handling
set -euo pipefail
trap cleanup ERR SIGINT SIGTERM
cleanup() {
local exit_code=$?
if [[ $exit_code -ne 0 ]]; then
date >> "$TARGET_DIR/errors.txt"
echo $exit_code >> "$TARGET_DIR/errors.txt"
fi
}
# Backup new file
ssh -i /home/rubus/.ssh/id_ed25519 citrus@orangepi3-lts.local "pg_dump -U citrus -d aq" > "$TARGET_DIR/aq_backup.tmp"
# Test temp file for content exists
[[ -s "$TARGET_DIR/aq_backup.tmp" ]]
# Rename to backup file only when valid
mv "$TARGET_DIR/aq_backup.tmp" "$TARGET_DIR/aq_backup_$(date +%Y%m%d%H%M%S).sql"
# If files > 8, remove the oldest file
count=$(find $TARGET_DIR -type f | grep backup | wc -l)
if [[ "$count" -gt 8 ]]; then
oldest=$(find $TARGET_DIR -type f | grep backup | sort | head -n1)
rm "${oldest}"
echo "[INFO] Removed oldest: ${oldest}"
fi
@wilyJ80

wilyJ80 commented Aug 1, 2026

Copy link
Copy Markdown
Author

0 3 * * * /bin/bash /home/rubus/backups/script.sh > /dev/null 2>&1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment