本站镜像源 https://github.xyh.moe:8888/xian.yuheng/distro-mirror 同步脚本

#!/bin/bash

set -e
##########################################################################
#  rsync                                                                 #
#  -a 归档模式                                                           #
#  -v 详细输出                                                           #
#  -r 递归模式                                                           #
#  -t 保留修改时间                                                       #
#  -L 符号链接重定向为文件                                               #
#  --delete 删除多余文件                                                 #
#  --no-motd 忽略 rsync daemon 的 MOTD 信息                              #
#  --quiet, -q 不显示 error 之外的信息                                   #
#  --mkpath 在 DEST 上创建 DEST 指定的路径字符串中不存在的路径           #
##########################################################################

readonly MIRRORS_DIR='/mirrors'                        # 定义镜像根目录

#readonly TSINGHUA_URL='mirrors.tuna.tsinghua.edu.cn'     # 清华大学
#readonly JTU_URL='mirror.sjtu.edu.cn'                    # 上海交通大学
readonly NJU_URL='mirrors.nju.edu.cn'                    # 南京大学
readonly USTC_URL='rsync.mirrors.ustc.edu.cn'            # 中国科学技术大学 
########################################################################################################################
# Debian
function fn_debian() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "======== Debian  Trixie ========"
	echo -e "================================"
	printf "\e[0m"
	
	[[ -d $MIRRORS_DIR/debian/ ]] || mkdir -p $MIRRORS_DIR/debian/
	
	debmirror \
		--progress \
		--nosource \
		--no-check-gpg \
		--dist=trixie,trixie-updates \
		--arch=amd64 \
		--i18n \
		-e rsync \
		--host=rsync.mirrors.ustc.edu.cn \
		--root=debian \
		$MIRRORS_DIR/debian/
}
# Debian Security
function fn_debian-security() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "======= Debian  Security ======="
	echo -e "================================"
	printf "\e[0m"
	
	[[ -d $MIRRORS_DIR/debian/ ]] || mkdir -p $MIRRORS_DIR/debian-security/

	debmirror \
		--progress \
		--nosource \
		--no-check-gpg \
		--dist=trixie-security \
		--arch=amd64 \
		--i18n \
		-e rsync \
		--host=rsync.mirrors.ustc.edu.cn \
		--root=debian-security \
		$MIRRORS_DIR/debian-security/
}

# CentOS 7
function fn_centos_7() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/centos-vault/centos/7/os/x86_64/      $MIRRORS_DIR/centos/7/os/x86_64/         # os
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/centos-vault/centos/7/updates/x86_64/ $MIRRORS_DIR/centos/7/updates/x86_64/    # updates
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/centos-vault/centos/7/extras/x86_64/  $MIRRORS_DIR/centos/7/extras/x86_64/     # extras

	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/centos/RPM-GPG-KEY-CentOS-7           $MIRRORS_DIR/centos/RPM-GPG-KEY-CentOS-7 # GPG-KEY
}
# Almalinux 9
function fn_almalinux_9() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/AppStream/x86_64/os/Packages/        $MIRRORS_DIR/almalinux/9/AppStream/x86_64/os/Packages/            # AppStream
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/AppStream/x86_64/os/repodata/        $MIRRORS_DIR/almalinux/9/AppStream/x86_64/os/repodata/            # AppStream
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/BaseOS/x86_64/os/Packages/           $MIRRORS_DIR/almalinux/9/BaseOS/x86_64/os/Packages/               # BaseOS
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/BaseOS/x86_64/os/repodata/           $MIRRORS_DIR/almalinux/9/BaseOS/x86_64/os/repodata/               # BaseOS
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/extras/x86_64/os/Packages/           $MIRRORS_DIR/almalinux/9/extras/x86_64/os/Packages/               # extras
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/9/extras/x86_64/os/repodata/           $MIRRORS_DIR/almalinux/9/extras/x86_64/os/repodata/               # extras
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/RPM-GPG-KEY-AlmaLinux                  $MIRRORS_DIR/almalinux/RPM-GPG-KEY-AlmaLinux                      # GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/RPM-GPG-KEY-AlmaLinux-9                $MIRRORS_DIR/almalinux/RPM-GPG-KEY-AlmaLinux-9                    # GPG-KEY
}
# Almalinux 10
function fn_almalinux_10() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/AppStream/x86_64/os/Packages/        $MIRRORS_DIR/almalinux/10/AppStream/x86_64/os/Packages/          # AppStream
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/AppStream/x86_64/os/repodata/        $MIRRORS_DIR/almalinux/10/AppStream/x86_64/os/repodata/          # AppStream
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/BaseOS/x86_64/os/Packages/           $MIRRORS_DIR/almalinux/10/BaseOS/x86_64/os/Packages/             # BaseOS
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/BaseOS/x86_64/os/repodata/           $MIRRORS_DIR/almalinux/10/BaseOS/x86_64/os/repodata/             # BaseOS
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/extras/x86_64/os/Packages/           $MIRRORS_DIR/almalinux/10/extras/x86_64/os/Packages/             # extras
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/10/extras/x86_64/os/repodata/           $MIRRORS_DIR/almalinux/10/extras/x86_64/os/repodata/             # extras
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/RPM-GPG-KEY-AlmaLinux                  $MIRRORS_DIR/almalinux/RPM-GPG-KEY-AlmaLinux                      # GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/almalinux/RPM-GPG-KEY-AlmaLinux-10                $MIRRORS_DIR/almalinux/RPM-GPG-KEY-AlmaLinux-10                  # GPG-KEY
}
# EPEL 7
function fn_epel_7() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/fedora-archive/epel/7/x86_64/Packages/ $MIRRORS_DIR/epel/7/x86_64/Packages/
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/fedora-archive/epel/7/x86_64/repodata/ $MIRRORS_DIR/epel/7/x86_64/repodata/
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/epel/RPM-GPG-KEY-EPEL-7                $MIRRORS_DIR/epel/RPM-GPG-KEY-EPEL-7
}
# EPEL 9
function fn_epel_9() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/9/Everything/x86_64/Packages/ $MIRRORS_DIR/epel/9/Everything/x86_64/Packages/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/9/Everything/x86_64/repodata/ $MIRRORS_DIR/epel/9/Everything/x86_64/repodata/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/RPM-GPG-KEY-EPEL-9            $MIRRORS_DIR/epel/RPM-GPG-KEY-EPEL-9
}
# EPEL 10
function fn_epel_10() {
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/10/Everything/x86_64/Packages/ $MIRRORS_DIR/epel/10/Everything/x86_64/Packages/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/10/Everything/x86_64/repodata/ $MIRRORS_DIR/epel/10/Everything/x86_64/repodata/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/epel/RPM-GPG-KEY-EPEL-10            $MIRRORS_DIR/epel/RPM-GPG-KEY-EPEL-10
}

