Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Created February 7, 2022 11:40
Show Gist options
  • Save hongkongkiwi/9ea843255d502d6483b5f812fda8fb0c to your computer and use it in GitHub Desktop.
Save hongkongkiwi/9ea843255d502d6483b5f812fda8fb0c to your computer and use it in GitHub Desktop.
RClone mount script for Synology NAS
#!/bin/bash
ACTION="$1"
# RClone Service Series
RCLONE="/bin/rclone"
CONFIG_FILE="/volume1/@rclone/config/rclone.conf"
MOUNT_NAME="${2:-"union_series"}"
MOUNT_POINT="${3:-"/volume1/media/series"}"
MOUNT_CACHE_DIR="/volume1/@tmp/@${MOUNT_NAME}"
LOG_FILE="/volume1/@rclone/log/${MOUNT_NAME}.log"
RC_ADDR="127.0.0.1:${4:-"5572"}"
CACHE_MODE="full"
CACHE_MAX_SIZE="3200G"
CACHE_MAX_AGE="9999h"
TPS_LIMIT="12"
POLL_INTERVAL="10s"
LOG_LEVEL="DEBUG"
#LOG_LEVEL="NOTICE"
RCLONE_VERSION="v1.56.2"
install_rclone() {
command -v "$RCLONE" >/dev/null 2>&1 && return 0
if [ -z "$RCLONE_VERSION" -o "$RCLONE_VERSION" == "latest" ]; then
curl "https://rclone.org/install.sh" | bash
elif [ "$RCLONE_VERSION" == "beta" ]; then
curl "https://rclone.org/install.sh" | bash -s beta
else
cd /tmp
curl -O "https://downloads.rclone.org/${RCLONE_VERSION}/rclone-${RCLONE_VERSION}-linux-amd64.zip"
mkdir "/tmp/rclone-${RCLONE_VERSION}-linux-amd64"
7z e -y -o"/tmp/rclone-${RCLONE_VERSION}-linux-amd64" "/tmp/rclone-${RCLONE_VERSION}-linux-amd64.zip" >/dev/null || exit $?
cd rclone-*-linux-amd64
cp rclone "$RCLONE"
chown root:root "$RCLONE"
chmod 755 "$RCLONE"
cd /
rm -f /tmp/rclone-*-linux-amd64.zip
rm -Rf /tmp/rclone-*-linux-amd64
fi
}
stop_mount() {
[ -d "$MOUNT_POINT" ] || return 0
check_mount || return 0
[ -f "/bin/fusermount" ] || return 255
"/bin/fusermount" -uz "$MOUNT_POINT"
return $?
}
start_mount() {
if [ "$EUID" -ne 0 ]; then
echo >&2 "ERROR: Please run as root"
exit 255
fi
# stop existing mount if it's mounted
check_mount && stop_mount
# [ -L "/sbin/mount.rclone" ] || ln -s "/bin/rclone" "/sbin/mount.rclone"
# [ -L "/usr/bin/rclonefs" ] || ln -s "/bin/rclone" "/usr/bin/rclonefs"
[ -d "$MOUNT_POINT" ] || mkdir -p "$MOUNT_POINT"
[ -d "$MOUNT_CACHE_DIR" ] || mkdir -p "$MOUNT_CACHE_DIR"
LOG_DIR="$(dirname "$LOG_FILE")"
[ -d "$LOG_DIR" ] || mkdir -p "$LOG_DIR"
chmod 777 "$MOUNT_POINT" "$MOUNT_CACHE_DIR" "$LOG_DIR"
touch "$LOG_FILE"
chmod 777 "$LOG_FILE"
"$RCLONE" \
mount "${MOUNT_NAME}:" \
"$MOUNT_POINT" \
$DAEMON_ARG \
--config "$CONFIG_FILE" \
--allow-other \
--dir-cache-time "$CACHE_MAX_AGE" \
--log-file "$LOG_FILE" \
--log-level "$LOG_LEVEL" \
--poll-interval $POLL_INTERVAL \
--umask 002 \
--cache-dir="$MOUNT_CACHE_DIR" \
--vfs-cache-mode "$CACHE_MODE" \
--vfs-cache-max-size "$CACHE_MAX_SIZE" \
--vfs-cache-max-age "$CACHE_MAX_AGE" \
--tpslimit $TPS_LIMIT \
--tpslimit-burst $TPS_LIMIT \
--rc \
--rc-addr "$RC_ADDR" \
--rc-no-auth \
--rc-enable-metrics
return $?
}
refresh_mount() {
"$RCLONE" rc vfs/refresh recursive=true --no-output --rc-addr "$RC_ADDR" _async=true
return $?
}
check_mount() {
mount | grep -q "^${MOUNT_NAME}:\s"
return $?
}
case "$ACTION" in
mount|start)
DAEMON_ARG="--daemon"
install_rclone;
start_mount;
refresh_mount;
exit $?;
;;
mount-foreground|foreground)
DAEMON_ARG=""
install_rclone;
start_mount;
exit $?;
;;
unmount|umount|stop)
stop_mount;
exit $?;
;;
refresh)
refresh_mount;
exit $?;
;;
check|checkmount)
check_mount && echo "mounted" || echo "not mounted";
;;
*)
echo >&2 "$0 {mount|foreground|unmount|refresh|check}";
exit 255;
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment