Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active February 18, 2021 02:15
Show Gist options
  • Save turboBasic/ee2f1bc2737786a5b6ec14fe800108f5 to your computer and use it in GitHub Desktop.
Save turboBasic/ee2f1bc2737786a5b6ec14fe800108f5 to your computer and use it in GitHub Desktop.
Backup all secret and public keys from GnuPG keyring #gpg

Backup private & public GnuPG keys

Backup

KEY_ID='i@example.com'

cat <<EOF >"${KEY_ID}...pub+sec.asc" \ 
&&  gpg --output "${KEY_ID}...pub.gpg" \
        --export ${KEY_ID} \
&&  gpg --output - \
        --export-secret-keys \
        --export-options export-backup \
        ${KEY_ID} \
|   cat "${KEY_ID}...pub.gpg" - \
|   gpg --armor \
        --symmetric \
        --cipher-algo aes256 \
>>  "${KEY_ID}...pub+sec.asc"

uid:    ${KEY_ID}
keys:   public + secret
cipher: aes256

EOF

Result

$ ls
i@example.com...pub+sec.asc  i@example.com...pub.gpg

Restore

Restore on different device

gpg --output - --decrypt "i@example.com...pub+sec.asc"  \
|   gpg --import

Restore in different GnuPG home dir on the same device

Execute

gpg --output - --decrypt "i@example.com...pub+sec.asc" \
|   gpg --homedir /path/to/newGpgHome --import

Unload GnuPG Agent in case of gpg: error sending to agent / gpg: error reading '[stdin]' errors:

gpg-connect-agent --homedir /path/to/newGpgHome killagent /bye

Related materials


tags: gpg, gnupg

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