A pair of bash scripts to securely backup and restore your SSH keys, GPG keys, and Git configuration with symmetric encryption.
This script creates an encrypted backup of the following:
- All SSH keypairs in
~/.ssh/starting withid_(e.g.,id_rsa,id_ed25519) - All GPG keys (private keys, public keys, and trust database)
- Your Git global configuration (
~/.gitconfig)
# Make the script executable
chmod +x backup-keys.sh
# Run the backup
./backup-keys.shYou'll be prompted to enter a passphrase for encryption. Choose a strong passphrase and remember it - you'll need it to restore your keys.
The script creates two items:
~/keys-backup/- Temporary directory containing unencrypted copies of your keys~/keys-backup-YYYYMMDD.tar.gz.gpg- Encrypted archive (safe to store anywhere)
This script restores the following from an encrypted backup:
- All SSH keypairs to
~/.ssh/with correct permissions - Adds SSH keys to
ssh-agent - All GPG keys to your GPG keyring
- Your Git global configuration to
~/.gitconfig
# Make the script executable
chmod +x restore-keys.sh
# Run the restore with the backup file as an argument
./restore-keys.sh keys-backup-YYYYMMDD.tar.gz.gpgYou'll be prompted to enter the passphrase you used during backup.
- Use a strong passphrase - Your backup is only as secure as your passphrase
- Keep your passphrase safe - It cannot be recovered if lost
- Delete unencrypted directories - Always remove
~/keys-backupafter creating/verifying the encrypted archive - Encrypted archives are safe - The
.tar.gz.gpgfiles can be safely stored in cloud storage or external drives - AES256 encryption - Industry-standard encryption protects your keys
- Download the encrypted backup to your new machine
- Download
restore-keys.shfrom this Gist - Run the restore script with your backup file
- Test your connections
- Remove the unencrypted temporary directory (
~/keys-backup)
- Download
backup-keys.shfrom this Gist - Run the backup script periodically (e.g., after generating new keys)
- Verify and store the encrypted archive
- Remove the unencrypted temporary directory (
~/keys-backup)