Advertisement
PlebCoder1337

Untitled

Jun 9th, 2025 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 0.86 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # backup.sh  –  Full tar.gz backup of ~/backup_demo/source
  4. #   1. Creates timestamped archive in ~/backup_demo/archives
  5. #   2. Keeps only the latest 7 archives (rotate)
  6. #
  7. set -e                          # Exit on first error
  8.  
  9. SRC_DIR="$HOME/backup_demo/source"
  10. DST_DIR="$HOME/backup_demo/archives"
  11.  
  12. # Create destination if missing
  13. mkdir -p "$DST_DIR"
  14.  
  15. # Build archive name: backup-YYYYMMDD-HHMMSS.tar.gz
  16. STAMP=$(date +'%Y%m%d-%H%M%S')
  17. ARCHIVE="$DST_DIR/backup-${STAMP}.tar.gz"
  18.  
  19. # -c = create    -z = gzip    -f = file name
  20. tar -czf "$ARCHIVE" -C "$SRC_DIR" .    # -C changes dir before adding “.”
  21.  
  22. echo "[+] Created $ARCHIVE"
  23.  
  24. # -------- Rotation: keep only latest 7 archives --------
  25. # List files sorted by newest first, skip first 7, delete the rest
  26. ls -1t "$DST_DIR"/backup-*.tar.gz | tail -n +8 | xargs -r rm -v
  27.  
  28. exit 0
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement