Aria2在Debian11下的配置

一、aria2的安装

(一)安装aria2命令:

1
2
3
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install aria2

(二)配置aria2命令:

1、设置aria2c配置文件

sudo vim /root/aria2/aria2.conf

输入以下基本配置:

1
2
3
4
5
6
7
8
9
10
11
12
dir=~/下载/aria2Download 
disable-ipv6=true
enable-rpc=true
rpc-allow-origin-all=true
rpc-listen-all=true
rpc-listen-port=9800
rpc-secret=123456
continue=true
input-file=~/文档/aria2/aria2.session
save-session=~/文档/aria2/aria2.session
max-concurrent-downloads=5
max-connection-per-server=16

其中 dir 为下载目录;
rpc-listen-port 为监听端口,随后配置 WebUI 时需要用到,可以任意设置;
rpc-secret 为口令,同样用于 WebUI 中;
input-filesave-session 分别用于读取和保存断点续传信息。
在第一次安装后需要手动创建这个自动保存文件。

2、设置aria2c开机启动

方法一:

保存好之后,我们打开 /etc/rc.local ,在 exit 0 之前添加一行命令用于开机启动 Aria2-D参数后台运行):

(1)手工添加一个 /etc/rc.local 文件

因为debian11没有这项目录需要新建并运行,参考以下内容:
由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local文件,
但是Debian 9默认不带/etc/rc.local文件,而rc.local服务却还是自带的

1
sudo cat /lib/systemd/system/rc.local.service` 

//查看rc.local服务,会显示存在

1
sudo systemctl status rc-local`   

//查询状态看到默认关闭的

为了解决这个问题,我们需要手工添加一个 /etc/rc.local 文件,输入命令:

1
sudo cat <<EOF >/etc/rc.local

输入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/usr/bin/aria2c --conf-path=/root/aria2/aria2.conf -D

exit 0
EOF

(2)保存后,赋权限,命令:

# chmod +x /etc/rc.local

(3)接着启动 rc-local 服务

# systemctl start rc-local //启动

# systemctl status rc-local //查询状态

以上内容参考:[Debian 9.x “stretch” 解决 /etc/rc.local 开机启动问题]:

方法二:

设置开机启动并通过脚本控制aria2c重启/停止/查看状态

(1)建立脚本文件,路径是/etc/init.d/aria2c

命令:
$ sudo vim /etc/init.d/aria2c
输入以下内容并保存[aria2配置的路径根据自己文件路径配置]:

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
#!/bin/sh
### BEGIN INIT INFO
# Provides: aria2
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Aria2 Downloader
### END INIT INFO

case "$1" in
start)

echo -n "已开启Aria2c"
aria2c --conf-path=/root/aria2/aria2.conf -D
;;
stop)

echo -n "已关闭Aria2c"
killall aria2c
;;
restart)

killall aria2c
sudo -u pi aria2c --conf-path=/root/aria2/aria2.conf -D
#sudo -u后面的是你正在使用的用户名,用debian的是pi(没改用户的话)
;;
esac
exit

(2)授权:

命令:
$ sudo chmod 755 /etc/init.d/aria2c

(3)将aria2c服务添加到开机启动

更新启动脚本命令:

1
sudo update-rc.d aria2c defaults

启动、停止、查询状态命令:
1
2
$ systemctl start/stop aria2c   ##启动/停止aria2c
$ systemctl status aria2c ##查询程序运行情况

方法三

1
sudo vim /lib/systemd/system/aria2@.service

输入以下内容:

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Aria2 Service for %i.
After=network.target

[Service]
#PIDFile=/run/aria2.pid
User=%i
ExecStart=/usr/bin/aria2c -c --conf-path=/home/%i/.aria2/aria2/aria2.conf # 注意使用自己的路径和文件名

[Install]
WantedBy=multi-user.target

然后输入命令:
1
2
sudo systemctl daemon-reload
sudo systemctl enable aria2@.service

即可开机启动。
其他命令:
user是登录的用户名
1
2
sudo systemctl start/stop aria2@user  ##启动/停止aria2
sudo systemctl status aria2@user ##查询程序运行情况

(三)安装aria2 WebUI

1、安装nodejsgit

命令:

1
sudo apt install nodejs npm

或者参照安装说明:安装 Node.js v16.x:
1
2
3
4
5
6
7
# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt-get install -y nodejs

2、 安装n模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ sudo npm install -g n
added 1 package, and audited 2 packages in 6s

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 7.20.3 -> 7.21.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.21.0
npm notice Run npm install -g npm@7.21.0 to update!
npm notice
$ sudo n stable
installing : node-v14.17.5
mkdir : /usr/local/n/versions/node/14.17.5
fetch : https://nodejs.org/dist/v14.17.5/node-v14.17.5-linux-x64.tar.xz
installed : v14.17.5 (with npm 6.14.14)

Note: the node command changed location and the old location may be remembered in your current shell.
old : /usr/bin/node
new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"

3、接着安装git

1
sudo apt install git`

4、新建WebUI网站目录

1
mkdir -p ~/www/html/nginx-web/aira2  

进入目录:

1
cd ~/www/html/nginx-web/aira2`

输入命令:

1
2
3
git clone https://github.com/ziahamza/webui-aria2.git
cd webui-aria2
mv * ..

文件下载下来并移动到aria2目录后,
输入命令:

1
2
cd ~/www/html/nginx-web/aira2
sudo node node-server.js

提示以下语句:
WebUI Aria2 Server is running on http://localhost:8888
[启动WebUI路径:~/www/html/nginx-web/aira2/node-server.js]

5、 完成安装

打开浏览器,输入以上网址:http://localhost:8888
设置—>连接设置—>PRC端口:6800—>密码令牌(可选):123456
安装完成。

二、aria2的配置

修改aria2.conf内容,输入以下命令:

1
sudo vim /opt/aria2/aria2.conf 

替换为以下内容:
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
##aria2.conf设置###
# 下载文件默认保存路径
dir=~/下载/aria2/

# 日志文件保存路径及级别
log=~/文档/aria2/aria2.log
log-level=error

# 从会话中读取任务
input-file=~/文档/aria2/aria2.session

# 在Aria2退出时保存`错误/未完成`的下载任务到会话文件
save-session=~/文档/aria2/aria2.session
#配置dht.dat文件存放位置
dht-file-path=~/文档/aria2/dht.dat
# 定时保存会话, 0为退出时才保存, 需1.16.1以上版本, 默认:0
save-session-interval=60
# 强制保存会话, 即使任务已经完成, 默认:false
# 较新的版本开启后会在任务完成后依然保留.aria2文件
force-save=true

# 下载选项
# 最大同时下载任务数, 运行时可修改, 默认:5
max-concurrent-downloads=5
# 断点续传
continue=true
# 整体下载速度限制, 运行时可修改, 默认:0
max-overall-download-limit=5M
# 单个任务下载速度限制, 默认:0
max-download-limit=10M
# 整体上传速度限制, 运行时可修改, 默认:0
max-overall-upload-limit=1K
# 单个任务上传速度限制, 默认:0
max-upload-limit=1K

# Http/FTP 选项
# 连接超时,建立链接后失效
connect-timeout=120
# 最小速度限制
# 当下载速度低于此选项设置的值(B/s) 时将会关闭连接. 0 表示不设置最小速度限制.
lowest-speed-limit=1K
# 同一服务器连接数, 添加时可指定, 默认:1,范围1-16
max-connection-per-server=15
# 文件未找到重试次数
# 设置为 0 将会禁用此选项
max-file-not-found=0
# 最小文件分片大小, 添加时可指定, 取值范围1M -1024M, 默认:20M
# 假定size=10M, 文件为20MiB 则使用两个来源下载; 文件为15MiB 则使用一个来源下载
min-split-size=10M
# 单个任务最大线程数, 添加时可指定, 默认:5
split=5
# 检查证书
check-certificate=false
# 禁用缓存
http-no-cache=true
# 磁盘缓存
# 如果设置为 0, 将禁用磁盘缓存. 此功能将下载的数据缓存在内存中
# 最多占用此选项设置的字节数.
disk-cache=32M

