Skip to content

Instantly share code, notes, and snippets.

@greyltc
Last active December 25, 2016 18:59
Show Gist options
  • Save greyltc/a924194847418dff9aa58d159a6ec717 to your computer and use it in GitHub Desktop.
Save greyltc/a924194847418dff9aa58d159a6ec717 to your computer and use it in GitHub Desktop.
encrypted backup to google drive
#!/usr/bin/env bash
# my backup script
# always requires three arguments:
# $1 = "upload" or "recover"
# $2 = encryption key
# $3 = target directory
# ===upload mode===
# Requires ~/.drive folder that das already been setup
# target directory is an absolute path to a folder to be encrypted and uploaded
# Puts backup files into a folder #encfs/targetName in google drive
# ===recover mode===
# target directory is an absolute path to a folder containing encrypted files
# .encfs6.xml (or _encfs6.xml) must be in the top level
# decrypted files will appear in /tmp/decrypted
set -eu -o pipefail
# requires: drive, encfs
# pacaur -Syyu drive encfs
# uncomment this to compute and compare all checksums
#IGNORE_CHECKSUM="-ignore-checksum=false"
IGNORE_CHECKSUM=""
ENCRYPTION_KEY=$2
TARGET="$3"
if [ ! -d "${TARGET}" ]; then
echo "Error: ${TARGET} does not exist."
exit -1
fi
TARGET_BASE=$(basename $TARGET)
upload(){
VIRTUAL_MOUNT="/tmp/encrypted"
DRIVE_FOLDER="${HOME}/.drive"
if [ ! -d "$DRIVE_FOLDER/.gd" ]; then
echo "Error: $DRIVE_FOLDER/.gd does not exist."
exit -1
fi
DRIVE_TARGET="${DRIVE_FOLDER}/#encfs/${TARGET_BASE}"
if [ -a "$DRIVE_TARGET" ]; then
echo "Error: $DRIVE_TARGET already exists."
exit -1
fi
mkdir "${VIRTUAL_MOUNT}"
echo -n "" | \
encfs --extpass="echo $ENCRYPTION_KEY" --reverse -o ro "${TARGET}" "${VIRTUAL_MOUNT}"
echo "Here is the encfs config being used here:"
echo "================="
cat "${TARGET}/.encfs6.xml"
echo "================="
echo "You can also find it here: ${TARGET}/.encfs6.xml"
mkdir -p "${DRIVE_FOLDER}/#encfs"
cd "$DRIVE_FOLDER"
cp "${TARGET}/.encfs6.xml" "#encfs/${TARGET_BASE}_encfs6.xml"
drive push -directories -no-prompt "#encfs"
drive push -files -ignore-checksum=false -no-prompt "#encfs"
ln -s "${VIRTUAL_MOUNT}" "${DRIVE_TARGET}"
drive push ${IGNORE_CHECKSUM} -no-prompt -quiet "#encfs/${TARGET_BASE}"
drive cp "#encfs/${TARGET_BASE}_encfs6.xml" "#encfs/${TARGET_BASE}/_encfs6.xml"
fusermount -u "${VIRTUAL_MOUNT}" && rm -rf "${VIRTUAL_MOUNT}" && rm -rf "${HOME}/.drive/#encfs" && echo "Great success!"
}
recover(){
VIRTUAL_MOUNT="/tmp/decrypted"
ENCFS6_CONFIG="${TARGET}/.encfs6.xml"
cp "${TARGET}/_encfs6.xml" "${ENCFS6_CONFIG}" &>/dev/null || true
if [ -f "${ENCFS6_CONFIG}" ]; then
mkdir ${VIRTUAL_MOUNT}
encfs --extpass="echo $ENCRYPTION_KEY" -o ro "${TARGET}" "${VIRTUAL_MOUNT}"
else
echo "Error: ${ENCFS6_CONFIG} not found."
exit -1
fi
echo "Unencrypted data now available readonly at ${VIRTUAL_MOUNT}"
echo "When you are finished with it, clean up with:"
echo "fusermount -u ${VIRTUAL_MOUNT} && rm -rf ${VIRTUAL_MOUNT}"
}
$1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment