brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: age encrypts a backup to a public key, so the backup job needs no password

Backup
~/tips/tip-age-encrypt-file-recipient

tar -czf - data | age -r "$PUB" -o data.tgz.age

Encrypts a stream to a recipient key, and only the matching key file can decrypt it.

Encrypting a backup with a passphrase means the passphrase has to live in the script that makes the backup. With age, the job only needs the public key. The secret half can stay somewhere else.

age-keygen -o key.txt
PUB=$(age-keygen -y key.txt)
tar -czf - data | age -r "$PUB" -o data.tgz.age

age-keygen -o writes the key pair to a file and prints the public key. -y turns the identity file into the public recipient line. age -r encrypts to that recipient. In a test the output file started with age-encryption.org/v1, and the key file had mode -rw-------.

To read the backup back, give age the identity file:

age -d -i key.txt data.tgz.age | tar -tzf -
data/
data/n.txt

The public key can sit on the server that runs the backups. Whoever gets hold of the encrypted file cannot read it without key.txt. The age help also lists -p for a passphrase and -a for a text format that you can paste into a mail or a note, and it says a recipient may be an SSH public key too. This was not run with an SSH key. It follows the help text, so check the age documentation before you rely on it.

Caveats: without key.txt the file cannot be decrypted, so the key is the one thing you must not lose. Keep a copy of the key file somewhere that is not the same disk as the backups, ideally in a password manager. Also, age -o says it overwrites an existing output file, so do not point it at a backup you want to keep.

Further reading: age, age on GitHub

More Backup tips restic snapshots --group-by host,paths mysqldump --routines --events mydb > mydb.sql rsync -a --files-from=list.txt /home/me/ backup:/backup/ restic find "*.txt" restic stats All Backup tips (46) All topics

Link