本文将叙述如何使用 tunasync 搭建一个 GitHub Releases 镜像站并提供 HTTP 下载服务。(只同步若干个指定仓库的 Released Assets )


前言

video-downloader-deploy 执行配置 youtube-dl, annie 等时需要从 GitHub Releases 下载文件,对于国内网络,下载速度可能极其缓慢甚至无法下载。此前采用的办法为:让用户手动下载文件并放入指定目录,然后继续配置流程。但这样会使得整体流程略显繁琐,一些用户甚至可能直接放弃。

更好的解决办法是通过镜像源,提供更高速度的直链下载。例如配置 Python-embed 时就是从 淘宝 NPM 镜像站的 Python 镜像 下载;而配置 you-get 时是从 清华开源镜像站的 PyPI 镜像 下载。

现有四种方法提供直链下载:

  1. GitHub raw.githubusercontent.com
  2. Coding {user}.coding.net/p/{proj}/d/{repo}/git/raw
  3. 用服务器自行搭建一个镜像站
  4. 使用付费 CDN 服务

使用 GitHub raw 的下载速度与从 Release 下载的速度相差无几;而鉴于 video-downloader-deploy 没有任何收益,不应当使用付费的 CDN 服务。虽然 Coding raw 的国内下载速度很快,也不像 Gitee 一样需要登录才能下载,但查阅其服务条款后发现,似乎没有涉及到使用 raw 功能提供下载服务的条款,暂时不敢滥用。总之自行搭建镜像站就成为了最终选择。

tunasync 是 清华大学 TUNA 镜像源 目前使用的镜像方案。

tunasync 提供多种可配置项,可以通过 rsync 或 shell/python 脚本完成多种同步任务。许多常见站点的同步脚本可以在 tuna/tunasync-scripts 中找到。


准备

系统环境

CentOS 7 x64, 1 vCore CPU, 1 GB RAM, 25 GB SSD.

由于仅需同步 GitHub Releases 上的 Assets 等一些小文件,并非 Linux 系统镜像之类的大文件,因此对系统配置和硬盘大小没有什么要求,一台基本 VPS 的配置即可。

安装依赖

本文以 CentOS 7 为例, Debian/Ubuntu 等其他系统大同小异,自行修改相关命令即可。简洁起见,下文所有命令均省略 sudo ,如遇权限问题,请自行添加。

1
2
$ yum install python3 python-pip
$ pip3 install --upgrade requests
注意:下文用到的 github-release.py 中使用了 “字符串插值(formatted string literals, f-strings)”,需要 Python 3.6 及以上的版本才能解析运行。

创建目录

除了 tunasync, tunasynctl 主程序放在 /usr/bin/ 目录下,服务脚本放在 /etc/init.d/ 目录下,其余所有相关文件均放在 /srv/tunasync/ 目录下。你可以自由选择目录,只需修改对应文件中的相关路径即可。

  • 程序路径:/usr/bin/
  • 服务脚本:/etc/init.d/tunasync-manager, /etc/init.d/tunasync-worker
  • 镜像路径:/srv/tunasync/
  • 配置文件:/srv/tunasync/data/conf/
  • 同步脚本:/srv/tunasync/data/scripts/
  • tunasync日志:/srv/tunasync/log/tunasync/
  • Nginx日志:/srv/tunasync/log/nginx/

