diff --git a/Dockerfile b/Dockerfile index 587a27f..f3f6c69 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,33 @@ -ARG BASE=alpine:3.9 +ARG BASE=alpine:latest FROM ${BASE} -LABEL maintainer="Brian J. Cardiff " +LABEL maintainer="pfidr" ARG RCLONE_VERSION=current ARG ARCH=amd64 ENV SYNC_SRC= ENV SYNC_DEST= ENV SYNC_OPTS=-v +ENV SYNC_OPTS_EVAL= +ENV SYNC_ONCE= +ENV RCLONE_CMD=sync +ENV RCLONE_DIR_CMD=ls +ENV RCLONE_DIR_CMD_DEPTH=-1 +ENV RCLONE_DIR_CHECK_SKIP= ENV RCLONE_OPTS="--config /config/rclone.conf" +ENV OUTPUT_LOG= +ENV ROTATE_LOG= ENV CRON= ENV CRON_ABORT= ENV FORCE_SYNC= ENV CHECK_URL= ENV FAIL_URL= +ENV HC_LOG= ENV TZ= -ENV COMMAND=sync +ENV UID= +ENV GID= -RUN apk -U add ca-certificates fuse wget dcron tzdata \ - && rm -rf /var/cache/apk/* +RUN apk --no-cache add ca-certificates fuse wget dcron tzdata RUN URL=http://downloads.rclone.org/${RCLONE_VERSION}/rclone-${RCLONE_VERSION}-linux-${ARCH}.zip ; \ URL=${URL/\/current/} ; \ @@ -33,6 +42,7 @@ COPY sync.sh / COPY sync-abort.sh / VOLUME ["/config"] +VOLUME ["/logs"] ENTRYPOINT ["/entrypoint.sh"] diff --git a/LICENSE.md b/LICENSE.md index 43dd8a7..2339a1d 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,3 +1,5 @@ +Copyright 2019 Pfidr34 + Copyright 2017 Brian J. Cardiff Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index add4bbd..2b29423 100644 --- a/README.md +++ b/README.md @@ -32,27 +32,138 @@ A first run of the container can help in the creation of the file, but feel free ``` $ mkdir config -$ docker run --rm -it -v $(pwd)/config:/config bcardiff/rclone +$ docker run --rm -it -v $(pwd)/config:/config pfidr/rclone ``` ### Perform sync in a daily basis -A few environment variables allow you to customize the behavior of the sync: +A few environment variables allow you to customize the behavior of rclone: -* `COMMAND` `rclone` command, e.g `sync`, `copy`, `move`. -Defaults to `sync` -* `SYNC_SRC` source location for `rclone sync` command -* `SYNC_DEST` destination location for `rclone sync` command -* `CRON` crontab schedule `0 0 * * *` to perform sync every midnight +* `SYNC_SRC` source location for `rclone sync/copy/move` command. Directories with spaces should be wrapped in single quotes. +* `SYNC_DEST` destination location for `rclone sync/copy/move` command. Directories with spaces should be wrapped in single quotes. +* `SYNC_OPTS` additional options for `rclone sync/copy/move` command. Defaults to `-v` +* `SYNC_OPTS_EVAL` further additional options for `rclone sync/copy/move` command. The variables and commands in the string are first interpolated like in a shell. The interpolated string is appended to SYNC_OPTS. That means '--backup-dir /old\`date -I\`' first evaluates to '--backup-dir /old2019-09-12', which is then appended to SYNC_OPTS. The evaluation happens immediately before rclone is called. +* `SYNC_ONCE` set variable to only run the sync one time and then exit the container +* `RCLONE_CMD` set variable to `sync` `copy` or `move` when running rclone. Defaults to `sync` +* `RCLONE_DIR_CMD` set variable to `ls` or `lsf` for source directory check style. Defaults to `ls` +* `RCLONE_DIR_CMD_DEPTH` set the limit of the recursion depth to this. Defaults to `-1` (rclone default) +* `RCLONE_DIR_CHECK_SKIP` set variable to skip source directory check before sync. *Use with caution* +* `CRON` crontab schedule `0 0 * * *` to perform sync every midnight. Also supprorts cron shortcuts: `@yearly` `@monthly` `@weekly` `@daily` `@hourly` * `CRON_ABORT` crontab schedule `0 6 * * *` to abort sync at 6am * `FORCE_SYNC` set variable to perform a sync upon boot * `CHECK_URL` [healthchecks.io](https://healthchecks.io) url or similar cron monitoring to perform a `GET` after a successful sync -* `FAIL_URL` Fail URL to perform a `GET` after unsuccessful execution. Default: `CHECK_URL` with appended "/fail" at the end -* `SYNC_OPTS` additional options for `rclone sync` command. Defaults to `-v` -* `TZ` set the [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) to use for the cron and log `America/Argentina/Buenos_Aires` +* `FAIL_URL` Fail URL to perform a `GET` after unsuccessful execution. By default this is `CHECK_URL` with appended "/fail" at the end +* `HC_LOG` set variable to send log data to healthchecks.io. `OUTPUT_LOG` must also be set. +* `OUTPUT_LOG` set variable to output log file to /logs +* `ROTATE_LOG` set variable to delete logs older than specified days from /logs +* `TZ` set the [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) to use for the cron and log `America/Chicago` +* `UID` set variable to specify user to run rclone as. Must also use GID. +* `GID` set variable to specify group to run rclone as. Must also use UID. + +**When using UID/GID the config and/or logs directory must be writeable by this UID** ```bash -$ docker run --rm -it -v $(pwd)/config:/config -v /path/to/source:/source -e SYNC_SRC="/source" -e SYNC_DEST="dest:path" -e TZ="America/Argentina/Buenos_Aires" -e CRON="0 0 * * *" -e CRON_ABORT="0 6 * * *" -e FORCE_SYNC=1 -e CHECK_URL=https://hchk.io/hchk_uuid bcardiff/rclone +$ docker run --rm -it -v $(pwd)/config:/config -v /path/to/source:/source -e SYNC_SRC="/source" -e SYNC_DEST="dest:path" -e TZ="America/Chicago" -e CRON="0 0 * * *" -e CRON_ABORT="0 6 * * *" -e FORCE_SYNC=1 -e CHECK_URL=https://hchk.io/hchk_uuid pfidr/rclone ``` See [rclone sync docs](https://rclone.org/commands/rclone_sync/) for source/dest syntax and additional options. + +## Changelog + ++ **07/06/2023:** + * Update to latest Rclone (v1.63.0) ++ **07/18/2022:** + * Update to latest Rclone (v1.59.0) ++ **04/25/2022:** + * Display exit code in console output + * Fix issue with healthcheck + * Update to latest Rclone (v1.58.0) ++ **11/18/2021:** + * Update to latest Rclone (v1.57.0) ++ **10/04/2021:** + * Update to latest Rclone (v1.56.2) ++ **09/27/2021:** + * Update to latest Rclone (v1.56.1) ++ **08/25/2021:** + * Update to latest Rclone (v1.56.0) ++ **06/25/2021:** + * Update to latest Rclone (v1.55.1) ++ **06/25/2021:** + * Update to latest Rclone (v1.55.0) ++ **03/10/2021:** + * Update to latest Rclone (v1.54.1) ++ **02/04/2021:** + * Update to latest Rclone (v1.54.0) ++ **01/22/2021:** + * Update to latest Rclone (v1.53.4) ++ **11/23/2020:** + * Update to latest Rclone (v1.53.3) ++ **10/28/2020:** + * Update to latest Rclone (v1.53.2) ++ **09/13/2020:** + * Update to latest Rclone (v1.53.1) ++ **09/05/2020:** + * Update to latest Rclone (v1.53.0) ++ **08/31/2020:** + * Add ability to send log data to healthchecks.io - See `HC_LOG` environment variable ++ **08/20/2020:** + * Update to latest Rclone (v1.52.3) ++ **06/25/2020:** + * Update to latest Rclone (v1.52.2) ++ **06/11/2020:** + * Update to latest Rclone (v1.52.1) ++ **05/28/2020:** + * Eval the entire rclone command + * Modify how rclone errors are interpreted when checking if source directory is empty ++ **05/27/2020:** + * Update to latest Rclone (v1.52.0) + * Add `RCLONE_DIR_CMD_DEPTH` option to declare recursion depth when checking if `SYNC_SRC` is empty + * Move call to signal start of healthchecks.io further up in the sync process + * Change when logs are deleted to make sure an active log is not deleted ++ **05/18/2020:** + * Modify how rclone errors are interpreted when passing results to healthchecks.io ++ **05/17/2020:** + * Handle spaces in `SYNC_SRC` and `SYNC_DEST` ++ **02/01/2020:** + * Update to latest Rclone (v1.51.0) ++ **11/20/2019:** + * Update to latest Rclone (v1.50.2) ++ **11/18/2019:** + * Add support for UID/GID ++ **11/06/2019:** + * Update to latest Rclone (v1.50.1) ++ **10/27/2019:** + * Update to latest Rclone (v1.50.0) ++ **10/07/2019:** + * Update to latest Rclone (v1.49.5) ++ **10/01/2019:** + * Update to latest Rclone (v1.49.4) ++ **09/23/2019:** + * Add environment variable SYNC_ONCE ++ **09/19/2019:** + * Add environment variable SYNC_OPTS_EVAL ++ **09/17/2019:** + * Update to latest Rclone (v1.49.3) ++ **09/10/2019:** + * Regression on log rotation ++ **09/09/2019:** + * Update to latest Rclone (v1.49.2) ++ **08/29/2019:** + * Update to latest Rclone (v1.49.1) ++ **08/20/2019:** + * Add start command for healthchecks.io calls + * Add debug messages for healthchecks.io calls ++ **08/19/2019:** + * Correct log rotation when there are no logs ++ **07/18/2019:** + * Optimizations to dockerfile ++ **06/22/2019:** + * Update to latest Rclone (v1.48.0) ++ **05/01/2019:** + * Initial release + +
+
+
+
+Credit to Brian J. Cardiff for the orginal project @ https://github.com/bcardiff/docker-rclone diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..a6ff8e7 --- /dev/null +++ b/build.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +docker build --no-cache --build-arg RCLONE_VERSION=v1.47.0 -t pfidr/rclone:1.47 -t pfidr/rclone:1.47.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.48.0 -t pfidr/rclone:1.48 -t pfidr/rclone:1.48.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.0 -t pfidr/rclone:1.49.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.1 -t pfidr/rclone:1.49.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.2 -t pfidr/rclone:1.49.2 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.3 -t pfidr/rclone:1.49.3 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.4 -t pfidr/rclone:1.49.4 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.49.5 -t pfidr/rclone:1.49.5 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.50.0 -t pfidr/rclone:1.50.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.50.1 -t pfidr/rclone:1.50.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.50.2 -t pfidr/rclone:1.50.2 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.51.0 -t pfidr/rclone:1.51.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.52.0 -t pfidr/rclone:1.52.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.52.1 -t pfidr/rclone:1.52.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.52.2 -t pfidr/rclone:1.52.2 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.52.3 -t pfidr/rclone:1.52.3 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.53.0 -t pfidr/rclone:1.53.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.53.1 -t pfidr/rclone:1.53.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.53.2 -t pfidr/rclone:1.53.2 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.53.3 -t pfidr/rclone:1.53.3 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.53.4 -t pfidr/rclone:1.53.4 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.54.0 -t pfidr/rclone:1.54.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.54.1 -t pfidr/rclone:1.54.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.55.0 -t pfidr/rclone:1.55.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.55.1 -t pfidr/rclone:1.55.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.56.0 -t pfidr/rclone:1.56.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.56.1 -t pfidr/rclone:1.56.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.56.2 -t pfidr/rclone:1.56.2 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.57.0 -t pfidr/rclone:1.57.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.58.0 -t pfidr/rclone:1.58.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.59.0 -t pfidr/rclone:1.59.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.60.0 -t pfidr/rclone:1.60.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.61.0 -t pfidr/rclone:1.61.0 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.61.1 -t pfidr/rclone:1.61.1 . +docker build --no-cache --build-arg RCLONE_VERSION=v1.63.0 -t pfidr/rclone:1.63.0 -t pfidr/rclone:latest -t pfidr/rclone:test -t rclone_test . diff --git a/entrypoint.sh b/entrypoint.sh index de9d02f..c0f5274 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,6 +2,73 @@ set -e +# Announce version +echo "INFO: Running $(rclone --version | head -n 1)" + +# Make sure sync/copy command is ok +if [ "$(echo "$RCLONE_CMD" | tr '[:lower:]' '[:upper:]')" != "SYNC" ] && [ "$(echo "$RCLONE_CMD" | tr '[:lower:]' '[:upper:]')" != "COPY" ] && [ "$(echo "$RCLONE_CMD" | tr '[:lower:]' '[:upper:]')" != "MOVE" ] +then + echo "WARNING: rclone command '$RCLONE_CMD' is not supported by this container, please use sync/copy/move. Stopping." + exit 1 +fi + +# Make sure dir command is ok +if [ "$(echo "$RCLONE_DIR_CMD" | tr '[:lower:]' '[:upper:]')" != "LS" ] && [ "$(echo "$RCLONE_DIR_CMD" | tr '[:lower:]' '[:upper:]')" != "LSF" ] +then + echo "WARNING: rclone directory command '$RCLONE_DIR_CMD' is not supported by this container, please use ls or lsf. Stopping." + exit 1 +fi + +# Make sure UID and GID are both supplied +if [ -z "$GID" -a ! -z "$UID" ] || [ -z "$UID" -a ! -z "$GID" ] +then + echo "WARNING: Must supply both UID and GID or neither. Stopping." + exit 1 +fi + +# Process UID and GID +if [ ! -z "$GID" ] +then + + #Get group name or add it + GROUP=$(getent group "$GID" | cut -d: -f1) + if [ -z "$GROUP" ] + then + GROUP=rclone + addgroup --gid "$GID" "$GROUP" + fi + + #get user or add it + USER=$(getent passwd "$UID" | cut -d: -f1) + if [ -z "$USER" ] + then + USER=rclone + adduser \ + --disabled-password \ + --gecos "" \ + --no-create-home \ + --ingroup "$GROUP" \ + --uid "$UID" \ + "$USER" >/dev/null + fi +else + USER=$(whoami) +fi + +# Re-write cron shortcut +case "$(echo "$CRON" | tr '[:lower:]' '[:upper:]')" in + *@YEARLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 1 1 *" && CRONS="0 0 1 1 *";; + *@ANNUALLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 1 1 *" && CRONS="0 0 1 1 *";; + *@MONTHLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 1 * *" && CRONS="0 0 1 * * ";; + *@WEEKLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 * * 0" && CRONS="0 0 * * 0";; + *@DAILY* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 * * *" && CRONS="0 0 * * *";; + *@MIDNIGHT* ) echo "INFO: Cron shortcut $CRON re-written to 0 0 * * *" && CRONS="0 0 * * *";; + *@HOURLY* ) echo "INFO: Cron shortcut $CRON re-written to 0 * * * *" && CRONS="0 * * * *";; + *@* ) echo "WARNING: Cron shortcut $CRON is not supported. Stopping." && exit 1;; + * ) CRONS=$CRON;; +esac + +# Set time zone if passed in if [ ! -z "$TZ" ] then cp /usr/share/zoneinfo/$TZ /etc/localtime @@ -10,34 +77,44 @@ fi rm -f /tmp/sync.pid +# Check for source and destination ; launch config if missing if [ -z "$SYNC_SRC" ] || [ -z "$SYNC_DEST" ] then echo "INFO: No SYNC_SRC and SYNC_DEST found. Starting rclone config" - rclone config $RCLONE_OPTS + su "$USER" -c "rclone config $RCLONE_OPTS" echo "INFO: Define SYNC_SRC and SYNC_DEST to start sync process." else # SYNC_SRC and SYNC_DEST setup # run sync either once or in cron depending on CRON - if [ -z "$FAIL_URL" ] - then + + #Create fail URL if CHECK_URL is populated but FAIL_URL is not + if [ ! -z "$CHECK_URL" ] && [ -z "$FAIL_URL" ] + then FAIL_URL="${CHECK_URL}/fail" fi - if [ -z "$CRON" ] + + if [ -z "$CRONS" ] then echo "INFO: No CRON setting found. Running sync once." echo "INFO: Add CRON=\"0 0 * * *\" to perform sync every midnight" - /sync.sh + su "$USER" -c /sync.sh else if [ -z "$FORCE_SYNC" ] then echo "INFO: Add FORCE_SYNC=1 to perform a sync upon boot" else - /sync.sh + su "$USER" -c /sync.sh + fi + + if [ ! -z "$SYNC_ONCE" ] + then + echo "INFO: SYNC_ONCE option activated. Stopping now." + exit 0 fi # Setup cron schedule crontab -d - echo "$CRON /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp + echo "$CRONS su $USER -c /sync.sh >>/tmp/sync.log 2>&1" > /tmp/crontab.tmp if [ -z "$CRON_ABORT" ] then echo "INFO: Add CRON_ABORT=\"0 6 * * *\" to cancel outstanding sync at 6am" @@ -56,4 +133,3 @@ else tail -F /tmp/crond.log /tmp/sync.log fi fi - diff --git a/sync.sh b/sync.sh index 2009560..9424d6b 100755 --- a/sync.sh +++ b/sync.sh @@ -4,27 +4,113 @@ echo "INFO: Starting sync.sh pid $$ $(date)" if [ `lsof | grep $0 | wc -l | tr -d ' '` -gt 1 ] then - echo "WARNING: A previous sync is still running. Skipping new sync command." + echo "WARNING: A previous $RCLONE_CMD is still running. Skipping new $RCLONE_CMD command." else -echo $$ > /tmp/sync.pid + # Signal start oh sync.sh to healthchecks.io + if [ ! -z "$CHECK_URL" ] + then + echo "INFO: Sending start signal to healthchecks.io" + wget $CHECK_URL/start -O /dev/null + fi -# the source directory is not empty -# it can be synced without clear data loss -echo "INFO: Starting rclone $COMMAND $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS" -rclone $COMMAND $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS -export RETURN_CODE=$? -if [ -z "$CHECK_URL" ] -then - echo "INFO: Define CHECK_URL with https://healthchecks.io to monitor sync job" -else - if [ "$RETURN_CODE" == 0 ] + # Delete logs by user request + if [ ! -z "${ROTATE_LOG##*[!0-9]*}" ] then - wget $CHECK_URL -O /dev/null + echo "INFO: Removing logs older than $ROTATE_LOG day(s)..." + touch /logs/tmp.txt && find /logs/*.txt -mtime +$ROTATE_LOG -type f -delete && rm -f /logs/tmp.txt + fi + + echo $$ > /tmp/sync.pid + + # Evaluate any sync options + if [ ! -z "$SYNC_OPTS_EVAL" ] + then + SYNC_OPTS_EVALUALTED=$(eval echo $SYNC_OPTS_EVAL) + echo "INFO: Evaluated SYNC_OPTS_EVAL to: ${SYNC_OPTS_EVALUALTED}" + SYNC_OPTS_ALL="${SYNC_OPTS} ${SYNC_OPTS_EVALUALTED}" else - wget $FAIL_URL -O /dev/null + SYNC_OPTS_ALL="${SYNC_OPTS}" + fi + + if [ ! -z "$RCLONE_DIR_CHECK_SKIP" ] + then + echo "INFO: Skipping source directory check..." + if [ ! -z "$OUTPUT_LOG" ] + then + d=$(date +%Y_%m_%d-%H_%M_%S) + LOG_FILE="/logs/$d.txt" + echo "INFO: Log file output to $LOG_FILE" + echo "INFO: Starting rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL --log-file=${LOG_FILE}" + set +e + eval "rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL --log-file=${LOG_FILE}" + export RETURN_CODE=$? + set -e + else + echo "INFO: Starting rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL" + set +e + eval "rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL" + export RETURN_CODE=$? + set -e + fi + echo "INFO: $RCLONE_CMD finished with return code: $RETURN_CODE" + else + set e+ + if test "$(rclone --max-depth $RCLONE_DIR_CMD_DEPTH $RCLONE_DIR_CMD "$(eval echo $SYNC_SRC)" $RCLONE_OPTS)"; then + set e- + echo "INFO: Source directory is not empty and can be processed without clear loss of data" + if [ ! -z "$OUTPUT_LOG" ] + then + d=$(date +%Y_%m_%d-%H_%M_%S) + LOG_FILE="/logs/$d.txt" + echo "INFO: Log file output to $LOG_FILE" + echo "INFO: Starting rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL --log-file=${LOG_FILE}" + set +e + eval "rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL --log-file=${LOG_FILE}" + export RETURN_CODE=$? + set -e + echo "INFO: $RCLONE_CMD finished with return code: $RETURN_CODE" + else + echo "INFO: Starting rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL" + set +e + eval "rclone $RCLONE_CMD $SYNC_SRC $SYNC_DEST $RCLONE_OPTS $SYNC_OPTS_ALL" + export RETURN_CODE=$? + set -e + echo "INFO: $RCLONE_CMD finished with return code: $RETURN_CODE" + fi + else + echo "WARNING: Source directory is empty. Skipping $RCLONE_CMD command." + fi + fi + + # Wrap up healthchecks.io call with complete or failure signal + if [ -z "$CHECK_URL" ] + then + echo "INFO: Define CHECK_URL with https://healthchecks.io to monitor $RCLONE_CMD job" + else + if [ "$RETURN_CODE" == 0 ] + then + if [ ! -z "$OUTPUT_LOG" ] && [ ! -z "$HC_LOG" ] && [ -f "$LOG_FILE" ] + then + echo "INFO: Sending complete signal with logs to healthchecks.io" + m=$(tail -c 10000 "$LOG_FILE") + wget $CHECK_URL -O /dev/null --post-data="$m" + else + echo "INFO: Sending complete signal to healthchecks.io" + wget $CHECK_URL -O /dev/null --post-data="SUCCESS" + fi + else + if [ ! -z "$OUTPUT_LOG" ] && [ ! -z "$HC_LOG" ] && [ -f "$LOG_FILE" ] + then + echo "INFO: Sending failure signal with logs to healthchecks.io" + m=$(tail -c 10000 "$LOG_FILE") + wget $FAIL_URL -O /dev/null --post-data="$m" + else + echo "INFO: Sending failure signal to healthchecks.io" + wget $FAIL_URL -O /dev/null --post-data="Check container logs" + fi + fi fi -fi rm -f /tmp/sync.pid