Skip to content

Instantly share code, notes, and snippets.

@breard-r
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save breard-r/208e3309425f899183e2 to your computer and use it in GitHub Desktop.

Select an option

Save breard-r/208e3309425f899183e2 to your computer and use it in GitHub Desktop.
Keeps files along with their GPG-encrypted copy.
# Ignore everything except .gpg files
# Useful when you want to sync encrypted files only.
*
!*.gpg
!*/
#!/bin/sh
## Copyright (c) 2015 Rodolphe Breard
##
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
##
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
##
set -e
GPG_ID_FILE=".gpg-id"
test -f "$GPG_ID_FILE" || (echo "$GPG_ID_FILE: No such file" && exit 2)
KEY_ID=$(cat "$GPG_ID_FILE")
DIR="."
test "$1" != "" && DIR="$1"
gpg_encrypt_file () {
FILENAME="$1"
GPG_FILENAME="$FILENAME.gpg"
if [ ! -f "$GPG_FILENAME" ]; then
echo "Encrypting $FILENAME...";
gpg --quiet -r "$KEY_ID" --output "$GPG_FILENAME" --encrypt --no-encrypt-to "$FILENAME"
fi
}
gpg_decrypt_file () {
GPG_FILENAME="$1"
FILENAME="$(dirname "$GPG_FILENAME")/$(basename "$GPG_FILENAME" ".gpg")"
if [ ! -f "$FILENAME" ]; then
echo "Decrypting $FILENAME...";
gpg --quiet -r "$KEY_ID" --output "$FILENAME" --decrypt "$GPG_FILENAME"
fi
}
gpg_encrypt_dir () {
find "$1" -not -path '*/\.*' -not -name '*~' -not -name "$(basename $0)" -not -name "*.gpg" -type f -print | while read file; do
gpg_encrypt_file "$file";
done
}
gpg_decrypt_dir () {
find "$1" -not -path '*/\.*' -not -name '*~' -not -name "$(basename $0)" -name "*.gpg" -type f -print | while read file; do
gpg_decrypt_file "$file";
done
}
sync_dir () {
gpg_encrypt_dir "$1"
gpg_decrypt_dir "$1"
}
sync_dir "$DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment