Skip to content

yairemartinez/bash-audit-backup-script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Audit & Backup Script

A simple Bash utility that audits a target directory for large or sensitive files, logs the findings, and creates a timestamped archive. Designed for quick system hygiene checks or small-scale backup tasks.

Features

  • Audit files

    • Logs the top 5 largest files in the directory.
    • Searches for large, log, or key files (excludes any matching DEBUG to avoid noisy debug logs).
  • Archiving

    • Creates a compressed .tar.gz archive of the target folder.
    • Automatically timestamps the archive name.
    • Excludes .bin files from the backup.
  • System summary

    • Records disk usage of the target folder.
    • Captures overall disk space available on the system.
  • Status feedback

    • Color-coded output for easy readability.
    • Clear success/failure messages for each step.
  • Note
    Archiving Behavior

By default, the script preserves the full path of the target directory inside the archive:

tar --exclude='*.bin' -czf "archive/$(basename "$target_dir")-$timestamp.tar.gz" "$target_dir" Optional: Archive Without Full Path

If you prefer to store only the folder name and its contents (without full system paths), you can comment out the default line and use this alternative: tar --exclude='*.bin' -czf "archive/$(basename "$target_dir")-$timestamp.tar.gz"
-C "$(dirname "$target_dir")" "$(basename "$target_dir")"

Usage ./audit_backup.sh target_directory

Example: ./audit_backup.sh /var/www

Output All results are saved under the logs/ and archive/ directories:

logs/largest_files.txt → Top 5 largest files

logs/findings.txt → Sensitive or large files (with DEBUG excluded)

logs/summary.txt → Disk usage summary

archive/target-YYYY-MM-DD-HHMM.tar.gz → Timestamped archive

Example Run [INFO] Scanning folder: /var/www [INFO] Logging top 5 largest files... [SUCCESS] Top 5 largest files saved to logs/largest_files.txt [INFO] Searching for large, log, or key files (excluding DEBUG)... [SUCCESS] Filtered file results saved to logs/findings.txt [INFO] Creating archive (excluding .bin)... [DONE] Archive created: archive/www-2025-08-19-2230.tar.gz [SUCCESS] Summary saved to logs/summary.txt All tasks completed successfully. Requirements Linux / Unix environment

bash, tar, du, df, grep, find

Notes Intended for test and learning environments.

Not optimized for production-scale backup.

Use with caution on sensitive directories.

Script auto-creates logs/ and archive/ if missing.

Tested on Ubuntu (bash 5+). Should work on most Linux distributions.

Test Environment (scan_targets/)

For demonstration purposes, the repository includes a scan_targets/ folder used to test the script. It contains sample files to illustrate how audit_backup.sh works:

  • debug.log → Simulated debug log file
  • error.log → Simulated error log file
  • secret.key → Dummy key file
  • junkfile.bin → 5MB random file generated with:
    head -c 5M /dev/urandom > junkfile.bin

About

“Bash script to audit directories and create timestamped backups, includes test environment”.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages