基本配置(未特殊说明均为Ubuntu用户下操作)

创建基本目录

在mirror.s-o-r-a.eu.org下创建后端相应目录
目录说明

conf----配置文件
db---数据库文件
bin---主程序(TUNASYNC_WORKING_DIR)
scripts---脚本文件
logs---日志文件
mkdir -p ~/mirrors/tunasync/conf
mkdir -p ~/mirrors/tunasync/db
mkdir -p ~/mirrors/tunasync/bin
mkdir -p ~/mirrors/tunasync/scripts
mkdir -p ~/mirrors/tunasync/logs

查询当前用户名以及所在组

sora@sora-virtual-machine:~$ whoami
sora   #用户名
sora@sora-virtual-machine:~$ id -gn
sora   #用户所在组

更改~/mirrors/ 目录的所有权,其中ubuntu:ubuntu用户名:用户所在组 ,注意替换

sudo chown -R sora:sora ~/mirrors/

下载源码

GitHub:

GitHub - tuna/tunasync: Mirror job management tool.

Gitee:

weiwei20180921/tunasync

部署

将下载好的源码上传到服务器上tunasync-v0.9.3-linux-amd64-bin.tar.gz(注意自己服务器架构),解压到/home/sora/mirrors/tunasync/bin(路径中的sora为用户名,注意替换)

#上传到/home/sora/mirrors/tunasync/bin
cd /home/sora/mirrors/tunasync/bin
tar -xf /home/sora/tunasync-v0.9.3-linux-amd64-bin.tar.gz
rm -f /home/sora/tunasync-v0.9.3-linux-amd64-bin.tar.gz   #删除压缩包

配置环境变量

先备份,然后修改,最后source

#如果存在的话,不存在就在ubuntu目录下创建一个
cd
cp .bash_profile .bash_profile.bak   #存在则使用该命令备份
touch .bash_profile   #不存在则使用该命令创建
vi .bash_profile

.bash_profile中的内容

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
TUNAPATH=/home/sora/mirrors/tunasync

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$TUNAPATH/bin

export PATH

修改完成后执行命令

source .bash_profile

验证环境是否生效

 tunasync -v

能够正常输出版本等信息就可以了

sora@sora-virtual-machine:~$  tunasync -v
Version: 0.9.3
Git Hash: 528b799bc4f1a9d51f73976ec570a2fb056aad62
Build Date: 2025-02-28 15:29:23 +0800 CST

manager配置

创建/home/sora/mirrors/tunasync/conf/manager.conf

touch /home/sora/mirrors/tunasync/conf/manager.conf

manger.conf中填入以下内容

debug = false

[server]
addr = "127.0.0.1"
port = 14242
ssl_cert = ""
ssl_key = ""

[files]
db_type = "bolt"
db_file = "/home/sora/mirrors/tunasync/db/manager.db"
ca_cert = ""
  • port:监听端口,默认为14242,如果修改端口要对应修改tunasynctl 配置文件,一般在/etc/tunasync/ctl.conf或者 ~/.config/tunasync/ctl.conf两个位置,后者可以覆盖前者的配置值。参考Github文档
  • ssl_cert与ssl_key:ssl 的配置,不需要可以不配置。如果你 worker 和 manager 都是在本机,那么没必要使用 https。此时 manager 就不指定 ssl_keyssl_cert,留空;worker 的 ca_cert 留空,api_basehttp:// 开头。
  • db_file:数据库文件,目前放在/www/wwwroot/mirror.s-o-r-a.eu.org/mirrors/tunasync/db目录
  • dbtype:除了 bolt 以外,还支持 badger、leveldb 和 redis 的数据库后端。对于 badger 和 leveldb,只需要修改 db_type。如果使用 redis 作为数据库后端,把 db_type 改为 redis,并把下面的 db_file 设为 redis 服务器的地址: redis://user:password@host:port/db_number

worker的配置

创建/home/sora/mirrors/tunasync/conf/worker.conf

touch /home/sora/mirrors/tunasync/conf/worker.conf

在woker.conf中填入以下内容

[global]
name = "myworker"
log_dir = "/home/sora/mirrors/tunasync/log/{{.Name}}"
mirror_dir = "/home/sora/mirrors/tunasync/bin"
concurrent = 10
interval = 1440

[manager]
api_base = "http://localhost:14242"   # 这个端口是你前面manager.conf中配置的API端口
token = "some_token"
ca_cert = ""

[cgroup]
enable = false
base_path = "/sys/fs/cgroup"
group = "tunasync"

[server]
hostname = "localhost"
listen_addr = "127.0.0.1"
listen_port = 16010
ssl_cert = ""
ssl_key = ""

[[mirrors]]
name = "7-zip"
log_dir = "/home/sora/mirrors/tunasync/logs/7-zip/{{.Name}}"
mirror_dir = "/home/sora/mirrors/tunasync/bin/7-zip"
provider = "rsync"
upstream = "rsync://mirror.nju.edu.cn/7-zip/"
use_ipv6 = false          
  • [[mirrors]]是你要同步的镜像,根据相关内容填写
  • mirror_dir:这个是存放下载的镜像的目录,需要使用mkdir手动创建目录
  • global name:worker 进程名称,用于程序识别(worker_id)
  • log_dir:tunasync 的日志路径
  • mirror_dir:镜像下载地址
  • concurrent:并发线程数
  • interval:rsync 同步周期,以分钟为单位
  • api_base:manager 地址,注意,此处需与 manager 所在服务器配置保持一致
  • server listen_port:该 worker 自身的监听端口,如果同一台服务器上有多个 worker,各个 worker 的配置文件中务必配置不同端口
  • mirrors name:镜像名称,tunasync 会在镜像根目录下建立一个该名称的目录用于下载镜像
  • upstream:同步地址,注意,参数最后需要有/,否则启动 tunasync 时会报错

启动

启动 tunasync 需开启 manager 进程与 worker 进程,先启动 manager,后启动 worker。

开启manager服务

输入以下命令运行manager服务

tunasync manager --config /home/sora/mirrors/tunasync/conf/manager.conf >> /home/sora/mirrors/tunasync/logs/manager.log &

输入netstat -anp | grep tunasy命令查看是否运行成功

sora@sora-virtual-machine:~$ netstat -anp | grep tunasy
[1]+  退出 1                tunasync manager --config /home/sora/mirrors/tunasync/conf/manager.conf >> /home/sora/mirrors/tunasync/logs/manager.log
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)

如果运行失败,打开日志文件查看错误原因/home/sora/mirrors/tunasync/logs/manager.log

开启worker服务

tunasync worker --config /home/sora/mirrors/tunasync/conf/worker.conf >> /home/sora/mirrors/tunasync/logs/worker.log &

输入netstat -anp | grep tunasy命令查看是否运行成功

sora@sora-virtual-machine:~$ netstat -anp | grep tunasy
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
tcp        0      0 127.0.0.1:16010         0.0.0.0:*               LISTEN      148724/tunasync     
tcp        0      0 127.0.0.1:14242         0.0.0.0:*               LISTEN      70227/tunasync 

这里应该有两个进程,分别是manager和worker,如果有问题请前往/home/sora/mirrors/tunasync/logs中查看日志

安装启停脚本

脚本下载

把脚本放到/home/sora/mirrors/tunasync/bin

https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-worker

https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-manager

cd /home/sora/mirrors/tunasync/bin
wget -O tunasync-worker.sh https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-worker
wget -O tunasync-manager.sh https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-manager

提升脚本执行权限

chmod u+x tunasync-worker.sh
chmod u+x tunasync-manager.sh

修改脚本tunasync-manager.sh,修改的地方为9~12行

#!/bin/sh
# Provides:          tunasync-manager 
# Description:       Mirror job management tool.

# Author: whsir <[email protected]>

NAME=tunasync
TUNASYNC_NAME=tunasync-manager

TUNASYNC_BIN=/home/sora/mirrors/tunasync/bin/$NAME
OPTIONS="manager --config /home/sora/mirrors/tunasync/conf/manager.conf"

StatBin=/bin/ps

case "$1" in
    start)
        if $StatBin -ef | grep -v grep | grep -q "tunasync manager";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync manager" | awk -F" " '{print $2}'`
            echo "$TUNASYNC_NAME (pid $TUNASYNC_PID) already running."
        else
            $TUNASYNC_BIN $OPTIONS > /dev/null &
            echo "Starting $TUNASYNC_NAME... "
        fi
    ;;

    status)
        if $StatBin -ef | grep -v grep | grep -q "tunasync manager";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync manager" | awk -F" " '{print $2}'`
            echo "$TUNASYNC_NAME (pid $TUNASYNC_PID) is running."
        else
            echo "$TUNASYNC_NAME is not running."
        fi
    ;;

    stop)
        if $StatBin -ef | grep -v grep | grep -q "tunasync manager";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync manager" | awk -F" " '{print $2}'`
            kill $TUNASYNC_PID
            echo "Stop $TUNASYNC_NAME...."
        else
            echo "$TUNASYNC_NAME already stopping."
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
    ;;
esac

修改tunasync-worker.sh,修改的地方为9~12行

#!/bin/sh
# Provides:          tunasync-worker
# Description:       Mirror job management tool.

# Author: whsir <[email protected]>

NAME=tunasync
TUNASYNC_NAME=tunasync-worker

TUNASYNC_BIN=/home/sora/mirrors/tunasync/bin/$NAME
OPTIONS="worker --config /home/sora/mirrors/tunasync/conf/worker.conf"

StatBin=/bin/ps

case "$1" in
    start)
        if $StatBin -ef | grep -v grep | grep -q "tunasync worker";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync worker" | awk -F" " '{print $2}'`
            echo "$TUNASYNC_NAME (pid $TUNASYNC_PID) already running."
        else
            $TUNASYNC_BIN $OPTIONS > /dev/null &
            echo "Starting $TUNASYNC_NAME... "
        fi
    ;;

    status)
        if $StatBin -ef | grep -v grep | grep -q "tunasync worker";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync worker" | awk -F" " '{print $2}'`
            echo "$TUNASYNC_NAME (pid $TUNASYNC_PID) is running."
        else
            echo "$TUNASYNC_NAME is not running."
        fi
    ;;

    stop)
        if $StatBin -ef | grep -v grep | grep -q "tunasync worker";then
            TUNASYNC_PID=`ps -ef | grep -v grep | grep "tunasync worker" | awk -F" " '{print $2}'`
            kill $TUNASYNC_PID
            echo "Stop $TUNASYNC_NAME...."
        else
            echo "$TUNASYNC_NAME already stopping."
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
    ;;
esac

验证脚本

使用脚本暂停tunasync-manager服务

tunasync-manager.sh stop
Stop tunasync-manager....

使用脚本暂停tunasync-worker服务

tunasync-worker.sh stop
Stop tunasync-worker....

脚本命令

tunasync-manager.sh start
tunasync-manager.sh stop
tunasync-manager.sh status

tunasync-manager.sh为脚本的名字,注意替换
注:运行脚本前你需要先cd到脚本所在目录,即/home/sora/mirrors/tunasync/bin

设置脚本开机自启

编辑 crontab 文件,首次打开会询问你使用哪个编辑器,选一个自己顺手的就行

crontab -e

在文件末尾添加以下行:

@reboot /home/sora/mirrors/tunasync/bin/tunasync-manager.sh start
@reboot /home/sora/mirrors/tunasync/bin/tunasync-worker.sh start

检查当前用户设置的定时任务

crontab  -l

检查服务运行状态

启动manager和worker服务后可以通过netstat -anp | grep tunasy命令来检查服务是否启动成功

sora@sora-virtual-machine:~/mirrors/tunasync/bin$ netstat -anp | grep tunasy
(并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
tcp        0      0 127.0.0.1:16010         0.0.0.0:*               LISTEN      310710/tunasync     
tcp        0      0 127.0.0.1:14242         0.0.0.0:*               LISTEN      310491/tunasync     

如果一切正常你应该能够在/home/sora/mirrors/tunasync/bin目录下发现名为7-zip的目录,这个就是我们同步的第一个项目。我们在worker中设置了如下内容,在worker服务启动后聚会开始进行同步,同步后生成的文件在/home/sora/mirrors/tunasync/bin目录下,运行日志在/home/sora/mirrors/tunasync/logs目录下

[[mirrors]]
name = "7-zip"
log_dir = "/home/sora/mirrors/tunasync/logs/7-zip/{{.Name}}"
mirror_dir = "/home/sora/mirrors/tunasync/bin/7-zip"
provider = "rsync"
upstream = "rsync://mirror.nju.edu.cn/7-zip/"
use_ipv6 = false
interval = 15

查看同步状态

端口是manager的端口,默认为14242

tunasynctl list -p 14242 --all

输出同步镜像的详细信息

sora@sora-virtual-machine:~$ tunasynctl list -p 14242 --all
[
  {
    "name": "7-zip",
    "is_master": true,
    "status": "success",
    "last_update": "2025-04-01 17:11:35 +0800",
    "last_update_ts": 1743498695,
    "last_started": "2025-04-01 17:11:32 +0800",
    "last_started_ts": 1743498692,
    "last_ended": "2025-04-01 17:11:35 +0800",
    "last_ended_ts": 1743498695,
    "next_schedule": "2025-04-01 17:26:35 +0800",
    "next_schedule_ts": 1743499595,
    "upstream": "rsync://mirror.nju.edu.cn/7-zip/",
    "size": "322.97M"
  }
]

删除某worker的某镜像

删除命令是tunasynctl disable -w <worker_id> <mirror_name>,其中<worker_id>worker.conf中[Global]下的name;<mirror_name>worker.conf中[[mirrors]]下的name
例如我要删除上面创建的7-zip这个同步任务
首先去worker.conf中删除名为7-zip的[[mirrors]],保存worker.conf并热重载

tunasynctl reload -w myworker

接着从数据库中删除同步任务

tunasynctl disable -w myworker 7-zip
tunasynctl flush

最后删除存放镜像的文件夹,即/home/sora/mirrors/tunasync/logs/7-zip
查看当前同步状态,从输出中可以看到已经删除了7-zip的同步任务

sora@sora-virtual-machine:~$ tunasynctl list -p 14242 --all
[]

删除worker

此操作会直接删除整个worker.conf下的所有[[mirrors]](PS:一个worker下可以有多个mirror)

tunasynctl rm-worker -w <worker_id>

例如删除myworker

tunasynctl rm-worker -w myworker

参考文献

GitHub - tuna/mirror-web: Source code of the web interface of https://mirrors.tuna.tsinghua.edu.cn/

GitHub - tuna/tunasync: Mirror job management tool.

GitHub - tuna/tunasync-scripts: Custom scripts for mirror jobs