Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- #
- # backup.sh – Full tar.gz backup of ~/backup_demo/source
- # 1. Creates timestamped archive in ~/backup_demo/archives
- # 2. Keeps only the latest 7 archives (rotate)
- #
- set -e # Exit on first error
- SRC_DIR="$HOME/backup_demo/source"
- DST_DIR="$HOME/backup_demo/archives"
- # Create destination if missing
- mkdir -p "$DST_DIR"
- # Build archive name: backup-YYYYMMDD-HHMMSS.tar.gz
- STAMP=$(date +'%Y%m%d-%H%M%S')
- ARCHIVE="$DST_DIR/backup-${STAMP}.tar.gz"
- # -c = create -z = gzip -f = file name
- tar -czf "$ARCHIVE" -C "$SRC_DIR" . # -C changes dir before adding “.”
- echo "[+] Created $ARCHIVE"
- # -------- Rotation: keep only latest 7 archives --------
- # List files sorted by newest first, skip first 7, delete the rest
- ls -1t "$DST_DIR"/backup-*.tar.gz | tail -n +8 | xargs -r rm -v
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement