#!/bin/bash set -e if [ $# -eq 2 ]; then >&2 echo curl -Lo "$1" "$2" curl -Lo "$1" "$2" exit fi if [ $# -ne 1 ] || [ ! -d "$1" ]; then >&2 echo 'usage:' >&2 printf '%s /path/to/mirror\n' "$0" exit 1 fi if [ "$(whoami)" != 'mirror' ]; then exec su mirror -c "$0 $*" fi exec 9> /tmp/sync-any-packages.lock if ! flock -n 9; then >&2 echo 'cannot obtain lock' exit fi dir="$(readlink -e "$1")" for repo in core extra; do mirrors=( $( sed -n ' s/^Server = // T s/\$arch/x86_64/ s/\$repo/'"${repo}"'/ p ' '/etc/pacman.d/mirrorlist' ) ) bsdtar -Oxf "/var/lib/pacman/sync/${repo}.db" \ | sed -n ' /^%\(FILENAME\|CSIZE\|PGPSIG\)%/ { N s/^.*\n// p } ' \ | sed ' /-any\.pkg\.tar\.zst$/ !d N s/\n/ / N s/\n/ / ' \ | while read -r pkg size sig; do [ -f "${dir}/pool-any/${pkg}" ] \ && [ -f "${dir}/pool-any/${pkg}.sig" ] \ && [ $(stat -c%s "${dir}/pool-any/${pkg}") -eq "${size}" ] \ && continue printf '%s\n' "${sig}" \ | base64 -d \ > "${dir}/pool-any/${pkg}.sig" printf '%s/pool-any/%s\n%s/%s\n' "${dir}" "${pkg}" "$(printf '%s\n' "${mirrors[@]}" | shuf -n1)" "${pkg}" done \ | parallel -N 2 -j 20 "$0" done tmp_dir=$(mktemp -d) trap 'cd / && rm -rf --one-file-system "${tmp_dir:?}"' EXIT for repo in core extra; do bsdtar -C "${tmp_dir}" -xf "/var/lib/pacman/sync/${repo}.db" done for desc_file in "${tmp_dir}/"*"/desc"; do sed ' /^%ARCH%$/ !d N s/^.*\n// ' "${desc_file}" \ | grep -qxF any \ && continue rm "${desc_file}" rmdir "${desc_file%/*}" done cd "${tmp_dir}" bsdtar -czf "${dir}/pool-any/build-support-any.db.tar.gz" --strip-components=1 .