2021-06-03 09:02:33 +00:00
|
|
|
#!/bin/env bash
|
2021-06-02 21:33:51 +00:00
|
|
|
#
|
|
|
|
# mirrorsync - Synchronize a Rocky mirror
|
|
|
|
# By: Dennis Koerner <koerner@netzwerge.de>
|
|
|
|
#
|
|
|
|
# The latest version of this script can be found at:
|
|
|
|
# https://github.com/rocky-linux/rocky-tools
|
|
|
|
#
|
|
|
|
# Please read https://docs.rockylinux.org/en/rocky/8/guides/add_mirror_manager
|
|
|
|
# for further information on setting up a Rocky mirror.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2021 Rocky Enterprise Software Foundation
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
# Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
# You can change v to q if you do not want detailed logging
|
2021-06-03 10:27:41 +00:00
|
|
|
opts=(-vrlptDSH --exclude="*.~tmp~" --delete-delay --delay-updates)
|
2021-06-02 21:33:51 +00:00
|
|
|
|
2021-11-23 16:52:40 +00:00
|
|
|
# Please use a mirror geographically close to you for initial sync,
|
|
|
|
# or if you are hosting a private mirror(not publicly available).
|
|
|
|
#
|
|
|
|
# Note that local mirrors may be faster, and we might restrict
|
|
|
|
# access to the master in the future.
|
|
|
|
#
|
2021-06-02 21:33:51 +00:00
|
|
|
# A complete list of mirrors can be found at
|
|
|
|
# https://mirrors.rockylinux.org/mirrormanager/mirrors/Rocky
|
2021-06-03 09:53:55 +00:00
|
|
|
src="msync.rockylinux.org::rocky/mirror/pub/rocky"
|
2021-06-02 21:33:51 +00:00
|
|
|
|
|
|
|
# Your local path. Change to whatever fits your system.
|
2021-06-03 09:53:55 +00:00
|
|
|
# $mirrormodule is also used in syslog output.
|
|
|
|
mirrormodule="rocky-linux"
|
|
|
|
dst="/mnt/mirrorserver/${mirrormodule}"
|
2021-06-02 21:33:51 +00:00
|
|
|
|
2021-06-03 09:53:55 +00:00
|
|
|
filelistfile="fullfiletimelist-rocky"
|
|
|
|
lockfile="$0.lockfile"
|
|
|
|
logfile="$0.log"
|
2021-06-02 21:33:51 +00:00
|
|
|
|
2021-06-03 10:25:32 +00:00
|
|
|
# Check if the filelistfile has changed on upstream mirror
|
|
|
|
# and exit if it is still the same
|
2021-06-03 10:40:08 +00:00
|
|
|
checkresult=$(rsync --no-motd --dry-run --out-format="%n" "${src}/${filelistfile}" "${dst}/${filelistfile}")
|
2021-06-03 09:53:55 +00:00
|
|
|
if [[ -z "$checkresult" ]]; then
|
|
|
|
printf "%s unchanged. Not updating at %(%c)T\n" "$filelistfile" -1 >> "$logfile" 2>&1
|
|
|
|
logger -t rsync "Not updating ${mirrormodule}: ${filelistfile} unchanged."
|
2021-06-02 21:33:51 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-06-03 10:25:32 +00:00
|
|
|
# Check for existing lockfile to avoid multiple simultaneously running syncs
|
|
|
|
# If lockfile exists but process is dead continue anyway
|
2021-06-03 10:44:27 +00:00
|
|
|
if [[ -e "$lockfile" ]] && ! kill -0 "$(< "$lockfile")" 2>/dev/null; then
|
2021-06-03 10:25:32 +00:00
|
|
|
printf "Warning: lockfile exists but process dead, continuing.\n" >> "$logfile" 2>&1
|
|
|
|
logger -t rsync "Warning: lockfile exists but process dead, continuing with updating ${mirrormodule}."
|
|
|
|
rm -f "$lockfile"
|
|
|
|
elif [[ -e "$lockfile" ]]; then
|
2021-06-03 09:53:55 +00:00
|
|
|
printf "Update already in progress at %(%c)T\n" -1 >> "$logfile" 2>&1
|
|
|
|
logger -t rsync "Not updating ${mirrormodule}: already in progress."
|
2021-06-03 10:25:32 +00:00
|
|
|
exit 1
|
2021-06-02 21:33:51 +00:00
|
|
|
fi
|
2021-06-03 10:25:32 +00:00
|
|
|
|
|
|
|
# The actual syncing part
|
|
|
|
printf '%s\n' "$$" > "$lockfile"
|
|
|
|
printf "Started update at %(%c)T\n" -1 >> "$logfile" 2>&1
|
|
|
|
logger -t rsync "Updating ${mirrormodule}"
|
2021-06-03 10:40:08 +00:00
|
|
|
rsync "${opts[@]}" "${src}/" "${dst}/" >> "$logfile" 2>&1
|
2021-06-03 10:25:32 +00:00
|
|
|
logger -t rsync "Finished updating ${mirrormodule}"
|
|
|
|
printf "End: %(%c)T\n" -1 >> "$logfile" 2>&1
|
2021-06-03 10:53:19 +00:00
|
|
|
rm -f "$lockfile"
|