Skip to content

Instantly share code, notes, and snippets.

@mschubert
Last active May 4, 2016 04:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mschubert/9281209 to your computer and use it in GitHub Desktop.
Save mschubert/9281209 to your computer and use it in GitHub Desktop.
Script to (decrypt encfs and) selectively sync a remote sshfs directory
#!/bin/bash
# Script to (decrypt encfs and) selectively sync a remote sshfs directory
REMOTEDIR=server:/path/to/some/encfs
LOCALDIR=/path/to/local/directory
MOUNTFUNC=_ssh+encfs
# verbose
# set -vx
# exit on error
set -e
which sshfs >/dev/null || (echo "Error missing required software: sshfs" && exit 1)
which encfs >/dev/null || (echo "Error missing required software: encfs" && exit 1)
which fusermount >/dev/null || (echo "Error missing required software: fusermount" && exit 1)
test -d /dev/shm >/dev/null || (echo "Error missing required directory: /dev/shm" && exit 1)
[[ "$REMOTEDIR" == *:/* ]] || (echo "Error: \$REMOTEDIR is relative path" && exit 1)
[[ "$LOCALDIR" == /* ]] || (echo "Error: \$LOCALDIR is relative path" && exit 1)
abspath() {
cd "$(dirname "$1")"
printf "%s/%s\n" "$(pwd)" "$(basename "$1")"
}
_sshfs() {
if [[ $1 = "-u" ]]; then
fusermount -uz $2
else
sshfs -o idmap=user -o reconnect $1 $2
fi
}
_ssh+encfs() {
if [[ $1 = "-u" ]]; then
fusermount -uz $2
fusermount -uz /dev/shm/encfs_tmp
else
mkdir -p /dev/shm/encfs_tmp
sshfs -o idmap=user -o reconnect $1 /dev/shm/encfs_tmp
encfs /dev/shm/encfs_tmp $2
fi
}
case "$1" in
init)
# write remote files to .sync file
mkdir -p /dev/shm/encfs_decrypted
$MOUNTFUNC "$REMOTEDIR" /dev/shm/encfs_decrypted
rsync -rmn --list-only /dev/shm/encfs_decrypted/* "$LOCALDIR" | \
awk '{print $5}' > "$(dirname $0)/.sync"
$MOUNTFUNC -u /dev/shm/encfs_decrypted
echo "Added file list to .sync file, remove lines you don't need"
;;
sync)
# update file list in .sync
[[ ! -f "$(dirname $0)/.sync" ]] && echo "Need to './data.sh init' first" && exit 1
mkdir -p /dev/shm/encfs_decrypted
$MOUNTFUNC "$REMOTEDIR" /dev/shm/encfs_decrypted
rsync -auvrm --files-from="$(dirname $0)/.sync" /dev/shm/encfs_decrypted "$LOCALDIR"
$MOUNTFUNC -u /dev/shm/encfs_decrypted
;;
mount)
# mount remote directory to $2
$MOUNTFUNC "$REMOTEDIR" $(abspath $2)
;;
umount)
# unmount remote directory at $2
$MOUNTFUNC -u $(abspath $2)
;;
*)
echo "Usage: ./$(basename $0) <command>"
echo " init - write available files to .sync file"
echo " sync - update files in .sync from server"
echo " mount <dir> - mount the volume to directory <dir>"
echo " umount <dir> - unmount the volume at directory <dir>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment