Skip to content

Instantly share code, notes, and snippets.

@Serranya
Created October 14, 2015 19:05
Show Gist options
  • Save Serranya/a19de6e45175fe0b8d10 to your computer and use it in GitHub Desktop.
Save Serranya/a19de6e45175fe0b8d10 to your computer and use it in GitHub Desktop.
#! /bin/bash
ECD=5
RECIPIENT=<REDACTED>
SRC=${1%/}
DST=${2%/}
# $1 folder to index
# $2 index file
function create_idx {
touch "$2"
exec 3<> "$2"
cd "$1" || exit $ECD
find . -type f -exec sha256sum "{}" \; >&3
exec 3>&-
cd - || exit $ECD
sort -o "$2" "$2"
}
echo "Backing up and encrypting directories in \"$SRC\" to \"$DST\""
mkdir -p "$DST/$(basename "$SRC")"
while IFS= read -r -d '' DIR
do
IDX="$DST/$(basename "$(dirname "$DIR")")/.$(basename "$DIR").idx"
IDX2=$(mktemp -q)
if [ ! -f "$IDX" ]; then
echo "Creating index file: $IDX"
create_idx "$DIR" "$IDX"
tar -v -C "$SRC" -c "$(basename "$DIR")" | gpg --compress-level 0 \
--output "$DST/$(basename "$SRC")/$(basename "$DIR").tar.gpg" \
--encrypt --recipient "$RECIPIENT"
else
create_idx "$DIR" "$IDX2"
if cmp -s "$IDX" "$IDX2"; then
echo "No changes"
else
echo "Changes detected"
diff "$IDX" "$IDX2"
cp "$IDX2" "$IDX"
tar -C "$SRC" -c "$(basename "$DIR")" | gpg --compress-level 0 --batch --yes \
--output "$DST/$(basename "$SRC")/$(basename "$DIR").tar.gpg" \
--encrypt --recipient "$RECIPIENT"
fi
rm "$IDX2"
fi
done < <(find "$SRC" -maxdepth 1 -mindepth 1 -type d -not -iname '.*' -print0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment