Here's mine to grab the latest snapshot, using the "current" symlink, fetching both the current snapshot and the sha512sum.txt file from the same server, to avoid problems with pooled servers out of sync. It no longer maintains its own timestamp.
It keeps the synchronized snapshot and the current sha512sum.txt file in "/var/db/snapshot/".
It checks the validity of the sha512sum.txt file, and that the sha512sum for the snapshot agrees with that expected.
(The validity check demands what I think is an obsolete key "Gentoo ebuild repository signing key (Automated Signing Key) <infrastructure@gentoo.org>". It's no longer listed amongst the signing keys, but Googling shows it was used in 2018. It's also the key used for the taballs in gentoo/snapshots.)
It's tailorable for file names, directories, and whether you want lzo or xz compressed snapshots.
It no longer writes messages to syslog.
If the snapshot was mounted before you invoke the script, it gets remounted after it runs, and it prints the timestamp from its Metadata file.
It works with dash, and so it should work with any shell worthy of note.
Enjoy.
Code: Select all
#!/bin/sh
### Update a squashfs portage snapshot from an rsync mirror
# Version 0.2 Paul Gover 2021/3/14
set -u -e # Catch typos and unchecked commands
# Portage definitions - basically from /etc/portage/make.conf and/or /etc/portage/repos.conf/gentoo
PORTDIR="/var/db/repos/gentoo"
SYNCURI="rsync.uk.gentoo.org"
SYNCDIR="gentoo/snapshots/squashfs"
# Defintions for what and where to store the snapshot. Note that rsync mirrors offer a choice of .lzo and .xz compressed SHOTs.
DIR="/var/db/snapshot"
SUMS="sha512sum.txt"
TYPE="lzo.sqfs"
NAME="gentoo-current"
NDIR="$DIR.new"
ODIR="$DIR.old"
### Utility functions
# Attempt a command, but don't exit just because it failed
tryto() {
$@ || echo "Command $* failed - continuing."
}
# Issue an error message to stderr, tidy up, then exit
die() {
echo "Error: %s\n" "$*" >& 2
tryto rm -r "$NDIR"
exit 1
}
# Run a command. If if fails, exit with an apprpriate error message
command() {
$@ || die "Command $* failed - exiting."
}
# Swap new for current directories safely
update() {
[ -e "$ODIR" ] && command rm -r "$ODIR"
command mv "$DIR" "$ODIR"
command mv "$NDIR" "$DIR"
command rm -r "$ODIR"
}
### Mainline code
tryto mkdir "$NDIR" # Assume fails means NDIR is left from before.
tryto cp -a "$DIR/*" "$NDIR" # Assume fail means DIR is not yet populated - i.e. this is first run
command rsync --copy-links --verbose --update "$SYNCURI::$SYNCDIR/$SUMS" "::$SYNCDIR/$NAME.$TYPE" "$NDIR"
command gpg --verify "$NDIR/$SUMS"
cd "$NDIR" # sha512sum --check has to be run from the same relative directory used to create the checksums
command sha512sum --check --ignore-missing --status "$SUMS"
if mountpoint -dq "$PORTDIR"
then
command umount "$PORTDIR"
update
command mount "$PORTDIR"
grep -F "TIMESTAMP" "$PORTDIR/Manifest"
else
update
fi
chgrp -R portage /var/db/snapshot




