This Bash script automates creating and encrypting a backup archive. It tars (optionally compresses) the specified directory and encrypts the resulting archive with AES-256 via OpenSSL.
- Positional and Named Arguments
- Positional:
./backup.sh <DIRECTORY> [<COMPRESSION>] [<OUTPUT_FILE>] - Named:
./backup.sh -i <DIRECTORY> [-c <COMPRESSION>] [-o <OUTPUT_FILE>]
-
Compression: Available algorithms are
gzip,bzip2,xz. Defaults to none if not specified. In case of none, archiving works without compression. -
Encryption: Encrypts the archive using OpenSSL with
-aes-256-cbc -pbkdf2and produces a final file ending in .enc unless an explicit filename is provided. -
Error Logging: All error messages (stderr) are timestamped and logged in error.log. Warnings and errors—such as missing directories or invalid compression—go here.
-
Help Option:
-hor--helpavailable anywhere in the arguments to show usage instructions. -
Debug Mode: By default, DEBUG=1, so script variables are printed to the terminal. If you set DEBUG=0, all normal script output is suppressed, and only errors are logged to error.log.
You can run the script in two ways: positional or named arguments.
./backup.sh <DIRECTORY> [<COMPRESSION>] [<OUTPUT_FILE>]<DIRECTORY>: Required. Must be an existing directory.<COMPRESSION>: (Optional) One of none (default),gzip,bzip2,xz.<OUTPUT_FILE>: (Optional) The final name for the encrypted backup. Defaults to<DIRECTORY>.backup.enc.
1. Minimal (no compression, default output):
./backup.sh /var/www/htmlProduces html.backup.enc in the current directory.
2. Specify compression:
./backup.sh /home/user gzipTar + gzip compresses /home/user, then encrypts into user.backup.enc.
3. Fully specify:
./backup.sh /srv/data xz data_archive.tar.xzCreates data_archive.tar.xz (tar + xz), then encrypts into data_archive.tar.xz.
./backup.sh -i <DIRECTORY> [-c <COMPRESSION>] [-o <OUTPUT_FILE>]- -i / --input
<DIRECTORY>: Required directory path to back up. - -c / --compression
<COMPRESSION>: Optional. Defaults to none. - -o / --output
<OUTPUT_FILE>: Optional. Defaults to<DIRECTORY>.backup.encif omitted.
1. Minimal:
./backup.sh -i /var/logUses no compression; final file log.backup.enc.
2. Specify compression & output:
./backup.sh -i /var/log -c bzip2 -o logs_backup.tar.bz2Compresses with bzip2, then encrypts into logs_backup.tar.bz2.
3. Help:
./backup.sh -h
./backup.sh --helpShows usage instructions and exits.
The script has a DEBUG variable near the top:
DEBUG=1- DEBUG=0 (default, quiet mode): Normal output is redirected to
/dev/null; only errors and warnings show up in error.log. - DEBUG=1 (verbose mode): Prints internal variables (
BACKUP_DIR,COMPRESSION, etc.) to stdout (for troubleshooting).
To enable verbose mode (i.e., keep normal output visible and print debugging info about the script’s variables) add -v or --verbose parameter while running the script.
After running successfully, you end up with an encrypted file (e.g., mydata.backup.enc). If you specified an output name (like myarchive.tar.gz), that’s the file you’ll see (still encrypted). The unencrypted tar is removed automatically.
1. Decrypt:
openssl enc -d -aes-256-cbc -pbkdf2 -in <encrypted_backup> -out <decrypted_backup>You’ll be prompted for the passphrase used to create the backup.
2. Extract:
- If uncompressed (none), just:
tar -xf <decrypted_backup>- If compressed with gzip:
tar -xzf <decrypted_backup>- If compressed with bzip2:
tar -xjf <decrypted_backup>- If compressed with xz:
tar -xJf <decrypted_backup>(You can also do tar -xaf <decrypted_backup> and many modern tar versions will auto-detect the compression.)
Errors and warnings (e.g., invalid compression type, missing directory) are timestamped in error.log. If you ever need to see normal script messages while debugging, use verbose mode.
- Bash (and basic Unix utilities like
tar). - OpenSSL installed and accessible in
PATH.