整体目录结构为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ tree /srv/tunasync/
/srv/tunasync/
|-- web # css/js/img 等静态资源文件
| `-- style.css
|-- data # tunasync 相关文件
| |-- conf # tunasync 配置文件
| | |-- manager.conf
| | `-- worker.conf
| |-- scripts # tunasync 同步脚本
| | `-- github-release.py
|-- github-release # 所同步的镜像文件
| |-- iawia002
| | `-- annie
| `-- ytdl-org
| `-- youtube-dl
|-- log
| |-- tunasync # tunasync 运行日志
| | `-- github-release
| `-- nginx # nginx 运行日志
|-- index.html # 镜像站主页
`-- manager.db # 数据库文件

另一种方案是把同步下来的镜像路径独立出来,例如 /srv/mirrors/ ,这样后续 Nginx 的配置文件中就不需要排除 data/ , log/ 等目录。

按以下命令创建目录即可:

1
2
3
4
5
6
# mkdir /srv/tunasync/
$ mkdir -p /srv/tunasync/data/conf/
$ mkdir -p /srv/tunasync/data/scripts/
$ mkdir -p /srv/tunasync/web/
$ mkdir -p /srv/tunasync/log/tunasync/
$ mkdir -p /srv/tunasync/log/nginx/

部署

编辑配置文件

请先阅读 tunasync 上手指南 和 worker 配置示例文件 再继续。

1
2
3
$ cd /srv/tunasync/data/
$ vi conf/manager.conf
$ vi conf/worker.conf

实际配置文件如下( TOML 语法规范):

  1. /srv/tunasync/data/conf/manager.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    debug = false

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

    [files]
    db_type = "bolt"
    db_file = "/srv/tunasync/manager.db"
    ca_cert = ""
  2. /srv/tunasync/data/conf/worker.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
    [global]
    name = "mirror_worker"
    log_dir = "/srv/tunasync/log/tunasync/{{.Name}}"
    mirror_dir = "/srv/tunasync"
    # 并发线程数
    concurrent = 10
    # 同步周期,分钟
    interval = 1440

    [manager]
    api_base = "http://localhost:12345"
    token = ""
    ca_cert = ""

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

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

    [[mirrors]]
    name = "github-release"
    provider = "command"
    upstream = "https://api.github.com/repos/"
    # https://github.com/tuna/tunasync-scripts/blob/master/github-release.py
    command = "/srv/tunasync/data/scripts/github-release.py"
    interval = 1440
    docker_image = "tunathu/tunasync-scripts:latest"

    # vim: ft=toml

自定义同步脚本

1
2
3
$ wget -P scripts/ https://raw.githubusercontent.com/tuna/tunasync-scripts/master/github-release.py
$ chmod +x scripts/github-release.py
$ vi scripts/github-release.py

修改 github-release.py 中的 REPOS 数组,改为需要同步的仓库即可:

1
2
3
4
REPOS = ["soimort/you-get",
"ytdl-org/youtube-dl",
"iawia002/annie",
]

可以先检验一下此脚本能否正常运行:

1
$ python3 scripts/github-release.py

如果只是提示 Exception: Working Directory is None 而不是其他类型的错误则说明一切正常。

部署 tunasync 及其服务脚本

将 tunasync 编译好的程序放在 /usr/bin/ 目录下。

1
2
3
4
$ wget https://github.com/tuna/tunasync/releases/download/v0.3.7/tunasync-linux-bin.tar.gz
$ tar -xzvf tunasync-linux-bin.tar.gz
$ mv tunasync /usr/bin/
$ mv tunasynctl /usr/bin/

从 whsir/tunasync-bin 下载服务启动停止脚并赋予执行权限。

1
2
3
4
$ wget -P /etc/init.d/ https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-manager
$ wget -P /etc/init.d/ https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-worker
$ chmod +x /etc/init.d/tunasync-manager
$ chmod +x /etc/init.d/tunasync-worker

按照 whsir/tunasync-bin 的说明,修改脚本中配置文件路径。

1
2
$ vi /etc/init.d/tunasync-manager
$ vi /etc/init.d/tunasync-worker

两个文件中 OPTIONS= 所在的一行分别改为:

1
2
OPTIONS="manager --config /srv/tunasync/data/conf/manager.conf"
OPTIONS="worker --config /srv/tunasync/data/conf/worker.conf"

启动服务:

1
2
3
4
5
6
7
$ /etc/init.d/tunasync-manager start
$ /etc/init.d/tunasync-worker start

# or

$ service tunasync-manager start
$ service tunasync-worker start

启动服务之前也可以先以 debug 模式运行,看看有没有什么错误:

1
2
$ /etc/init.d/tunasync-manager start
$ tunasync worker --config /srv/tunasync/data/conf/worker.conf --debug

提供 HTTP 服务

安装 Nginx

本文以 Nginx 为例,选择 Apache, Caddy 等服务器亦可。

首先安装 Nginx ,这里就使用预编译包来安装了。请按照 官方说明 去操作。

1
2
$ sudo yum install yum-utils
$ vi /etc/yum.repos.d/nginx.repo

/etc/yum.repos.d/nginx.repo 的文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
1
2
3
$ yum install nginx
$ systemctl enable nginx
$ systemctl start nginx

此时访问服务器的 IP 地址应该能看到 Nginx 的欢迎界面。CentOS 需要注意防火墙问题,可以选择放行 80 端口或禁用防火墙:

1
2
3
4
5
6
7
$ firewall-cmd --permanent --add-port=80/tcp
$ firewall-cmd --reload
$ systemctl restart firewalld.service

# or

$ systemctl stop firewalld

配置 Nginx

查看 Nginx 配置文件的位置:

1
$ nginx -t

显示配置文件为 /etc/nginx/nginx.conf ,并 include 了 /etc/nginx/conf.d/ 目录。

1
$ vi /etc/nginx/conf.d/tunasync.conf

/etc/nginx/conf.d/tunasync.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
server {
listen 80 default_server;
server_name _;
root /srv/tunasync;
location ~ ^/(github-release) {
index index.html index.htm;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}

error_page 500 502 503 504 /50x.html;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /srv/tunasync/log/nginx/tunasync_access.log;
error_log /srv/tunasync/log/nginx/tunasync_error.log;
}

检测配置文件是否正确并重启 Nginx :

1
2
$ nginx -t
$ systemctl restart nginx

现在使用浏览器访问 http://xx.xx.xx.xx/github-release/ 就能看到目录结构了。

再给首页添加一个简单的 HTML 页面即可,可以参照 weyo/mirrors 进行修改简化。


参考资料

  1. tunasync 上手指南 - 你可能会用到的操作
  2. GitHub - tuna/tunasync-scripts: Custom scripts for mirror jobs
  3. 使用 tunasync 搭建开源镜像站 - 简书
  4. 通过tunasync搭建镜像仓库 - 吴昊博客
  5. Linux下使用tunasync搭建内网镜像站 | 聂扬帆博客
  6. GitHub - tuna/mirror-web: Source code of the web interface of https://mirrors.tuna.tsinghua.edu.cn
  7. Upgrade Python on Debian (3.5 => 3.6+)

  8. github release 下载代理 – Strcpy²