install.fairie/home/dot_local/bin/executable_rclone-mount
Brian Zalewski bdaf704a7d Latest
2024-04-02 03:09:30 +00:00

107 lines
3 KiB
Bash

#!/usr/bin/env bash
# @file RClone Mount
# @brief Helper utility to aid with mounting RClone S3 buckets to the file system
# @description
# This script is a helper utility to assist a system service that ensures various
# RClone S3 mounts are mounted properly. For more information, take a look at the
# RClone files stored in `~/.config/rclone`.
### Variables
TYPE="$1"
USER="$2"
MOUNT="$3"
### Mount variables
if [ "$TYPE" = 'user' ]; then
MOUNT_REF="User-$USER"
MOUNT_LOWERCASE="user/$USER-$MOUNT"
else
MOUNT_REF="$MOUNT"
MOUNT_LOWERCASE="$(echo "$MOUNT" | tr "[:upper:]" "[:lower:]")"
fi
### Path definitions
if [ "$TYPE" = 'user' ]; then
if [ -d /Applications ] && [ -d /System ]; then
USER_FOLDER='Users'
else
USER_FOLDER='home'
fi
CACHE_FOLDER="/var/cache/rclone/$MOUNT"
CONFIG_FOLDER="/$USER_FOLDER/$USER/.config/rclone"
LOG_FOLDER="/var/log/rclone"
LOG_FILE="$LOG_FOLDER/$MOUNT_LOWERCASE.log"
if [ -d /Applications ] && [ -d /System ]; then
MNT_FOLDER='Volumes'
else
MNT_FOLDER='mnt'
fi
MOUNT_PATH="/$MNT_FOLDER/$MOUNT"
else
CACHE_FOLDER="/var/cache/rclone/$MOUNT"
CONFIG_FOLDER="/etc"
LOG_FOLDER="/var/log/rclone"
LOG_FILE="$LOG_FOLDER/$MOUNT_LOWERCASE.log"
if [ -d /Applications ] && [ -d /System ]; then
MNT_FOLDER='Volumes'
else
MNT_FOLDER='mnt'
fi
MOUNT_PATH="/$MNT_FOLDER/$MOUNT"
fi
### Ensure folders exist
for FOLDER in "$CACHE_FOLDER" "$CONFIG_FOLDER" "$LOG_FOLDER" "$MOUNT_PATH"; do
if [ ! -d "$FOLDER" ]; then
mkdir -p "$FOLDER"
chmod 750 "$FOLDER"
chown -f "$USER:rclone" "$FOLDER"
fi
done
### Define rcloneignore location
RCLONE_IGNORE="$CONFIG_FOLDER/rcloneignore"
if [ ! -f "$RCLONE_IGNORE" ] && [ -f "/etc/rcloneignore" ]; then
RCLONE_IGNORE='/etc/rcloneignore'
fi
chown -f "$USER:rclone" "$CONFIG_FOLDER/rclone.conf"
chmod -f 600 "$CONFIG_FOLDER/rclone.conf"
### Mount
unset AWS_CA_BUNDLE
export PATH="$PATH:/usr/local/bin:/usr/bin"
# TODO: Only launch with --rc-web-gui if the servers hostname is the {{ .kubernetesHost }}
# TODO: Add more secure authentication method
rclone --config "$CONFIG_FOLDER/rclone.conf" \
mount \
--allow-other \
--buffer-size 4G \
--bwlimit 200M \
--cache-chunk-path "$CACHE_FOLDER/$MOUNT_REF-chunks" \
--cache-db-path "$CACHE_FOLDER/$MOUNT_REF-db" \
--cache-dir "$CACHE_FOLDER/$MOUNT_REF-vfs" \
--cache-info-age 1m \
--cache-tmp-upload-path "$CACHE_FOLDER/$MOUNT_REF-upload" \
--cache-workers 8 \
--cache-writes \
--checkers 16 \
--dir-cache-time 30s \
--drive-use-trash \
--exclude-from "$RCLONE_IGNORE" \
--log-file "$LOG_FILE" \
--log-level INFO \
--no-modtime \
--noapplexattr \
--poll-interval 15s \
--stats 0 \
--vfs-cache-max-age 1000h \
--vfs-cache-max-size 5G \
--vfs-cache-mode full \
--vfs-cache-poll-interval 15s \
--vfs-fast-fingerprint \
--vfs-read-ahead 128M \
--vfs-read-chunk-size 16M \
--vfs-read-chunk-size-limit 128M \
--volname "$MOUNT_REF" \
"$MOUNT_REF":"$MOUNT_LOWERCASE" "$MOUNT_PATH"