From b9f2e7af736b5a3a24f5e582093e5186f7f6678a Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Sun, 10 Feb 2019 13:00:13 +0100 Subject: [PATCH] nextcloud: Add nc-inotify-sync --- nextcloud/nc-inotify-sync | 101 ++++++++++++++++++++++++++++++++++++++ nextcloud/nextcloud-sync | 6 +++ 2 files changed, 107 insertions(+) create mode 100755 nextcloud/nc-inotify-sync create mode 100644 nextcloud/nextcloud-sync diff --git a/nextcloud/nc-inotify-sync b/nextcloud/nc-inotify-sync new file mode 100755 index 0000000..e310352 --- /dev/null +++ b/nextcloud/nc-inotify-sync @@ -0,0 +1,101 @@ +#!/bin/sh + +NCC=/usr/bin/nextcloudcmd +if ! [ -x ${NCC} ]; then + echo "There is no ${NCC}." + exit 2 +fi + +IWAIT=/usr/bin/inotifywait +if ! [ -x ${IWAIT} ]; then + echo "There is no ${IWAIT}." + exit 2 +fi + +usage() { + cat << EOF + +Calls nextcloudcmd to sync a source directory and server url. The script +triggers a synchronization when (i) the source directory was changed or (ii) +after some period, whatever happens first. The nextcloudcmd is called with +netrc for login. + +Usage: + $0 [OPTIONS] source_dir server_url + $0 -h + +OPTIONS: + -h Show this text + -o One-shot instead of looping + -s Silent, be less verbose + -t SECS Sync period if nothing changed locally. + Default: 180 + +EXAMPLE + $0 ~/.vimwiki https://cloud.example.com/remote.php/webdav/vimwiki +EOF +} + +NCC_OPTS="-n" +IWAIT_OPTS="" + +SILENT=0 +ONESHOT=0 +PERIOD=180 + +while getopts "host:" OPTION; do + case "${OPTION}" in + h) + usage + exit + ;; + o) + ONESHOT=1 + ;; + s) + SILENT=1 + NCC_OPTS="${NCC_OPTS} -s" + IWAIT_OPTS="${IWAIT_OPTS} -q" + ;; + t) + PERIOD=${OPTARG} + ;; + *) + usage + exit 1 + ;; + esac +done +shift $((OPTIND-1)) + +SOURCE_DIR=$1 +SERVER_URL=$2 + +if [ -z "${SOURCE_DIR}" ] ; then + echo "No source directory given." + usage + exit 1 +fi + +if [ -z "${SERVER_URL}" ] ; then + echo "No server URL given." + usage + exit 1 +fi + +NCC_OPTS="${NCC_OPTS} ${SOURCE_DIR} ${SERVER_URL}" + +while true; do + echo "About to sync..." + sleep 2 + [ "$SILENT" = "1" ] || echo "[$(date '+%F %X')] Perform sync..." + + ${NCC} ${NCC_OPTS} + + [ "${ONESHOT}" = "1" ] && break + + sleep 3 + [ "$SILENT" = "1" ] || echo "" + [ "$SILENT" = "1" ] || echo "[$(date '+%F %X')] Wait for ${PERIOD} secs or source directory change." + ${IWAIT} ${IWAIT_OPTS} -t ${PERIOD} -r -e modify -e move -e create -e delete ${SOURCE_DIR} +done diff --git a/nextcloud/nextcloud-sync b/nextcloud/nextcloud-sync new file mode 100644 index 0000000..84eacea --- /dev/null +++ b/nextcloud/nextcloud-sync @@ -0,0 +1,6 @@ +#!/bin/sh + +# This script can be fired in crontab; it will launch a tmux sync session when none exists. + +ncc=~/.local/bin/nc-inotify-sync +tmux has-session -t nc-vimwiki || tmux new-session -d -s nc-vimwiki "${ncc} ~/.vimwiki https://cloud.example.com/remote.php/webdav/vimwiki" -- 2.30.2