Last active
August 1, 2026 22:49
-
-
Save wilyJ80/62ec457067a543f17dbe60f171d3a4fc to your computer and use it in GitHub Desktop.
backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
0 3 * * * /bin/bash /home/rubus/backups/script.sh > /dev/null 2>&1