-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmount.sh
executable file
·130 lines (111 loc) · 3.18 KB
/
mount.sh
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
set -e
usage="$(basename "$0") -d/--drives-mount-dir=/path/to/drive, to mount all remotes configed in rclone
where:
-h show this help text
-d/--drives-base-dir set the remotes mount base directory
or set the drives-base-dir via envrionment variables:
export DRIVES_BASE_DIR=/path/to/drive
"
function parser {
for i in "$@"; do
case $i in
-d=* | --drives-base-dir=*)
DRIVES_BASE_DIR="${i#*=}"
shift # past argument=value
;;
-h)
echo "$usage"
exit 1
;;
*)
echo "Unknown option $i"
echo "$usage"
exit 1
;;
esac
done
}
function assert_installed {
# if ! apt -qq list "$1" --installed 2>/dev/null | grep -qE "(installed|upgradeable)"; then
if ! command -v "$1" &>/dev/null; then
echo " $1 is not installed, please install it, exit..."
exit 1
fi
}
function assert_dir_exists {
if [ ! -d "$1" ]; then
echo " $1 does not exist, exit..."
exit 1
fi
}
function create_dir {
# create dir if not exists, make sure the parente exists
[ -d "$1" ] || mkdir -p "$1"
}
function is_mount {
path=$(readlink -f "$1")
grep -q "$path" /proc/mounts
}
parser "$@"
# check required parameters
if [[ -z "${DRIVES_BASE_DIR}" ]]; then
echo "$usage"
exit 1
else
DRIVES_BASE_DIR="$(realpath "${DRIVES_BASE_DIR}")"
fi
# check drives-base-dir exists
assert_dir_exists "$DRIVES_BASE_DIR"
# check if rclone installed
PRE_INSTALLS=("rclone")
for program in "${PRE_INSTALLS[@]}"; do
assert_installed "$program"
done
DRIVES_LOG_DIR="$DRIVES_BASE_DIR/logs"
DRIVES_CACHE_DIR="$DRIVES_BASE_DIR/.cache"
DRIVES_MOUNT_DIR="$DRIVES_BASE_DIR/drives"
# create directories for logs, cache
for path in "$DRIVES_LOG_DIR" "$DRIVES_CACHE_DIR" "$DRIVES_MOUNT_DIR"; do
create_dir "$path"
done
# wait internet on
while ! wget -q --spider https://google.com; do
echo "Waiting for internet online..."
sleep 5
done
while read -r REMOTE; do
REMOTE_NAME="${REMOTE%?}"
MOUNTPOINT="$DRIVES_MOUNT_DIR/$REMOTE_NAME"
# no write permission, and mounted
if [ ! -w "$MOUNTPOINT" ] && is_mount "$MOUNTPOINT"; then
# try to unmount
fusermount -uz "$MOUNTPOINT"
fi
create_dir "$MOUNTPOINT"
if is_mount "$MOUNTPOINT"; then
echo "$MOUNTPOINT is already mounted"
else
echo "Prepare to mount remote:$REMOTE under directory:$MOUNTPOINT"
if rclone mount "$REMOTE" "$MOUNTPOINT" \
--config ~/.config/rclone/rclone.conf \
--attr-timeout 1s \
--dir-cache-time 5m0s \
--vfs-cache-mode full \
--vfs-cache-max-age 480h0m0s \
--vfs-cache-poll-interval 1m0s \
--vfs-write-back 5s \
--write-back-cache \
--log-file "$DRIVES_LOG_DIR/$REMOTE_NAME.log" \
--cache-dir "$DRIVES_CACHE_DIR" \
--daemon; then
echo "$REMOTE is mounted at: $MOUNTPOINT"
else
echo "$REMOTE mount failed. Perform test now"
rclone lsd "$REMOTE" -vv
exit 1
fi
fi
done <<EOF
$(rclone listremotes)
EOF