使用tunasync搭建自己的开源软件镜像仓库
在开源软件的使用中,镜像仓库(Mirror Repository)扮演着至关重要的角色。它们为全球用户提供快速、可靠的软件下载服务。特别是在国内,由于网络限制,访问官方仓库往往较为缓慢,因此搭建一个自己的镜像仓库就显得尤为重要。而 TunaSync,作为一个开源的同步工具,提供了简便的方式来搭建和管理自己的镜像仓库。
tunasync 是清华大学 TUNA 镜像源目前使用的镜像方案。
1. 准备工作镜像站需要一台服务器,如果有条件,建议准备一台专用的X86物理服务器作为镜像站的服务器。服务器推荐配置:
CPU: 8 核心 2.5GHz 以上
内存: 64 GB以上
硬盘: 3TB 以上,越大越好
网络: 千兆上行带宽及以上
系统:Centos8/9及其衍生版本、Debian10/11/12及其衍生版本
当然,如果条件不具备,以上配置可以无视,选择自己最好配置的机器即可,毕竟服务器配置还是取决于最终用户的需求。
特别说要说明的是,硬盘大小要求必须超过需要同步的源文件大小,比如需要同时同步 CentOS 与 Ubuntu,就需要准备一块 1.6T 以上大小的硬盘。每个镜像的具体大小可以在清华大学开源软件镜像站 同步状态 页面查到。
2. 同步源与架构选择在官方提供的源列表中选择最方便的同步源(最好是支持 rsync 的国内镜像源,但国内目前提供 rsync 服务的镜像源不多,以下是网络收集的部分 rsync 源)。
Ubuntu 源列表:https://launchpad.net/ubuntu/+archivemirrors
12345rsync://mirrors.shuosc.org/ubuntu/rsync://mirrors.sohu.com/ubuntu/rsync://mirrors.tuna.tsinghua.edu.cn/ubuntu/rsync://mirrors.ustc.edu.cn/ubuntu/rsync://mirrors.yun-idc.com/ubuntu/
CentOS 源列表:https://www.centos.org/download/mirrors/
123rsync://mirrors.tuna.tsinghua.edu.cn/centos/rsync://mirror.es.its.nyu.edu/centos/rsync://centos.sonn.com/CentOS/
EPEL 源列表:https://admin.fedoraproject.org/mirrormanager/mirrors/EPEL
12rsync://mirrors.yun-idc.com/epelrsync://rsync.mirrors.ustc.edu.cn/epel
3.开始安装安装需要的软件1yum install rsync wget
下载tunasync12345wget https://gh-proxy.com/https://github.com/tuna/tunasync/releases/download/v0.8.0/tunasync-linux-amd64-bin.tar.gz#基于amd64,如果是ARM系列,请去https://github.com/tuna/tunasync/releases自行替换下载一个tar xf tunasync-linux-amd64-bin.tar.gzmv tunasync /usr/bin/mv tunasynctl /usr/bin/
创建仓库文件夹1mkdir -p /home/www/mirrors
创建日志文件夹1mkdir -p /home/www/mirrors/logs
创建配置文件夹1mkdir /etc/tunasync
创建manager的配置文件1vim /etc/tunasync/manager.conf
将以下内容根据自己的需求修改后写入manager的配置文件内
123456789101112debug = false [server]addr = "127.0.0.1" #manager服务的监听地址,默认127.0.0.1一般就可以了port = 12345 # manager服务监听端口ssl_cert = "" #证书配置,可为空ssl_key = "" #证书配置,可为空 [files]db_type = "bolt"db_file = "/etc/tunasync/manager.db" #数据库文件位置ca_cert = ""
创建worker.conf配置文件1vim /etc/tunasync/worker.conf
将以下内容根据自己的需求修改后写入worker的配置文件内,mirrors部分仅作示范写了两个,可以根据自身需要自由添加或删减
1234567891011121314151617181920212223242526272829303132333435[global]name = "worker" # worker名字log_dir = "/home/www/mirrors/logs/{{.Name}}" # 日志存储位置mirror_dir = "/home/www/mirrors" # 仓库存储位置concurrent = 10 # 线程数interval = 240 # 同步周期,单位分钟 [manager]api_base = "http://127.0.0.1:12345" # manager的API地址token = ""ca_cert = "" [cgroup]enable = falsebase_path = "/sys/fs/cgroup"group = "tunasync" [server]hostname = "localhost"listen_addr = "127.0.0.1"listen_port = 6000ssl_cert = ""ssl_key = "" [[mirrors]]name = "centos"provider = "rsync"upstream = "rsync://ftp.kaist.ac.kr/CentOS/"use_ipv6 = false [[mirrors]]name = "ubuntu"provider = "rsync"upstream = "rsync://ftp.kaist.ac.kr/ubuntu/"use_ipv6 = false
自定义同步脚本以Github为例
创建文件夹
1mkdir -p /home/www/mirrors/scripts
在worker.conf文件添加
1234567[[mirrors]]name = "github-release"provider = "command"upstream = "https://api.github.com/repos/"command = "/home/www/mirrors/scripts/github-release.py"interval = 1440docker_image = "tunathu/tunasync-scripts:latest"
下载同步脚本
12345wget -P scripts/ https://gh-proxy.com/https://raw.githubusercontent.com/muzihuaner/mirrors/refs/heads/new/mirrors/scripts/github-release.pywget -P scripts/ https://gh-proxy.com/https://raw.githubusercontent.com/muzihuaner/mirrors/refs/heads/new/mirrors/scripts/github-release.jsonchmod +x scripts/github-release.pyvim scripts/github-release.pyvim scripts/github-release.json #修改你要同步的仓库
可以先检验一下此脚本能否正常运行:
1python3 scripts/github-release.py
更多配置:https://github.com/tuna/tunasync/blob/master/docs/zh_CN/workers.conf
更多脚本:https://github.com/tuna/tunasync-scripts
下载tunasync启动停止脚本https://github.com/whsir/tunasync-bin
1234wget -P /etc/init.d/ https://gh-proxy.com/https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-managerwget -P /etc/init.d/ https://gh-proxy.com/https://raw.githubusercontent.com/whsir/tunasync-bin/master/tunasync-workerchmod +x /etc/init.d/tunasync-managerchmod +x /etc/init.d/tunasync-worker
启动服务12/etc/init.d/tunasync-manager start/etc/init.d/tunasync-worker start
重启服务12/etc/init.d/tunasync-manager restart/etc/init.d/tunasync-worker restart
查看服务状态
12/etc/init.d/tunasync-manager status/etc/init.d/tunasync-worker status
到这里为止,自建仓库就算完成了,可以通过以下命令来查看进程
1tunasynctl list -p 12345 --all
添加计划任务创建日志文件
1touch /home/www/mirrors/logs/wget.log
获取任务状态信息并保存为 json 文件
1crontab -e
添加
1*/1 * * * * wget -q http://127.0.0.1:12345/jobs -O /home/www/mirrors/jobs.json -o /home/www/mirrors/logs/wget.log
验证
1crontab -l
安装 Caddy Web服务器配置访问权限镜像目录权限设置
1chmod 755 /home/www/mirrors
SELINUX权限设置(可选)
1chcon -R -t httpd_sys_content_t /home/www/mirrors
安装 Docker如果你还没有安装 Docker,可以使用脚本安装 Docker:
https://getdocker.quickso.cn/
创建Caddy文件夹
1mkdir -p /home/www/Caddy
创建Caddyfile
1234:80 { root * /usr/share/caddy file_server browse}
为了简化部署,可以使用 Docker Compose 来管理 Caddy 服务和配置。
创建 docker-compose.yml12345678910111213141516171819version: '3'services: caddy: image: caddy:latest container_name: caddy restart: unless-stopped ports: - "80:80" - "443:443" volumes: - /home/www/Caddy/Caddyfile:/etc/caddy/Caddyfile - /home/www/mirrors:/usr/share/caddy - caddy_data:/data - caddy_config:/configvolumes: caddy_data: caddy_config:
启动 Caddy 服务运行以下命令启动 Caddy 服务:
1docker-compose up -d
并根据需要补充 index.html 和配置的 web 文件到 /mirrors 目录。
网页文件可以在 https://github.com/muzihuaner/mirrors/tree/new/mirrors上下载使用(所有 web 文件放到 /mirrors 目录即可)。
文件夹层级
123456789.└── www └── mirrors 网站根目录 ├── index.html 首页 ├── jobs.json 状态信息 ├── logs 日志 ├── scripts 脚本 ├── static 网页静态资源 ├── status 服务器状态网页目录
至此,同步服务以及 web 访问服务全部搭建完成,内网的小伙伴们可以通过浏览器访问镜像站了 http://服务器IP/。
文章参考http://weyo.me/pages/techs/how-to-make-a-mirror-site/
https://blog.lussac.net/archives/342/
https://github.com/tuna/tunasync
https://blog.whsir.com/post-5094.html
https://www.shellop.com/archives/36
https://fangpsh.github.io/posts/2014/2014-01-01.html