-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathon-complete.sh
153 lines (138 loc) · 5.39 KB
/
on-complete.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Aria2 Autoupload.sh by P3TERX, adapt Heroku-AriaNG by xinxin8816 and Kwok1am.
# Limit the minimum upload size, which is only valid when downloading multiple BT files, and is used to filter useless files. Files below this size will be deleted and will not be uploaded.
#MIN_SIZE=10m
# RCLONE Configuration file path
export RCLONE_CONFIG=rclone.conf
# RCLONE The size of the block, the default is 5M. Theoretically, the larger the upload speed, the faster it will occupy more memory. If the setting is too large, the process may be interrupted.
export RCLONE_CACHE_CHUNK_SIZE=3M
# RCLONE Upload failure retry wait time, the default is disabled, unit s, m, h
export RCLONE_RETRIES_SLEEP=30s
# RCLONE Abnormal exit retry count
RETRY_NUM=6
#============================================================
DOWNLOAD_PATH='downloads'
FILE_PATH=$3
REMOVE_DOWNLOAD_PATH=${FILE_PATH#${DOWNLOAD_PATH}/}
TOP_PATH=${DOWNLOAD_PATH}/${REMOVE_DOWNLOAD_PATH%%/*}
INFO="[INFO]"
ERROR="[ERROR]"
WARRING="[WARRING]"
TASK_INFO() {
echo -e "
-------------------------- [TASK INFO] --------------------------
Download path: ${DOWNLOAD_PATH}
File path: ${FILE_PATH}
Upload path: ${UPLOAD_PATH}
Remote path A: ${REMOTE_PATH}
Remote path B: ${REMOTE_PATH_2}
Remote path C: ${REMOTE_PATH_3}
Remote path D: ${REMOTE_PATH_4}
Remote path E: ${REMOTE_PATH_5}
-------------------------- [TASK INFO] --------------------------
"
}
CLEAN_UP() {
[[ -n ${MIN_SIZE} || -n ${INCLUDE_FILE} || -n ${EXCLUDE_FILE} ]] && echo -e "${INFO} Clean up excluded files ..."
[[ -n ${MIN_SIZE} ]] && rclone delete -v "${UPLOAD_PATH}" --max-size ${MIN_SIZE}
[[ -n ${INCLUDE_FILE} ]] && rclone delete -v "${UPLOAD_PATH}" --exclude "*.{${INCLUDE_FILE}}"
[[ -n ${EXCLUDE_FILE} ]] && rclone delete -v "${UPLOAD_PATH}" --include "*.{${EXCLUDE_FILE}}"
}
UPLOAD_FILE() {
RETRY=0
echo "$(($(cat numUpload)+4))" > numUpload # Plus 1
while [ ${RETRY} -le ${RETRY_NUM} ]; do
[ ${RETRY} != 0 ] && (
echo
echo -e "$(date +"%m/%d %H:%M:%S") ${ERROR} ${UPLOAD_PATH} Upload failed! Retry ${RETRY}/${RETRY_NUM} ..."
echo
)
rclone copy -v "${UPLOAD_PATH}" "${REMOTE_PATH}"
RCLONE_EXIT_CODE=$?
RCLONE_EXIT_CODE_2=0
RCLONE_EXIT_CODE_3=0
RCLONE_EXIT_CODE_4=0
RCLONE_EXIT_CODE_5=0
if [ -n "${RCLONE_DESTINATION_2}" ]; then
rclone copy -v "${UPLOAD_PATH}" "${REMOTE_PATH_2}"
RCLONE_EXIT_CODE_2=$?
fi
if [ -n "${RCLONE_DESTINATION_3}" ]; then
rclone copy -v "${UPLOAD_PATH}" "${REMOTE_PATH_3}"
RCLONE_EXIT_CODE_3=$?
fi
if [ -n "${RCLONE_DESTINATION_4}" ]; then
rclone copy -v "${UPLOAD_PATH}" "${REMOTE_PATH_4}"
RCLONE_EXIT_CODE_4=$?
fi
if [ -n "${RCLONE_DESTINATION_5}" ]; then
rclone copy -v "${UPLOAD_PATH}" "${REMOTE_PATH_5}"
RCLONE_EXIT_CODE_5=$?
fi
if [ ${RCLONE_EXIT_CODE} -eq 0 ] && [ ${RCLONE_EXIT_CODE_2} -eq 0 ] && [ ${RCLONE_EXIT_CODE_3} -eq 0 ] && [ ${RCLONE_EXIT_CODE_4} -eq 0 ] && [ ${RCLONE_EXIT_CODE_5} -eq 0 ]; then
[ -e "${DOT_ARIA2_FILE}" ] && rm -vf "${DOT_ARIA2_FILE}"
rclone rmdirs -v "${DOWNLOAD_PATH}" --leave-root
echo -e "$(date +"%m/%d %H:%M:%S") ${INFO} Upload done: ${UPLOAD_PATH}"
rclone delete -v "${UPLOAD_PATH}"
break
else
RETRY=$((${RETRY} + 4))
[ ${RETRY} -gt ${RETRY_NUM} ] && (
echo
echo -e "$(date +"%m/%d %H:%M:%S") ${ERROR} Upload failed: ${UPLOAD_PATH}"
echo
)
sleep 3
fi
done
echo "$(($(cat numUpload)-1))" > numUpload # Minus 1
}
UPLOAD() {
echo -e "$(date +"%m/%d %H:%M:%S") ${INFO} Start upload..."
TASK_INFO
UPLOAD_FILE
}
if [ -z $2 ]; then
echo && echo -e "${ERROR} This script can only be used by passing parameters through Aria2."
echo && echo -e "${WARRING} 直接运行此脚本可能导致无法开机!"
exit 1
elif [ $2 -eq 0 ]; then
exit 0
fi
if [ -e "${FILE_PATH}.aria2" ]; then
DOT_ARIA2_FILE="${FILE_PATH}.aria2"
elif [ -e "${TOP_PATH}.aria2" ]; then
DOT_ARIA2_FILE="${TOP_PATH}.aria2"
fi
if [ "${TOP_PATH}" = "${FILE_PATH}" ] && [ $2 -eq 1 ]; then
UPLOAD_PATH="${FILE_PATH}"
REMOTE_PATH="${RCLONE_DESTINATION}/"
REMOTE_PATH_2="${RCLONE_DESTINATION_2}/"
REMOTE_PATH_3="${RCLONE_DESTINATION_3}/"
REMOTE_PATH_4="${RCLONE_DESTINATION_4}/"
REMOTE_PATH_5="${RCLONE_DESTINATION_5}/"
UPLOAD
exit 0
elif [ "${TOP_PATH}" != "${FILE_PATH}" ] && [ $2 -gt 1 ]; then
UPLOAD_PATH="${TOP_PATH}"
REMOTE_PATH="${RCLONE_DESTINATION}/${REMOVE_DOWNLOAD_PATH%%/*}"
REMOTE_PATH_2="${RCLONE_DESTINATION_2}/${REMOVE_DOWNLOAD_PATH%%/*}"
REMOTE_PATH_3="${RCLONE_DESTINATION_3}/${REMOVE_DOWNLOAD_PATH%%/*}"
REMOTE_PATH_4="${RCLONE_DESTINATION_4}/${REMOVE_DOWNLOAD_PATH%%/*}"
REMOTE_PATH_5="${RCLONE_DESTINATION_5}/${REMOVE_DOWNLOAD_PATH%%/*}"
CLEAN_UP
UPLOAD
exit 0
elif [ "${TOP_PATH}" != "${FILE_PATH}" ] && [ $2 -eq 1 ]; then
UPLOAD_PATH="${FILE_PATH}"
REMOTE_PATH="${RCLONE_DESTINATION}/${REMOVE_DOWNLOAD_PATH%/*}"
REMOTE_PATH_2="${RCLONE_DESTINATION_2}/${REMOVE_DOWNLOAD_PATH%/*}"
REMOTE_PATH_3="${RCLONE_DESTINATION_3}/${REMOVE_DOWNLOAD_PATH%/*}"
REMOTE_PATH_4="${RCLONE_DESTINATION_4}/${REMOVE_DOWNLOAD_PATH%/*}"
REMOTE_PATH_5="${RCLONE_DESTINATION_5}/${REMOVE_DOWNLOAD_PATH%/*}"
UPLOAD
exit 0
fi
echo -e "${ERROR} Unknown error."
TASK_INFO
exit 1