# FTP Specific Options
# BT/PT Setting
# 本地节点查找, PT需要禁用, 默认:false
bt-enable-lpd=false
#最大连接节点数
#设置每个BT下载的最大连接节点数. 0 表示不限制.
bt-max-peers=100
# 自动BT任务
follow-torrent=true
# 启用 DHT (IPv6)
# 启用 IPv6 DHT 功能. 如果种子设置为私有, 即使此选项设置为"是", aria2 也不会启用
enable-dht6=true
enable-dht=true
dht-listen-port=6881-6999

# 不检查已经下载的文件
# 不检查之前下载文件中每个分片的哈希值.
bt-seed-unverified=false
# 做种前检查文件哈希
# 如果设置为"是", 当使用 --check-integrity 选项完成哈希检查及文件完成后才继续做种. 如果您希望仅当文件损坏或未完成时检查文件, 请设置为"否". 此选项仅对 BT 下载有效
bt-hash-check-seed=false

# 保存上传的种子文件
rpc-save-upload-metadata=true
# 删除未选择的文件
# 当 BT 任务完成后删除未选择的文件. 要选择需要下载的文件, 请使用 --select-file选项.
# 如果没有选择, 则所有文件都默认为需要下载. 此选项会从磁盘上直接删除文件,
# 请谨慎使用此选项.
bt-remove-unselected-file=false

# 每个种子限速, 对少种的PT很有用, 默认:50K
bt-request-peer-speed-limit=2M
#做种时间
seed-time=0
#优先下载
bt-prioritize-piece=head=10M
# 当种子的分享率达到这个数时, 自动停止做种, 0为一直做种, 默认:1.0
seed-ratio=1.0

# Metalink Specific Options

# RPC Options
# 启用RPC, 默认:false
enable-rpc=true
# 种子文件下载完后暂停
pause-metadata=false
#种子交换, PT需要禁用, 默认:true
enable-peer-exchange=false
# 允许所有来源, 默认:false
rpc-allow-origin-all=true
# 允许非外部访问, 默认:false
rpc-listen-all=true
#保存上传的种子文件
rpc-save-upload-metadata=true
# 启用加密后 RPC 服务需要使用 https 或者 wss 协议连接
rpc-secure=false

# Advanced Options
#启用后台进程
daemon=true
# 禁用IPV6,默认:false
disable-ipv6=true
# 启用 MMap
# 内存中存放映射文件. 当文件空间没有预先分配至, 此选项无效. 参见 --file-allocation.
enable-mmap=true
# 文件分配方法
# "prealloc"会在下载开始前预先分配空间.
# ext4 (带扩展支持), btrfs, xfs 或 NTFS (仅 MinGW 构建), "falloc" 是最好的选择
file-allocation=falloc
#最多下载结果 下载结果包括已完成/错误/已删除的下载
max-download-result=30
# 文件分配限制
# 不对比此参数设置大小小的分配文件. 您可以增加数值的单位 K 或 M
#no-file-allocation-limit=32M
# 强制顺序
force-sequential=true
# 启用参数化 URI 支持
# 您可以指定部分的集合: http://{sv1,sv2,sv3}/foo.iso.
# 同时您也可以使用步进计数器指定数字化的序列: http://host/image[000-100:2].img.
# [需使用 -z 选项]
# 步进计数器可以省略.
parameterized-uri=true
#延迟加载
deferred-input=true
# bt-tracker=BT服务器(多个服务器之间用,分开)
bt-tracker=udp://tracker.coppersurfer.tk:6969/announc,udp://tracker.leechers-paradise.org:6969/announc,udp://tracker.opentrackr.org:1337/announc,udp://p4p.arenabg.com:1337/announc,udp://9.rarbg.to:2710/announc,udp://9.rarbg.me:2710/announc,udp://exodus.desync.com:6969/announc,udp://tracker.pomf.se:80/announc,udp://tracker.openbittorrent.com:80/announc,udp://tracker.tiny-vps.com:6969/announc,udp://open.stealth.si:80/announc,udp://denis.stalker.upeer.me:6969/announc,udp://tracker.torrent.eu.org:451/announc,udp://tracker.moeking.me:6969/announc,udp://tracker.cyberia.is:6969/announc,udp://ipv4.tracker.harry.lu:80/announc,udp://tracker3.itzmx.com:6961/announc,udp://open.demonii.si:1337/announc,udp://explodie.org:6969/announc,udp://valakas.rollo.dnsabr.com:2710/announe