# Archlinux
function fn_archlinux() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "========   Archlinux    ========"
	echo -e "================================"
	printf "\e[0m"

	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/archlinux/core/      $MIRRORS_DIR/archlinux/core/         # core
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/archlinux/extra/     $MIRRORS_DIR/archlinux/extra/        # extra
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/archlinux/community/ $MIRRORS_DIR/archlinux/community/    # community
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/archlinux/multilib/  $MIRRORS_DIR/archlinux/multilib/     # multilib
}
# Archlinuxcn
function fn_archlinuxcn() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "========  Archlinux cn  ========"
	echo -e "================================"
	printf "\e[0m"

	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/archlinuxcn/x86_64/ $MIRRORS_DIR/archlinuxcn/x86_64/
}

# Docker-CE
function fn_dockerce_debian_bookworm() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "== Docker-CE Debian Bookworm  =="
	echo -e "================================"
	printf "\e[0m"
	
	# Debian 12
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/        $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/pool/stable/amd64/
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/stable/binary-amd64/      $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/stable/binary-amd64/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64     $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64.bz2 $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64.bz2
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64.gz  $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/stable/Contents-amd64.gz
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/InRelease                 $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/InRelease
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/Release                   $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/Release
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/bookworm/Release.gpg               $MIRRORS_DIR/docker-ce/linux/debian/dists/bookworm/Release.gpg
	
	# GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/gpg $MIRRORS_DIR/docker-ce/linux/debian/gpg
}
function fn_dockerce_debian_trixie() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "== Docker-CE Debian Trixie    =="
	echo -e "================================"
	printf "\e[0m"
	
	# Debian 13
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/pool/stable/amd64/        $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/pool/stable/amd64/
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/stable/binary-amd64/      $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/stable/binary-amd64/
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64     $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64.bz2 $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64.bz2
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64.gz  $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/stable/Contents-amd64.gz
	
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/InRelease                 $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/InRelease
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/Release                   $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/Release
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/dists/trixie/Release.gpg               $MIRRORS_DIR/docker-ce/linux/debian/dists/trixie/Release.gpg
	
	# GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$USTC_URL/docker-ce/linux/debian/gpg $MIRRORS_DIR/docker-ce/linux/debian/gpg
}
function fn_dockerce_centos_7() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "====  Docker-CE CentOS  7   ===="
	echo -e "================================"
	printf "\e[0m"
	
	# CentOS 7
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/7/x86_64/stable/ $MIRRORS_DIR/docker-ce/linux/centos/7/x86_64/stable/
	
	# GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/gpg $MIRRORS_DIR/docker-ce/linux/centos/gpg
}
function fn_dockerce_centos_9() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "====  Docker-CE CentOS  9   ===="
	echo -e "================================"
	printf "\e[0m"
	
	# CentOS 9
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/9/x86_64/stable/ $MIRRORS_DIR/docker-ce/linux/centos/9/x86_64/stable/
	
	# GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/gpg $MIRRORS_DIR/docker-ce/linux/centos/gpg
}
function fn_dockerce_centos_10() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "====  Docker-CE CentOS 10   ===="
	echo -e "================================"
	printf "\e[0m"
	
	# CentOS 10
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/10/x86_64/stable/ $MIRRORS_DIR/docker-ce/linux/centos/10/x86_64/stable/
	
	# GPG-KEY
	rsync -avrtL --delete --mkpath --no-motd rsync://$NJU_URL/docker-ce/linux/centos/gpg $MIRRORS_DIR/docker-ce/linux/centos/gpg
}

# Kubernetes
function fn_kubernetes() {
	printf "\e[32m"
	echo -e "================================"
	echo -e "========   Kubernetes   ========"
	echo -e "================================"
	printf "\e[0m"
	
	rsync -avrtL \
		--exclude="*/*/arm64/"         \
		--exclude="*/*/ppc64el/"       \
		--exclude="*/*/s390x/"         \
		--exclude="*/*/aarch64/"       \
		--exclude="*/*/ppc64le/"       \
		--delete --mkpath --no-motd    \
		rsync://$USTC_URL/kubernetes/addons:/cri-o:/stable:/ $MIRRORS_DIR/kubernetes/addons:/cri-o:/stable:/

	rsync -avrtL \
		--exclude="*/*/arm64/"         \
		--exclude="*/*/ppc64el/"       \
		--exclude="*/*/s390x/"         \
		--exclude="*/*/aarch64/"       \
		--exclude="*/*/ppc64le/"       \
		--delete --mkpath --no-motd    \
		rsync://$USTC_URL/kubernetes/core:/stable:/ $MIRRORS_DIR/kubernetes/core:/stable:/
}

function fn_main() {
	# 记录开始时间
	start_time=$(date +%s)
	start_dt=$(date "+%Y-%m-%d %H:%M:%S")
	#================================================================
	[[ $DEBIAN == 1 ]]                     && fn_debian
	[[ $DEBIAN_SEC == 1 ]]                 && fn_debian-security
	
	[[ $CENTOS_7 == 1 ]]                   && fn_centos_7
	[[ $ALMALINUX_9 == 1 ]]                && fn_almalinux_9
	[[ $ALMALINUX_10 == 1 ]]               && fn_almalinux_10
	[[ $EPEL_7 == 1 ]]                     && fn_epel_7
	[[ $EPEL_9 == 1 ]]                     && fn_epel_9
	[[ $EPEL_10 == 1 ]]                    && fn_epel_10
	
	[[ $ARCHLINUX == 1 ]]                  && fn_archlinux
	[[ $ARCHLINUXCN == 1 ]]                && fn_archlinuxcn
	
	[[ $DOCKERCE_DEBIAN_BOOKWORM == 1 ]]   && fn_dockerce_debian_bookworm
	[[ $DOCKERCE_DEBIAN_TRIXIE == 1 ]]     && fn_dockerce_debian_trixie
	[[ $DOCKERCE_CENTOS_7 == 1 ]]          && fn_dockerce_centos_7
	[[ $DOCKERCE_CENTOS_9 == 1 ]]          && fn_dockerce_centos_9
	[[ $DOCKERCE_CENTOS_10 == 1 ]]         && fn_dockerce_centos_10
	
	[[ $KUBERNETES == 1 ]]                 && fn_kubernetes
	#================================================================
	# 记录结束时间
	end_time=$(date +%s)
	end_dt=$(date "+%Y-%m-%d %H:%M:%S")
	
	# 计算耗时
	elapsed=$((end_time - start_time))
    # 转换为时分秒
    hours=$((elapsed / 3600))
    minutes=$(((elapsed % 3600) / 60))
    seconds=$((elapsed % 60))
    formatted_elapsed=$(printf "%02d 小时 %02d 分 %02d 秒" "$hours" "$minutes" "$seconds")
	
	printf "\e[32m"
	echo "===================================="
	printf "%s%s%-19s%s\n"    "== " "开始时间 : " "$start_dt"          " =="
	printf "%s%s%-19s%s\n"    "== " "结束时间 : " "$end_dt"            " =="
	printf "%s%s%16s 秒%s\n"  "== " "总计耗时 : " "$elapsed"           " =="
	printf "%s%s%23s%s\n"     "== " "总计耗时 : " "$formatted_elapsed" " =="
	echo "===================================="
	printf "\e[0m"
}

fn_main