-
Notifications
You must be signed in to change notification settings - Fork 3
/
utility-rclone-google-drive
294 lines (192 loc) · 6.22 KB
/
utility-rclone-google-drive
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# dependencies /////////////////////////////////////////////////////////////////
if [ -f "${HOME}"/.local/bin/frobulator ]
then
rm -r -f "${HOME}"/.local/bin/frobulator
fi
if [[ -z $(command -v frobulator) ]]
then
if [[ $(id -u -n) = "root" ]]
then
SUDO_HOME=/root
USER="${SUDO_USER}"
HOME=/home/"${USER}"
fi
if [[ -z $(command -v curl) ]]
then
yes | apt-get install curl
fi
if [ ! -d "${HOME}"/.local/bin ]
then
mkdir -p "${HOME}"/.local/bin
fi
curl -s -L get.frbltr.app > "${HOME}"/.local/bin/frobulator
chmod +x "${HOME}"/.local/bin/frobulator
fi
. "${HOME}"/.local/bin/frobulator
# superuser ////////////////////////////////////////////////////////////////////
# script ///////////////////////////////////////////////////////////////////////
script=$(basename -- "${BASH_SOURCE[0]}")
# version //////////////////////////////////////////////////////////////////////
version="11-09-2024"
# usage ////////////////////////////////////////////////////////////////////////
# prompt ///////////////////////////////////////////////////////////////////////
frobulator.script "Setting up ${script#*-}"
# variables ////////////////////////////////////////////////////////////////////
service="rclone-google-drive"
service_timer="${service}.timer"
service_service="${service}.service"
service_interval="5"
cloud_service="google drive"
cloud_server_directory="Google:"
local_server_directory="/media/internal"
# defaults /////////////////////////////////////////////////////////////////////
# functions ////////////////////////////////////////////////////////////////////
# defaults /////////////////////////////////////////////////////////////////////
list=(
rclone
rclone-browser
)
frobulator.require "${list[@]}"
list=()
# configuration
if [ -d /media/[iI]nternal/[rR][cC]lone ]
then
if [ -f /media/[iI]nternal/[rR][cC]lone/rclone.conf ]
then
frobulator.inf "Linking configuration"
echo
frobulator.directory "${HOME}" .config/rclone
frobulator.link /media/[iI]nternal/[rR][cC]lone/ "${HOME}"/.config/rclone rclone.conf
fi
fi
# script
cat <<- FILE > "${HOME}"/.local/bin/"${service}"
#!/bin/bash
# variables ////////////////////////////////////////////////////////////////////
cloud_server_directory="${cloud_server_directory}"
local_server_directory="${local_server_directory}"
# functions ////////////////////////////////////////////////////////////////////
rclone_transfer () {
${TAB}rclone=$(command -v rclone)
${TAB}streams="16"
${TAB}synchronization_option="\${1}"
${TAB}source="\${2}"
${TAB}destination="\${3}"
${TAB}exclude="\${4}"
${TAB}unset transfer_command
${TAB}transfer_command+="\${rclone}"
${TAB}transfer_command+=" \${synchronization_option}"
${TAB}transfer_command+=" \${source}"
${TAB}transfer_command+=" \${destination}"
${TAB}transfer_command+=" --create-empty-src-dirs"
${TAB}transfer_command+=" --drive-skip-gdocs"
${TAB}if [ ! -z "\${4}" ]
${TAB}then
${TAB}${TAB}exclude="/\${exclude}/**"
${TAB}${TAB}transfer_command+=" --exclude \${exclude}"
${TAB}fi
${TAB}transfer_command+=" --multi-thread-streams \${streams}"
${TAB}transfer_command+=" --progress"
${TAB}transfer_command+=" --verbose"
${TAB}eval \${transfer_command}
}
# batch directory management
# set IFS
IFS=\$'\\n'
server_directories_list=(
${TAB}\$(find "\${local_server_directory}" \\
${TAB}${TAB}-mindepth 1 \\
${TAB}${TAB}-maxdepth 1 \\
${TAB}${TAB}-type d \\
${TAB}${TAB}! -iname "*.Trash*" \\
${TAB}${TAB}! -iname "*Git*" \\
${TAB}${TAB}! -iname "*Music*" \\
${TAB})
)
# unset IFS
IFS=''
for server_directory in \${server_directories_list[@]}
do
${TAB}server_directory=\${server_directory##*/}
${TAB}# synchronize:
${TAB}# local machine >>> ${cloud_service}
${TAB}echo "Synchronizing: " "\${server_directory}"
${TAB}echo
${TAB}rclone_transfer sync "\${local_server_directory}/\${server_directory}" "\${cloud_server_directory}/\${server_directory}"
${TAB}# copy:
${TAB}# ${cloud_service} >>> local machine
${TAB}echo "Copying: " "\${server_directory}"
${TAB}echo
${TAB}rclone_transfer copy "\${cloud_server_directory}/\${server_directory}" "\${local_server_directory}/\${server_directory}"
done
# unset directory list
server_directories_list=()
# individual directory management
echo "Synchronizing: " "Git"
echo
local_server_directory="${local_server_directory}"
rclone_transfer sync "\${local_server_directory}/Git" "\${cloud_server_directory}/Git"
echo "Synchronizing: " "Music"
echo
local_server_directory="/media/nathanel/Samsung${SPC}T7"
server_directory="/Music/STEYOYOKE"
rclone_transfer sync "\${local_server_directory}/\${server_directory}" "\${cloud_server_directory}/\${server_directory}"
local_server_directory="/media/nathanel/Samsung${SPC}T7"
server_directory="/Music/INNER${SPC}SYMPHONY"
rclone_transfer sync "\${local_server_directory}/\${server_directory}" "\${cloud_server_directory}/\${server_directory}"
FILE
# permissions
chmod +x "${HOME}"/.local/bin/"${service}"
# service
frobulator.directory "${HOME}" .config/systemd/user
# service timer
cat <<- 'FILE' > "${HOME}"/.config/systemd/user/"${service_timer}"
[Unit]
Description=RClone Google Drive synchronization
[Timer]
OnCalendar=*:0/05
Persistent=true
[Install]
WantedBy=timers.target
FILE
# service desciptor
cat <<- FILE > "${HOME}"/.config/systemd/user/"${service_service}"
[Unit]
Description=Synchronize Google Drive using RClone every 5 minutes
[Service]
ExecStart=${SHELL} ${HOME}/.local/bin/${service}
FILE
# service activation
if [[ $(systemctl status --user "${service_timer}") ]]
then
service_actions_list=(
disable
stop
)
service_action_message="Stopping service"
fi
service_actions_list=()
service_actions_list=(
enable
start
)
service_action_message="Starting service"
for service_action in ${service_actions_list[@]}
do
systemctl "${service_action}" --user "${service_timer}" > "${sink}" 2>&1 &
frobulator.process "${service_action_message}"
systemctl --user daemon-reload > "${sink}" 2>&1 &
frobulator.process "Reloading"
done
# service process verification
if [[ $(systemctl status --user "${service_service}" | grep "active") ]]
then
frobulator.scs "Service activation success" "[ ${service} ]"
echo
else
frobulator.err "Service activation failed" "[ ${service} ]"
echo
exit 1
fi
frobulator.scs "Done"