三、一些脚本处理

1、新建tracker.sh更新脚本

更新配置文件aria2.conf里的tracker.

输入命令:

1
sudo vim /opt/aria2/tracker.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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 P3TERX <https://p3terx.com>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# https://github.com/P3TERX/aria2.conf
# File name:tracker.sh
# Description: Get BT trackers and add to Aria2
# Version: 3.0
#
# BT tracker is provided by the following project.
# https://github.com/XIU2/TrackersListCollection
#
# Fallback URLs provided by jsDelivr
# https://www.jsdelivr.com
#

RED_FONT_PREFIX="\033[31m"
GREEN_FONT_PREFIX="\033[32m"
YELLOW_FONT_PREFIX="\033[1;33m"
LIGHT_PURPLE_FONT_PREFIX="\033[1;35m"
FONT_COLOR_SUFFIX="\033[0m"
INFO="[${GREEN_FONT_PREFIX}INFO${FONT_COLOR_SUFFIX}]"
ERROR="[${RED_FONT_PREFIX}ERROR${FONT_COLOR_SUFFIX}]"
ARIA2_CONF=${1:-aria2.conf}
DOWNLOADER="curl -fsSL --connect-timeout 3 --max-time 3 --retry 2"

DATE_TIME() {
date +"%m/%d %H:%M:%S"
}

GET_TRACKERS() {
echo && echo -e "$(DATE_TIME) ${INFO} Get BT trackers ..."
if [[ -z "${CUSTOM_TRACKER_URL}" ]]; then
TRACKER=$(
${DOWNLOADER} https://trackerslist.com/all_aria2.txt ||
${DOWNLOADER} https://cdn.jsdelivr.net/gh/XIU2/TrackersListCollection@master/all_aria2.txt ||
${DOWNLOADER} https://trackers.p3terx.com/all_aria2.txt
)
else
TRACKER=$(${DOWNLOADER} ${CUSTOM_TRACKER_URL} | awk NF | sed ":a;N;s/\n/,/g;ta")
fi
[[ -z "${TRACKER}" ]] && {
echo
echo -e "$(DATE_TIME) ${ERROR} Unable to get trackers, network failure or invalid links." && exit 1
}
}

ECHO_TRACKERS() {
echo -e "
--------------------[BitTorrent Trackers]--------------------
${TRACKER}
--------------------[BitTorrent Trackers]--------------------
"
}

ADD_TRACKERS() {
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to Aria2 configuration file ${LIGHT_PURPLE_FONT_PREFIX}${ARIA2_CONF}${FONT_COLOR_SUFFIX} ..." && echo
if [ ! -f ${ARIA2_CONF} ]; then
echo -e "$(DATE_TIME) ${ERROR} '${ARIA2_CONF}' does not exist."
exit 1
else
[ -z $(grep "bt-tracker=" ${ARIA2_CONF}) ] && echo "bt-tracker=" >>${ARIA2_CONF}
sed -i "s@^\(bt-tracker=\).*@\1${TRACKER}@" ${ARIA2_CONF} && echo -e "$(DATE_TIME) ${INFO} BT trackers successfully added to Aria2 configuration file !"
fi
}

ADD_TRACKERS_RPC() {
if [[ "${RPC_SECRET}" ]]; then
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.changeGlobalOption","id":"P3TERX","params":["token:'${RPC_SECRET}'",{"bt-tracker":"'${TRACKER}'"}]}'
else
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.changeGlobalOption","id":"P3TERX","params":[{"bt-tracker":"'${TRACKER}'"}]}'
fi
curl "${RPC_ADDRESS}" -fsSd "${RPC_PAYLOAD}" || curl "https://${RPC_ADDRESS}" -kfsSd "${RPC_PAYLOAD}"
}

