Created
March 28, 2019 12:00
-
-
Save mvtango/9cbe7d9dff9f5b35669928820a14b7c2 to your computer and use it in GitHub Desktop.
bash service: sync on file change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Improvement: Use https://github.com/reduardo7/bash-service-manager | |
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ | |
# -o pipefail fail immediately if error in pipe | |
# -e fail immediately on error | |
# -E subshells inherit ERR handler | |
# -u Error on unset variables | |
# -x Echo all lines to STDERR | |
# -k get assignments | |
set -Eeuo pipefail | |
if [ "${DEBUG:-}" != "" ] ; then | |
set -x | |
fi | |
export myname=$(basename $0) | |
export serviceName="Credentials Sync" | |
export workDir=$(dirname $0) | |
export PID_FILE_PATH="/tmp/${myname}.pid" | |
export LOG_FILE_PATH="/tmp/${myname}.log" | |
export LOG_ERROR_FILE_PATH="/tmp/${myname}.error.log" | |
. ~/projekte/scripts/services.sh | |
SYNC_HERE=/home/martin/.credentials_encfs | |
SYNC_REMOTE=dropbox:/sync/martinvirtel/.credentials_encfs | |
TIME_FORMAT='%F %H:%M' | |
OUTPUT_FORMAT='%T Event(s): %e fired for file: %w. Refreshing.' | |
function do_copy() { | |
rclone -v copy $SYNC_HERE $SYNC_REMOTE | |
} | |
#Events: | |
# access file or directory contents were read | |
# modify file or directory contents were written | |
# attrib file or directory attributes changed | |
# close_write file or directory closed, after being opened in | |
# writable mode | |
# close_nowrite file or directory closed, after being opened in | |
# read-only mode | |
# close file or directory closed, regardless of read/write mode | |
# open file or directory opened | |
# moved_to file or directory moved to watched directory | |
# moved_from file or directory moved from watched directory | |
# move file or directory moved to or from watched directory | |
# create file or directory created within watched directory | |
# delete file or directory deleted within watched directory | |
# delete_self file or directory was deleted | |
# unmount file system containing file or directory unmounted | |
function forever() { | |
do_copy 2>&1 | |
while inotifywait -q -r -e modify,attrib,close_write,move,create,delete \ | |
--timefmt "${TIME_FORMAT}" --format "${OUTPUT_FORMAT}" "$SYNC_HERE"; do | |
do_copy 2>&1 | |
done | |
} | |
function run-service() { | |
forever | |
} | |
before-start() { | |
local action="$1" # Action | |
echo "* Starting with $action" | |
} | |
after-finish() { | |
local action="$1" # Action | |
local serviceExitCode=$2 # Service exit code | |
echo "* Finish with $action. Exit code: $serviceExitCode" | |
} | |
action="${1:-}" | |
serviceMenu "$action" "$serviceName" run-service "$workDir" before-start after-finish | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment