ansible/roles/rclone/files/rclone-sync.sh

37 lines
959 B
Bash
Executable file

#!/bin/sh
set -eu
umask 027
SERVICE="$(whoami)"
TARGET="/srv/${SERVICE}"
CONFIG="/etc/rclone/${SERVICE}.conf"
LOGDIR="/var/log/rclone/${SERVICE}"
RCLONE="/usr/local/bin/rclone"
timestamp="$(date +%Y%m%d%H%M%S)"
if [ ! -f "$CONFIG" ]; then
echo "ERR: Config file '${CONFIG}' does not exist" 1>&2
exit 1
fi
if [ ! -d "$TARGET" ]; then
echo "ERR: Destination directory '${TARGET}' does not exist" 1>&2
exit 1
fi
for host in $("$RCLONE" --config "$CONFIG" listremotes | tr -d ":") ; do
fqdn="$("$RCLONE" --config "$CONFIG" config show "$host" | \
awk '{ if ($1 == "host") print $3 }')"
if [ ! -d "${TARGET}/${fqdn}" ]; then
mkdir "${TARGET}/${fqdn}"
fi
log="${LOGDIR}/${fqdn}.${timestamp}.log"
if ! "$RCLONE" --config "$CONFIG" --log-file "$log" --log-level INFO \
sync "${host}:/" "${TARGET}/${fqdn}/"; then
cat "$log"
fi
done
find "$LOGDIR" -type f -name "*.log" -mtime +30 -delete