Skip to content

Instantly share code, notes, and snippets.

@SpareSimian
Created January 10, 2025 23:14
Show Gist options
  • Save SpareSimian/ce1a7378517cfccfdddce3fe45ea4126 to your computer and use it in GitHub Desktop.
Save SpareSimian/ce1a7378517cfccfdddce3fe45ea4126 to your computer and use it in GitHub Desktop.
Tar, compress, and encrypt a directory hierarchy
# commands used to encrypt a filesystem image
# create the encryption key (256 bits in hex)
openssl rand -hex 32 > /root/aes_key.txt
# move to top of desired filesystem to backup and encrypt
cd /mnt/root-to-back-up
# Tar using xz compression and pipe to AES256 encryption
tar cvfJ - . | openssl enc -aes-256-cbc -e -pass file:/root/aes_key.txt -out /mnt/USB-thumb-drive/encrypted-backup.tar.xz
# verify against source, decrypting and using tar --compare
openssl enc -aes-256-cbc -d -pass file:/root/aes_key.txt -in /mnt/USB-thumb-drive/encrypted-backup.tar.xz | tar dJfv -
# now ship the thumb drive and share the key by secure means
@SpareSimian
Copy link
Author

For saving to a thumb drive, remember that FAT32 only supports files up to 4 GB. You might want to reformat it to XFS, which handles large files better than EXT4. Add a label to make the partition easy to find in /dev/disk/by-label. You can use gparted for easy GUI reformatting and labeling, or mkfs from the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment