|
#!/bin/bash |
|
# Check for required arguments |
|
if [ "$#" -ne 2 ]; then |
|
echo "Usage: $0 <webdav_username> <webdav_password>" |
|
exit 1 |
|
fi |
|
|
|
# Assign arguments to variables |
|
webdav_username="$1" |
|
webdav_password="$2" |
|
|
|
shift 2 |
|
|
|
# Variables |
|
user_home=$(getent passwd ${SUDO_USER:-$USER} | cut -d: -f6) |
|
log_dir="$user_home/rclone_setup_logs" |
|
log_file="$log_dir/rclone_setup.log" |
|
ip_file="$user_home/webdav_ips.txt" |
|
|
|
# Ensure directories and files exist |
|
initialize() { |
|
mkdir -p "$log_dir" |
|
touch "$log_file" |
|
if [ ! -f "$ip_file" ]; then |
|
echo "Creating empty WebDAV IPs file." |
|
touch "$ip_file" |
|
fi |
|
} |
|
|
|
# Read WebDAV server IPs from file |
|
read_webdav_ips() { |
|
if [ -s "$ip_file" ]; then |
|
readarray -t webdav_servers < "$ip_file" |
|
else |
|
echo "No WebDAV server IPs found in $ip_file. Exiting." |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Add new IPs to the file (avoid duplicates) |
|
add_ips_to_file() { |
|
for ip in "$@"; do |
|
if ! grep -q "^$ip$" "$ip_file"; then |
|
echo "$ip" >> "$ip_file" |
|
fi |
|
done |
|
} |
|
|
|
# Install or update rclone |
|
install_update_rclone() { |
|
echo "Installing or updating rclone..." |
|
curl https://rclone.org/install.sh | sudo bash |
|
} |
|
|
|
# Configure a single WebDAV remote |
|
configure_webdav() { |
|
local name=$1 |
|
local ip=$2 |
|
echo "Configuring WebDAV remote: $name with IP: $ip" |
|
rclone config create "$name" webdav url "http://$ip" vendor "other" user "$webdav_username" pass "$webdav_password" > /dev/null 2>&1 |
|
} |
|
|
|
# Configure the union remote |
|
configure_union() { |
|
if [ "${#webdav_servers[@]}" -lt 1 ]; then |
|
echo "Union remote requires at least two upstreams to be configured. Exiting." |
|
exit 1 |
|
fi |
|
local remotes="" |
|
for i in "${!webdav_servers[@]}"; do |
|
remotes+="webdav$i:/ " |
|
done |
|
echo "Configuring union remote with remotes: $remotes" |
|
rclone config create union_remote union upstreams "$remotes" > /dev/null 2>&1 |
|
} |
|
|
|
# Update WebDAV IPs, reconfigure remotes |
|
update_and_configure() { |
|
echo "Updating WebDAV IPs and reconfiguring remotes..." |
|
rclone config delete union_remote |
|
for i in "${!webdav_servers[@]}"; do |
|
configure_webdav "webdav$i" "${webdav_servers[$i]}" |
|
done |
|
configure_union |
|
} |
|
|
|
# Stop existing rclone services |
|
stop_services() { |
|
echo "Stopping any existing rclone serve services..." |
|
sudo systemctl stop rclone-serve-webdav.service || true |
|
sudo systemctl stop rclone-mount.service || true |
|
} |
|
|
|
# Create and start systemd services for rclone serve |
|
create_and_start_services() { |
|
echo "Creating and starting systemd services for rclone serve..." |
|
|
|
# WebDAV Serve Service |
|
sudo bash -c "cat <<EOF > /etc/systemd/system/rclone-serve-webdav.service |
|
[Unit] |
|
Description=Rclone Serve WebDAV |
|
After=network.target |
|
|
|
[Service] |
|
Type=simple |
|
ExecStart=/usr/bin/rclone serve webdav union_remote: --addr 0.0.0.0:8080 --user \"$webdav_username\" --pass \"$webdav_password\" |
|
Restart=always |
|
RestartSec=10 |
|
|
|
[Install] |
|
WantedBy=multi-user.target |
|
EOF" |
|
|
|
# WebDAV Serve Service |
|
sudo bash -c "cat <<EOF > /etc/systemd/system/rclone-mount.service |
|
[Unit] |
|
Description=Rclone Mount |
|
After=network.target |
|
|
|
[Service] |
|
Type=simple |
|
ExecStart=/usr/bin/rclone mount union_remote: /mnt/union --allow-other --allow-non-empty --dir-cache-time 30s --poll-interval 5s --vfs-read-chunk-size 32M --vfs-read-chunk-size-limit 2G --buffer-size 512M --read-only --no-modtime --vfs-cache-mode=full |
|
Restart=always |
|
RestartSec=10 |
|
|
|
[Install] |
|
WantedBy=multi-user.target |
|
EOF" |
|
|
|
# Reload systemd, enable and start services |
|
sudo systemctl daemon-reload |
|
sudo systemctl enable rclone-serve-webdav.service |
|
sudo systemctl enable rclone-mount.service |
|
sudo systemctl start rclone-serve-webdav.service |
|
sudo systemctl start rclone-mount.service |
|
} |
|
|
|
# Main execution logic |
|
initialize |
|
sudo mkdir -p /mnt/union |
|
stop_services |
|
add_ips_to_file "$@" |
|
read_webdav_ips |
|
|
|
install_update_rclone |
|
|
|
# If there are WebDAV servers to configure, proceed |
|
if [ ${#webdav_servers[@]} -ne 0 ]; then |
|
update_and_configure |
|
create_and_start_services |
|
else |
|
echo "No WebDAV servers to configure. Exiting." |
|
exit 1 |
|
fi |
|
|
|
echo "Rclone setup and serving configuration complete." |