← Back to Blog
Code Snippets July 29, 2026

Archive Files with Progress Bar, Exclusions & Integrity Check (Bash)

💻 Code Snippet of the Day — July 29, 2026

Archive Files with Progress Bar, Exclusions & Integrity Check (Bash)

Vanilla tar gives you no progress feedback, no integrity verification, and leaves partial files on failure. This safe_archive function adds all of that — zero dependencies, works on Linux/macOS/WSL.

Features:
  • Live progress bar (--checkpoint-action=dot)

  • --dry-run to preview before committing

  • --exclude (repeatable) and --exclude-file (.tarignore style)

  • --max-size guard against disk fill

  • Post-creation integrity check (tar -tzf)

  • Auto-cleanup on Ctrl-C/failure

  • Color output auto-disables in pipes/CI


safe_archive /project /backup.tar.gz
safe_archive -e '*.tmp' -e 'cache/' /project /backup.tar.gz
safe_archive --exclude-file .tarignore /site /site-backup.tar.gz
safe_archive -n /opt/app /dev/null   # dry-run preview
safe_archive -m 2G -q /data /data.tar.gz


Drop in ~/.bashrc or ship as standalone script. Full script is 197 lines — the core: tar with checkpoint progress, a cleanup trap on ERR|INT|TERM, post-creation tar -tzf validation, human-readable size reporting, and a size-limit guard that aborts and deletes the archive if it exceeds --max-size.

💡 The hidden gem is --dry-run: it prints exactly what would be archived without creating any file, so you can verify exclude patterns before committing. Essential for production backups.