Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active May 14, 2024 10:01
Show Gist options
  • Select an option

  • Save slavafomin/7114c76a55a2d89e4119a56aa52cd484 to your computer and use it in GitHub Desktop.

Select an option

Save slavafomin/7114c76a55a2d89e4119a56aa52cd484 to your computer and use it in GitHub Desktop.

Revisions

  1. slavafomin revised this gist May 14, 2024. 2 changed files with 54 additions and 0 deletions.
    1 change: 1 addition & 0 deletions gpg-encryption.md → 0-gpg-encryption.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    # GPG Encryption

    ## List Keys

    53 changes: 53 additions & 0 deletions 1-backup-script.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env bash

    # This is an example of a backup script that
    # creates encrypted archive of a JavaScript project.

    # The "vars" file should be created alongside
    # this script with the following vars:
    #
    # BACKUP_PATH="/home/username/backup.tgz.gpg" # path to the destination archive
    # GPG_KEY="12345" # recipient's GPG key

    # Decrypt it with:
    # gpg -d filename.gpg > filename.tgz

    set -e
    set -o pipefail

    SCRIPT_PATH="$(dirname "$0")"
    VARS_PATH=$(realpath "${SCRIPT_PATH}/vars")

    if [ ! -f "$VARS_PATH" ]; then
    echo -e "Missing vars file at:\n${VARS_PATH}"
    echo -e "\nCreate it with \"cp ./bin/vars.dist ./bin/vars\""
    exit 1
    fi

    # shellcheck source=../vars
    source "${VARS_PATH}"

    if [ -z "${BACKUP_PATH-}" ]; then
    echo -e "Missing the BACKUP_PATH variable from:\n${VARS_PATH}"
    exit 1
    fi

    if [ -z "${GPG_KEY-}" ]; then
    echo -e "Missing the GPG_KEY variable from:\n${VARS_PATH}"
    exit 1
    fi

    echo -e "Creating archive at:\n${BACKUP_PATH}"

    echo -e "\nUsing GPG key:\n${GPG_KEY}"

    tar czf - \
    --exclude='.git' \
    --exclude='.idea' \
    --exclude='dist' \
    --exclude='node_modules' \
    --exclude='tmp' \
    . \
    | gpg -e -R "${GPG_KEY}" > "${BACKUP_PATH}"

    echo -e "\nBackup complete"
  2. slavafomin revised this gist May 14, 2024. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions gpg-encryption.md
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,10 @@ This decrypts it back:

    ```shell
    gpg -d filename.gpg > filename
    ```

    ## Acrhive & Encrypt

    ```shell
    tar czf - . | gpg -e > archive.gpg
    ```
  3. slavafomin created this gist May 14, 2024.
    35 changes: 35 additions & 0 deletions gpg-encryption.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@

    ## List Keys

    ```shell
    gpg --list-keys
    ```

    ## Import Key

    ```shell
    gpg --import private.key
    ```

    ## Config file

    ```shell
    # ~/.gnupg/gpg.conf:

    default-key {KEY-ID}
    default-recipient-self
    ```

    ## Encrypt/Decrypt File

    This encrypts the file using default key (default recipient):

    ```shell
    gpg -e filename
    ```

    This decrypts it back:

    ```shell
    gpg -d filename.gpg > filename
    ```