ADD_TRACKERS_RPC_STATUS() {
RPC_RESULT=$(ADD_TRACKERS_RPC)
[[ $(echo ${RPC_RESULT} | grep OK) ]] &&
echo -e "$(DATE_TIME) ${INFO} BT trackers successfully added to Aria2 !" ||
echo -e "$(DATE_TIME) ${ERROR} Network failure or Aria2 RPC interface error!"
}

ADD_TRACKERS_REMOTE_RPC() {
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to remote Aria2: ${LIGHT_PURPLE_FONT_PREFIX}${RPC_ADDRESS%/*}${FONT_COLOR_SUFFIX} ..." && echo
ADD_TRACKERS_RPC_STATUS
}

ADD_TRACKERS_LOCAL_RPC() {
if [ ! -f ${ARIA2_CONF} ]; then
echo -e "$(DATE_TIME) ${ERROR} '${ARIA2_CONF}' does not exist."
exit 1
else
RPC_PORT=$(grep ^rpc-listen-port ${ARIA2_CONF} | cut -d= -f2-)
RPC_SECRET=$(grep ^rpc-secret ${ARIA2_CONF} | cut -d= -f2-)
[[ ${RPC_PORT} ]] || {
echo -e "$(DATE_TIME) ${ERROR} Aria2 configuration file incomplete."
exit 1
}
RPC_ADDRESS="localhost:${RPC_PORT}/jsonrpc"
echo -e "$(DATE_TIME) ${INFO} Adding BT trackers to Aria2 ..." && echo
ADD_TRACKERS_RPC_STATUS
fi
}

[ $(command -v curl) ] || {
echo -e "$(DATE_TIME) ${ERROR} curl is not installed."
exit 1
}

if [ "$1" = "cat" ]; then
GET_TRACKERS
ECHO_TRACKERS
elif [ "$1" = "RPC" ]; then
RPC_ADDRESS="$2/jsonrpc"
RPC_SECRET="$3"
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS_REMOTE_RPC
elif [ "$2" = "RPC" ]; then
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS
echo
ADD_TRACKERS_LOCAL_RPC
else
GET_TRACKERS
ECHO_TRACKERS
ADD_TRACKERS
fi

exit 0

2、新建clean.sh更新脚本

Aria2下载完成后删除冗余文件.

输入命令:

1
sudo vim /opt/aria2/clean.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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 P3TERX <https://p3terx.com>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# https://github.com/P3TERX/aria2.conf
# File name:clean.sh
# Description: Remove redundant files after Aria2 download is complete
# Version: 3.0
#

CHECK_CORE_FILE() {
CORE_FILE="$(dirname $0)/core"
if [[ -f "${CORE_FILE}" ]]; then
. "${CORE_FILE}"
else
echo "!!! core file does not exist !!!"
exit 1
fi
}

CHECK_CORE_FILE "$@"
CHECK_PARAMETER "$@"
CHECK_FILE_NUM
CHECK_SCRIPT_CONF
GET_TASK_INFO
GET_DOWNLOAD_DIR
CONVERSION_PATH
CLEAN_UP
exit 0

3、新建delete.sh更新脚本

Aria2下载错误或任务删除后删除文件.

输入命令:

1
sudo vim /opt/aria2/delete.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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 P3TERX <https://p3terx.com>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# https://github.com/P3TERX/aria2.conf
# File name:delete.sh
# Description: Delete files after Aria2 download error or task removed
# Version: 3.0
#

CHECK_CORE_FILE() {
CORE_FILE="$(dirname $0)/core"
if [[ -f "${CORE_FILE}" ]]; then
. "${CORE_FILE}"
else
echo "!!! core file does not exist !!!"
exit 1
fi
}

CHECK_RPC_CONECTION() {
READ_ARIA2_CONF
if [[ "${RPC_SECRET}" ]]; then
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.getVersion","id":"P3TERX","params":["token:'${RPC_SECRET}'"]}'
else
RPC_PAYLOAD='{"jsonrpc":"2.0","method":"aria2.getVersion","id":"P3TERX"}'
fi
(curl "${RPC_ADDRESS}" -fsSd "${RPC_PAYLOAD}" || curl "https://${RPC_ADDRESS}" -kfsSd "${RPC_PAYLOAD}") >/dev/null
}

DELETE_ON_STOP() {
if [[ "${TASK_STATUS}" = "error" && "${DELETE_ON_ERROR}" = "true" ]] || [[ "${TASK_STATUS}" = "removed" && "${DELETE_ON_REMOVED}" = "true" ]]; then
if [[ -f "${TASK_PATH}.aria2" ]]; then
echo -e "$(DATE_TIME) ${INFO} Download task ${TASK_STATUS}, deleting files..."
rm -vrf "${TASK_PATH}.aria2" "${TASK_PATH}"
else
[[ -e "${TASK_PATH}" ]] &&
echo -e "$(DATE_TIME) ${WARRING} Skip delete. Download completed files: ${TASK_PATH}" ||
echo -e "$(DATE_TIME) ${WARRING} Skip delete. File does not exist: ${TASK_PATH}"
fi
else
echo -e "$(DATE_TIME) ${WARRING} Skip delete. Task status invalid: ${TASK_STATUS}"
fi
}

DELETE_ON_UNKNOWN() {
if [[ -f "${FILE_PATH}.aria2" ]]; then
echo -e "$(DATE_TIME) ${INFO} Download task force removed, deleting files..."
rm -vrf "${FILE_PATH}.aria2" "${FILE_PATH}"
else
[[ -e "${FILE_PATH}" ]] &&
echo -e "$(DATE_TIME) ${WARRING} Skip delete. Download completed files: ${FILE_PATH}" ||
echo -e "$(DATE_TIME) ${WARRING} Skip delete. File does not exist: ${FILE_PATH}"
fi
}

DELETE_FILE() {
if GET_TASK_INFO; then
GET_DOWNLOAD_DIR
GET_TASK_STATUS
CONVERSION_PATH
DELETE_ON_STOP
DELETE_DOT_TORRENT
DELETE_EMPTY_DIR
elif CHECK_RPC_CONECTION && [[ "${DELETE_ON_UNKNOWN}" = "true" && ${FILE_NUM} -eq 1 ]]; then
DELETE_ON_UNKNOWN
else
echo -e "$(DATE_TIME) ${ERROR} Aria2 RPC interface error!"
exit 1
fi
}

CHECK_CORE_FILE "$@"
CHECK_PARAMETER "$@"
CHECK_FILE_NUM
CHECK_SCRIPT_CONF
DELETE_FILE
exit 0

4、新建move.sh更新脚本

Aria2 下载完成后移动文件.

输入命令:

1
sudo vim /opt/aria2/move.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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 P3TERX <https://p3terx.com>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# https://github.com/P3TERX/aria2.conf
# File name:move.sh
# Description: Move files after Aria2 download is complete
# Version: 3.0
#

CHECK_CORE_FILE() {
CORE_FILE="$(dirname $0)/core"
if [[ -f "${CORE_FILE}" ]]; then
. "${CORE_FILE}"
else
echo "!!! core file does not exist !!!"
exit 1
fi
}

TASK_INFO() {
echo -e "
-------------------------- [${YELLOW_FONT_PREFIX}Task Infomation${FONT_COLOR_SUFFIX}] --------------------------
${LIGHT_PURPLE_FONT_PREFIX}Task GID:${FONT_COLOR_SUFFIX} ${TASK_GID}
${LIGHT_PURPLE_FONT_PREFIX}Number of Files:${FONT_COLOR_SUFFIX} ${FILE_NUM}
${LIGHT_PURPLE_FONT_PREFIX}First File Path:${FONT_COLOR_SUFFIX} ${FILE_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Task File Name:${FONT_COLOR_SUFFIX} ${TASK_FILE_NAME}
${LIGHT_PURPLE_FONT_PREFIX}Task Path:${FONT_COLOR_SUFFIX} ${TASK_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Aria2 Download Directory:${FONT_COLOR_SUFFIX} ${ARIA2_DOWNLOAD_DIR}
${LIGHT_PURPLE_FONT_PREFIX}Custom Download Directory:${FONT_COLOR_SUFFIX} ${DOWNLOAD_DIR}
${LIGHT_PURPLE_FONT_PREFIX}Source Path:${FONT_COLOR_SUFFIX} ${SOURCE_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Destination Path:${FONT_COLOR_SUFFIX} ${DEST_PATH}
${LIGHT_PURPLE_FONT_PREFIX}.aria2 File Path:${FONT_COLOR_SUFFIX} ${DOT_ARIA2_FILE}
-------------------------- [${YELLOW_FONT_PREFIX}Task Infomation${FONT_COLOR_SUFFIX}] --------------------------
"
}

OUTPUT_MOVE_LOG() {
LOG="${MOVE_LOG}"
LOG_PATH="${MOVE_LOG_PATH}"
OUTPUT_LOG
}

DEFINITION_PATH() {
SOURCE_PATH="${TASK_PATH}"
if [[ "${DOWNLOAD_DIR}" != "${ARIA2_DOWNLOAD_DIR}" && "${DOWNLOAD_DIR}" =~ "${ARIA2_DOWNLOAD_DIR}" ]]; then
DEST_PATH="${DEST_DIR}${DEST_PATH_SUFFIX%/*}"
else
DEST_PATH="${DEST_DIR}"
fi
}

MOVE_FILE() {
echo -e "$(DATE_TIME) ${INFO} Start move files ..."
TASK_INFO
mkdir -p "${DEST_PATH}"
mv -vf "${SOURCE_PATH}" "${DEST_PATH}"
MOVE_EXIT_CODE=$?
if [ ${MOVE_EXIT_CODE} -eq 0 ]; then
MOVE_LOG="$(DATE_TIME) ${INFO} Move done: ${SOURCE_PATH} -> ${DEST_PATH}"
else
MOVE_LOG="$(DATE_TIME) ${ERROR} Move failed: ${SOURCE_PATH}"
fi
OUTPUT_MOVE_LOG
DELETE_EMPTY_DIR
}

CHECK_CORE_FILE "$@"
CHECK_PARAMETER "$@"
CHECK_FILE_NUM
CHECK_SCRIPT_CONF
GET_TASK_INFO
GET_DOWNLOAD_DIR
CONVERSION_PATH
DEFINITION_PATH
CLEAN_UP
MOVE_FILE
exit 0

5、新建upload.sh脚本

Aria2下载完成后使用Rclone上传文件.
输入命令:

1
sudo vim /opt/aria2/upload.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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2020 P3TERX <https://p3terx.com>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# https://github.com/P3TERX/aria2.conf
# File name:upload.sh
# Description: Use Rclone to upload files after Aria2 download is complete
# Version: 3.1
#

CHECK_CORE_FILE() {
CORE_FILE="$(dirname $0)/core"
if [[ -f "${CORE_FILE}" ]]; then
. "${CORE_FILE}"
else
echo && echo "!!! core file does not exist !!!"
exit 1
fi
}

CHECK_RCLONE() {
[[ $# -eq 0 ]] && {
echo && echo -e "Checking RCLONE connection ..."
rclone mkdir "${DRIVE_NAME}:${DRIVE_DIR}/P3TERX.COM"
if [[ $? -eq 0 ]]; then
rclone rmdir "${DRIVE_NAME}:${DRIVE_DIR}/P3TERX.COM"
echo
echo -e "${LIGHT_GREEN_FONT_PREFIX}success${FONT_COLOR_SUFFIX}"
exit 0
else
echo
echo -e "${RED_FONT_PREFIX}failure${FONT_COLOR_SUFFIX}"
exit 1
fi
}
}

TASK_INFO() {
echo -e "
-------------------------- [${YELLOW_FONT_PREFIX}Task Infomation${FONT_COLOR_SUFFIX}] --------------------------
${LIGHT_PURPLE_FONT_PREFIX}Task GID:${FONT_COLOR_SUFFIX} ${TASK_GID}
${LIGHT_PURPLE_FONT_PREFIX}Number of Files:${FONT_COLOR_SUFFIX} ${FILE_NUM}
${LIGHT_PURPLE_FONT_PREFIX}First File Path:${FONT_COLOR_SUFFIX} ${FILE_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Task File Name:${FONT_COLOR_SUFFIX} ${TASK_FILE_NAME}
${LIGHT_PURPLE_FONT_PREFIX}Task Path:${FONT_COLOR_SUFFIX} ${TASK_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Aria2 Download Directory:${FONT_COLOR_SUFFIX} ${ARIA2_DOWNLOAD_DIR}
${LIGHT_PURPLE_FONT_PREFIX}Custom Download Directory:${FONT_COLOR_SUFFIX} ${DOWNLOAD_DIR}
${LIGHT_PURPLE_FONT_PREFIX}Local Path:${FONT_COLOR_SUFFIX} ${LOCAL_PATH}
${LIGHT_PURPLE_FONT_PREFIX}Remote Path:${FONT_COLOR_SUFFIX} ${REMOTE_PATH}
${LIGHT_PURPLE_FONT_PREFIX}.aria2 File Path:${FONT_COLOR_SUFFIX} ${DOT_ARIA2_FILE}
-------------------------- [${YELLOW_FONT_PREFIX}Task Infomation${FONT_COLOR_SUFFIX}] --------------------------
"
}

OUTPUT_UPLOAD_LOG() {
LOG="${UPLOAD_LOG}"
LOG_PATH="${UPLOAD_LOG_PATH}"
OUTPUT_LOG
}

DEFINITION_PATH() {
LOCAL_PATH="${TASK_PATH}"
if [[ -f "${TASK_PATH}" ]]; then
REMOTE_PATH="${DRIVE_NAME}:${DRIVE_DIR}${DEST_PATH_SUFFIX%/*}"
else
REMOTE_PATH="${DRIVE_NAME}:${DRIVE_DIR}${DEST_PATH_SUFFIX}"
fi
}

LOAD_RCLONE_ENV() {
RCLONE_ENV_FILE="${ARIA2_CONF_DIR}/rclone.env"
[[ -f ${RCLONE_ENV_FILE} ]] && export $(grep -Ev "^#|^$" ${RCLONE_ENV_FILE} | xargs -0)
}

UPLOAD_FILE() {
echo -e "$(DATE_TIME) ${INFO} Start upload files..."
TASK_INFO
RETRY=0
RETRY_NUM=3
while [ ${RETRY} -le ${RETRY_NUM} ]; do
[ ${RETRY} != 0 ] && (
echo
echo -e "$(DATE_TIME) ${ERROR} Upload failed! Retry ${RETRY}/${RETRY_NUM} ..."
echo
)
rclone move -v "${LOCAL_PATH}" "${REMOTE_PATH}"
RCLONE_EXIT_CODE=$?
if [ ${RCLONE_EXIT_CODE} -eq 0 ]; then
UPLOAD_LOG="$(DATE_TIME) ${INFO} Upload done: ${LOCAL_PATH} -> ${REMOTE_PATH}"
OUTPUT_UPLOAD_LOG
DELETE_EMPTY_DIR
break
else
RETRY=$((${RETRY} + 1))
[ ${RETRY} -gt ${RETRY_NUM} ] && (
echo
UPLOAD_LOG="$(DATE_TIME) ${ERROR} Upload failed: ${LOCAL_PATH}"
OUTPUT_UPLOAD_LOG
)
sleep 3
fi
done
}

CHECK_CORE_FILE "$@"
CHECK_SCRIPT_CONF
CHECK_RCLONE "$@"
CHECK_FILE_NUM
GET_TASK_INFO
GET_DOWNLOAD_DIR
CONVERSION_PATH
DEFINITION_PATH
CLEAN_UP
LOAD_RCLONE_ENV
UPLOAD_FILE
exit 0

6、定时更新tracker信息

终端输入命令:

1
sudo crontab -e`  

添加以下内容:
1
0 9 * * * /opt/aria2/tracker.sh  // 每天9点执行更新脚本

参考资料:

1、Ubuntu/Debian系统安装配置aria2和WebUI

2、Debian 9.x “stretch” 解决 /etc/rc.local 开机启动问题]:

3、一键安装及配置开源项目地址:

4、Debian上Crontab的使用