-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount_synology.sh
More file actions
90 lines (83 loc) · 3.65 KB
/
mount_synology.sh
File metadata and controls
90 lines (83 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# ===== Synology Drive Mount =====
#
# Description:
# This script mounts a Synology Drive remote (via rclone) to a local directory using FUSE.
# It handles cleanup of stale mounts, ensures the mountpoint exists, and performs the mount.
# Designed to be run as a non-root user.
#
# Usage:
# bash mount_synology.sh [REMOTE_PATH] [MOUNTPOINT]
#
# - REMOTE_PATH: The rclone remote path, e.g., "synology:/backups/Computers" (default if not provided)
# - MOUNTPOINT: The local mount directory, e.g., "/mnt/synology/Computers" (default if not provided)
#
# Prerequisites:
# - rclone installed and configured (e.g., run 'rclone config' to set up your Synology remote).
# - FUSE installed (e.g., 'sudo apt install fuse3' on Debian-based systems).
# - Add 'user_allow_other' to /etc/fuse.conf for --allow-other to work (one-time: 'echo "user_allow_other" | sudo tee /etc/fuse.conf').
# - If mounting under /mnt as a non-root user, create and assign the mountpoint once:
# sudo mkdir -p /mnt/synology/Computers
# sudo chown "$USER:$USER" /mnt/synology/Computers
# # if needed:
# sudo chown "$USER:$USER" /mnt/synology
#
# Customization:
# - Adjust cache options in the rclone mount command below as needed (e.g., --vfs-cache-max-size).
# - The script auto-detects the current user via $USER.
# - rclone config file is assumed at ~/.config/rclone/rclone.conf (adjust if different).
# - Logs errors to a file in the same directory as this script (synology-mount.log).
#
# For Automation:
# To run this automatically on user login via systemd:
# 1. Create ~/.config/systemd/user/synology-mount.service with:
# [Unit]
# Description=Mount Synology Drive with rclone
# After=network-online.target
#
# [Service]
# ExecStart=/bin/bash /path/to/this/script.sh "your_remote_path" "your_mountpoint" # e.g., /home/$USER/bin/mount_synology.sh "synology:/backups/Computers" "/mnt/synology/Computers"
# ExecStop=/bin/fusermount -uz "your_mountpoint"
# Restart=on-failure
# RestartSec=10
#
# [Install]
# WantedBy=default.target
#
# 2. Reload: systemctl --user daemon-reload
# 3. Enable and start: systemctl --user enable --now synology-mount.service
# 4. Check: systemctl --user status synology-mount.service
# 5. Logs: journalctl --user -u synology-mount.service
#
# On failure, a desktop notification will pop up (requires notify-send, common on GNOME/KDE/etc.).
#
# Troubleshooting:
# - Add --log-level DEBUG to rclone for more verbose output (logs to journal or file if set).
# - If mount fails, check network, rclone auth, or permissions.
# Get the directory of this script
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Default values (can be overridden by arguments)
REMOTE_PATH="${1:-synology:/backups/Computers}"
MOUNTPOINT="${2:-/mnt/synology/Computers}"
CURRENT_USER="${USER:-$(whoami)}"
RCLONE_CONF="/home/${CURRENT_USER}/.config/rclone/rclone.conf"
LOG_FILE="${SCRIPT_DIR}/synology-mount.log"
# --- Cleanup stale mounts ---
fusermount -uz "${MOUNTPOINT}" 2>/dev/null || true
# --- Ensure mountpoint exists and owned by current user ---
mkdir -p "${MOUNTPOINT}"
chown "${CURRENT_USER}:${CURRENT_USER}" "${MOUNTPOINT}"
# --- Mount command ---
/usr/bin/rclone mount "${REMOTE_PATH}" "${MOUNTPOINT}" \
--config "${RCLONE_CONF}" \
--allow-other \
--vfs-cache-mode full \
--vfs-cache-max-size 20G \
--vfs-cache-max-age 24h \
--dir-cache-time 12h \
--poll-interval 15s \
--umask 022 \
--log-level ERROR \
--log-file "${LOG_FILE}"
# If mount fails (rclone exits), notify user
notify-send "Synology Mount Failed" "Check journalctl or ${LOG_FILE} for details" -u critical