Citation
BibTEX
@misc { npapadopoulos_working_with_encrypted_file_archives,
author = "Nikolaos Papadopoulos",
title = "Working with encrypted file archives",
howpublished = "\url{https://www.4rknova.com/blog/2023/07/01/encrypted-archives}",
month = "07",
year = "2023",
}
IEEE
[1] N. Papadopoulos, "Working with encrypted file archives",
https://www.4rknova.com, 2023. [Online].
Available: \url{https://www.4rknova.com/blog/2023/07/01/encrypted-archives}.
[Accessed: 01-03-2025].
There are many use cases for creating encrypted file archives. One such use case is managing file backups. In this short blog post we look into how to use tar and gpg to achieve that.
Creating the archive
Generating an encrypted archive is fairly trivial:
$ tar -cvzf - directory | gpg -c > directory.tar.gz.gpg
In the above snippet, tar creates a compressed archive of the directory specified, using gzip, and pipes the data to gpg for encryption, before writing to the output file. The ‘-c’ flag instructs gpg to create a prompt for the user to enter a passphrase that will serve as the encryption key.
Alternatively, the passphrase can be inlined as shown below:
$ tar -cvzf - directory | gpg -c --passphrase a_passphrase > directory.tar.gz.gpg
The ’–passphrase’ switch will suppress the prompt and will use the next argument as the passphrase.
Extracting the archive
It’s equally simple to decrypt and decompress the encrypted file as shown below.
$ gpg -d directory.tar.gz.gpg | tar -xvzf -