-
Notifications
You must be signed in to change notification settings - Fork 0
/
rclone_upload
214 lines (185 loc) · 9.67 KB
/
rclone_upload
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
#!/bin/bash
######################
### Upload Script ####
######################
### Version 0.95.5-scumbug ###
######################
####### EDIT ONLY THESE SETTINGS #######
# INSTRUCTIONS
# 1. Edit the settings below to match your setup
# 2. NOTE: enter RcloneRemoteName WITHOUT ':'
# 3. Optional: Add additional commands or filters
# 4. Optional: Use bind mount settings for potential traffic shaping/monitoring
# 5. Optional: Use service accounts in your upload remote
# 6. Optional: Use backup directory for rclone sync jobs
# REQUIRED SETTINGS
RcloneCommand="move" # choose your rclone command e.g. move, copy, sync
RcloneRemoteName="google_vfs" # Name of rclone remote mount WITHOUT ':'.
RcloneUploadRemoteName="google_vfs" # If you have a second remote created for uploads put it here. Otherwise use the same remote as RcloneRemoteName.
LocalFilesShare="/mnt/user/local" # location of the local files without trailing slash you want to rclone to use
RcloneMountShare="/mnt/user/mount_rclone" # where your rclone mount is located without trailing slash e.g. /mnt/user/mount_rclone
MinimumAge="30m" # sync files suffix ms|s|m|h|d|w|M|y
ModSort="ascending" # "ascending" oldest files first, "descending" newest files first
# Note: Again - remember to NOT use ':' in your remote name above
# Bandwidth limits: specify the desired bandwidth in kBytes/s, or use a suffix b|k|M|G. Or 'off' or '0' for unlimited. The script uses --drive-stop-on-upload-limit which stops the script if the 750GB/day limit is achieved, so you no longer have to slow 'trickle' your files all day if you don't want to e.g. could just do an unlimited job overnight.
BWLimit1Time="01:00"
BWLimit1="off"
BWLimit2Time="08:00"
BWLimit2="15M"
BWLimit3Time="16:00"
BWLimit3="12M"
# OPTIONAL SETTINGS
# Add extra commands or filters
Command1="--exclude downloads/**"
Command2=""
Command3=""
Command4=""
Command5=""
Command6=""
Command7=""
Command8=""
# Bind the mount to an IP address
CreateBindMount="N" # Y/N. Choose whether or not to bind traffic to a network adapter.
RCloneMountIP="192.168.1.253" # Choose IP to bind upload to.
NetworkAdapter="eth0" # choose your network adapter. eth0 recommended.
VirtualIPNumber="1" # creates eth0:x e.g. eth0:1.
# Use Service Accounts. Instructions: https://github.com/xyou365/AutoRclone
UseServiceAccountUpload="N" # Y/N. Choose whether to use Service Accounts.
ServiceAccountDirectory="/mnt/user/appdata/other/rclone/service_accounts" # Path to your Service Account's .json files.
ServiceAccountFile="sa_gdrive_upload" # Enter characters before counter in your json files e.g. for sa_gdrive_upload1.json -->sa_gdrive_upload100.json, enter "sa_gdrive_upload".
CountServiceAccounts="15" # Integer number of service accounts to use.
# Is this a backup job
BackupJob="N" # Y/N. Syncs or Copies files from LocalFilesLocation to BackupRemoteLocation, rather than moving from LocalFilesLocation/RcloneRemoteName
BackupRemoteLocation="backup" # choose location on mount for deleted sync files
BackupRemoteDeletedLocation="backup_deleted" # choose location on mount for deleted sync files
BackupRetention="90d" # How long to keep deleted sync files suffix ms|s|m|h|d|w|M|y
####### END SETTINGS #######
###############################################################################
##### DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING #####
###############################################################################
####### Preparing mount location variables #######
if [[ $BackupJob == 'Y' ]]; then
LocalFilesLocation="$LocalFilesShare"
echo "$(date "+%d.%m.%Y %T") INFO: *** Backup selected. Files will be copied or synced from ${LocalFilesLocation} for ${RcloneUploadRemoteName} ***"
else
LocalFilesLocation="$LocalFilesShare/$RcloneRemoteName"
echo "$(date "+%d.%m.%Y %T") INFO: *** Rclone move selected. Files will be moved from ${LocalFilesLocation} for ${RcloneUploadRemoteName} ***"
fi
RcloneMountLocation="$RcloneMountShare/$RcloneRemoteName" # Location of rclone mount
####### create directory for script files #######
mkdir -p /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName #for script files
####### Check if script already running ##########
echo "$(date "+%d.%m.%Y %T") INFO: *** Starting rclone_upload script for ${RcloneUploadRemoteName} ***"
if [[ -f "/mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running" ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: Exiting as script already running."
exit
else
echo "$(date "+%d.%m.%Y %T") INFO: Script not running - proceeding."
touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running
fi
####### check if rclone installed ##########
echo "$(date "+%d.%m.%Y %T") INFO: Checking if rclone installed successfully."
if [[ -f "$RcloneMountLocation/mountcheck" ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: rclone installed successfully - proceeding with upload."
else
echo "$(date "+%d.%m.%Y %T") INFO: rclone not installed - will try again later."
rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running
exit
fi
####### Rotating serviceaccount.json file if using Service Accounts #######
if [[ $UseServiceAccountUpload == 'Y' ]]; then
cd /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/
CounterNumber=$(find -name 'counter*' | cut -c 11,12)
CounterCheck="1"
if [[ "$CounterNumber" -ge "$CounterCheck" ]];then
echo "$(date "+%d.%m.%Y %T") INFO: Counter file found for ${RcloneUploadRemoteName}."
else
echo "$(date "+%d.%m.%Y %T") INFO: No counter file found for ${RcloneUploadRemoteName}. Creating counter_1."
touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_1
CounterNumber="1"
fi
ServiceAccount="--drive-service-account-file=$ServiceAccountDirectory/$ServiceAccountFile$CounterNumber.json"
echo "$(date "+%d.%m.%Y %T") INFO: Adjusted service_account_file for upload remote ${RcloneUploadRemoteName} to ${ServiceAccountFile}${CounterNumber}.json based on counter ${CounterNumber}."
else
echo "$(date "+%d.%m.%Y %T") INFO: Uploading using upload remote ${RcloneUploadRemoteName}"
ServiceAccount=""
fi
####### Upload files ##########
# Check bind option
if [[ $CreateBindMount == 'Y' ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: *** Checking if IP address ${RCloneMountIP} already created for upload to remote ${RcloneUploadRemoteName}"
ping -q -c2 $RCloneMountIP > /dev/null # -q quiet, -c number of pings to perform
if [ $? -eq 0 ]; then # ping returns exit status 0 if successful
echo "$(date "+%d.%m.%Y %T") INFO: *** IP address ${RCloneMountIP} already created for upload to remote ${RcloneUploadRemoteName}"
else
echo "$(date "+%d.%m.%Y %T") INFO: *** Creating IP address ${RCloneMountIP} for upload to remote ${RcloneUploadRemoteName}"
ip addr add $RCloneMountIP/24 dev $NetworkAdapter label $NetworkAdapter:$VirtualIPNumber
fi
else
RCloneMountIP=""
fi
# Remove --delete-empty-src-dirs if rclone sync or copy
if [[ $RcloneCommand == 'move' ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: *** Using rclone move - will add --delete-empty-src-dirs to upload."
DeleteEmpty="--delete-empty-src-dirs "
else
echo "$(date "+%d.%m.%Y %T") INFO: *** Not using rclone move - will remove --delete-empty-src-dirs to upload."
DeleteEmpty=""
fi
# Check --backup-directory
if [[ $BackupJob == 'Y' ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: *** Will backup to ${BackupRemoteLocation} and use ${BackupRemoteDeletedLocation} as --backup-directory with ${BackupRetention} retention for ${RcloneUploadRemoteName}."
LocalFilesLocation="$LocalFilesShare"
BackupDir="--backup-dir $RcloneUploadRemoteName:$BackupRemoteDeletedLocation"
else
BackupRemoteLocation=""
BackupRemoteDeletedLocation=""
BackupRetention=""
BackupDir=""
fi
# process files
rclone $RcloneCommand $LocalFilesLocation $RcloneUploadRemoteName:$BackupRemoteLocation $ServiceAccount $BackupDir \
--user-agent="$RcloneUploadRemoteName" \
-v \ #output lesser stuff
--buffer-size 512M \
--drive-chunk-size 512M \
--tpslimit 8 \
--checkers 8 \
--transfers 4 \
--order-by modtime,$ModSort \
--min-age $MinimumAge \
$Command1 $Command2 $Command3 $Command4 $Command5 $Command6 $Command7 $Command8 \
--exclude *fuse_hidden* \
--exclude *_HIDDEN \
--exclude .recycle** \
--exclude .Recycle.Bin/** \
--exclude *.backup~* \
--exclude *.partial~* \
--drive-stop-on-upload-limit \
--bwlimit "${BWLimit1Time},${BWLimit1} ${BWLimit2Time},${BWLimit2} ${BWLimit3Time},${BWLimit3}" \
--bind=$RCloneMountIP $DeleteEmpty
# Delete old files from mount
if [[ $UseBackupDirectory == 'Y' ]]; then
echo "$(date "+%d.%m.%Y %T") INFO: *** Removing files older than ${BackupRetention} from ${BackupDirectoryLocation} for ${RcloneUploadRemoteName}."
rclone delete --min-age $BackupRetention $RcloneUploadRemoteName:$BackupRemoteDeletedLocation
fi
####### Remove Control Files ##########
# update counter and remove other control files
if [[ $UseServiceAccountUpload == 'Y' ]]; then
if [[ "$CounterNumber" == "$CountServiceAccounts" ]];then
rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_*
touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_1
echo "$(date "+%d.%m.%Y %T") INFO: Final counter used - resetting loop and created counter_1."
else
rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_*
CounterNumber=$((CounterNumber+1))
touch /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/counter_$CounterNumber
echo "$(date "+%d.%m.%Y %T") INFO: Created counter_${CounterNumber} for next upload run."
fi
else
echo "$(date "+%d.%m.%Y %T") INFO: Not utilising service accounts."
fi
# remove dummy file
rm /mnt/user/appdata/other/rclone/remotes/$RcloneUploadRemoteName/upload_running
echo "$(date "+%d.%m.%Y %T") INFO: Script complete"
exit