Skip to content

Instantly share code, notes, and snippets.

@udienz
Last active December 26, 2015 07:29
Show Gist options
  • Select an option

  • Save udienz/7116049 to your computer and use it in GitHub Desktop.

Select an option

Save udienz/7116049 to your computer and use it in GitHub Desktop.
Debian CD Mirror script, used at buaya.klas.or.id. See https://github.com/udienz/status and http://www.debian.org/CD/mirroring/#jigdomirror
#!/bin/bash
# Stolen from http://ftp.mgts.by/debian-mirror/cdimage/
#BASE=/srv/buaya.klas.or.id/status
. "$BASE/status/lib/init.sh"
. "$BASE/status/lib/common.sh"
DELETEFIRST=1
tmpDirBase=$(mktemp -d)
jigdoConf=$PKGDIR/jigdo-mirror.conf
masterList=$tmpDirBase/master.list
HUB=${HUB:-"false"}
TRACE="project/trace/${MIRROR_NAME}"
SLOCK="Archive-Update-in-Progress-${MIRROR_NAME}"
LOCK="${TARGET}/${SLOCK}"
SREQ="Archive-Update-Required-${MIRROR_NAME}"
UPDATEREQUIRED="${TARGET}/${SREQ}"
# rsync program
RSYNC=${RSYNC:-rsync}
RSYNC_FILTER="--filter=protect_${SLOCK}"
RSYNC_FILTER="${RSYNC_FILTER} --filter=protect_${SREQ}"
RSYNC_FILTER="${RSYNC_FILTER} --filter=protect_${TRACE}"
RSYNC_OPTIONS="-rplHtSihvB8192 --timeout 7200 --stats --exclude-from=$PKGDIR/rsync.exclude ${RSYNC_FILTER} --delete"
RSYNC_OPTIONS2="--delete"
RSYNC_OPT_NOISO="--exclude *.iso"
RSYNC_OPT_MASTERLIST='-r --list-only --no-motd --filter=+_*/ --filter=+_*iso --filter=-_*'
RSYNC_OPT_DELFIRST="--delete --ignore-existing --existing"
if [ $DELETEFIRST ]; then
RSYNC_OPTIONS2="${RSYNC_OPTIONS2} --delete-before"
else
RSYNC_OPTIONS2="${RSYNC_OPTIONS2} --delete-after"
fi
if [ ! -d ${TARGET}/project/trace ]; then
echo 'mkdir ${TARGET}/project/trace'
mkdir -p ${TARGET}/project/trace
fi
touch ${UPDATEREQUIRED}
# Check to see if another sync is in progress
if lockfile -! -l 43200 -r 0 "$LOCK" >/dev/null 2>&1 ; then
echo "Unable to start mirror sync, lock file $LOCK exists"
exit
fi
echo "Acquired main lock"
while [ -f ${UPDATEREQUIRED} ]; do
echo "Running mirrorsync, update is required, ${UPDATEREQUIRED} exists"
rm -f ${UPDATEREQUIRED}
touch $SLOCK
# Deleting the images before the run if DELETEFIRST is set.
if [ $DELETEFIRST ]; then
echo "Removing images not on the other host: ${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPT_DELFIRST} ${SOURCE} ${TARGET}"
let NRSYNCS++
${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPT_DELFIRST} \
${SOURCE} ${TARGET}
fi
echo "Perform jigdo rsync stage: ${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPTIONS2} ${RSYNC_OPT_NOISO} ${SOURCE} ${TARGET}"
let NRSYNCS++
${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPTIONS2} ${RSYNC_OPT_NOISO} \
${SOURCE} ${TARGET}
if [ -n "$masterList" ]; then
echo "Receiving master list: ${RSYNC} $RSYNC_OPT_MASTERLIST ${SOURCE}"
${RSYNC} $RSYNC_OPT_MASTERLIST \
${SOURCE} | awk '/^-/ {print $5}' > $masterList
if [ -s "$masterList" ]; then
echo "Master list generated"
else
echo "Master list generation failed"
masterList=""
fi
fi
echo "Now generating images"
typeset currentVersion=`ls -l ${TARGET}/current`
currentVersion="${currentVersion##* -> }"
versionDir="${TARGET}/${currentVersion}"
mkdir -p ${LOGDIR}/jigdo/ >/dev/null 2>&1
for a in ${versionDir}/*/; do
arch=`basename $a`
if [ -f "${TARGET}/project/build/${currentVersion}/${arch}" ]; then
sets=`cat ${TARGET}/project/build/${currentVersion}/${arch}`
else
sets="cd dvd"
fi
for s in $sets; do
typeset jigdoDir=${TARGET}/${currentVersion}/${arch}/jigdo-${s}
typeset imageDir=${TARGET}/${currentVersion}/${arch}/iso-${s}
[ -d $jigdoDir ] || continue
if [ ! -d $imageDir ]; then
log "Creating $imageDir"
mkdir -p $imageDir
fi
[ -f $imageDir/MD5SUMS ] || cp $jigdoDir/MD5SUMS $imageDir/MD5SUMS
echo "jigdoDir=$jigdoDir" > $jigdoConf.$arch.$s
echo "imageDir=$imageDir" >> $jigdoConf.$arch.$s
echo "tmpDir=$tmpDirBase/$arch.$s" >> $jigdoConf.$arch.$s
echo "logfile=${LOG}" >> $jigdoConf.$arch.$s
echo "masterList=$masterList" >> $jigdoConf.$arch.$s
cat ${jigdoConf}.in >> $jigdoConf.$arch.$s
echo "Start to jigdo $arch-$s"
jigdo-mirror $jigdoConf.$arch.$s
done
done
[ -n "$masterList" -a -f "$masterList" ] && rm -f "$masterList"
#ls ${LOGDIR}/jigdo/*.log >/dev/null 2>&1 && savelog ${LOGDIR}/jigdo/*.log >/dev/null
echo "Image generation done"
echo "Doing final rsync: ${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPTIONS2} --size-only ${SOURCE}/. ${TARGET}/."
let NRSYNCS++
${RSYNC} ${RSYNC_OPTIONS} ${RSYNC_OPTIONS2} \
--size-only --delete-excluded \
${SOURCE}/. ${TARGET}/.
echo "Final rsync with delete done"
done
if [ -d "`dirname "${TARGET}/${TRACE}"`" ]; then
LC_ALL=POSIX LANG=POSIX date -u > "${TARGET}/${TRACE}"
echo "Used cd-mirror/jigdo script version: ${VERSION}" >> "${TARGET}/${TRACE}"
echo "Running on host: $(hostname -f)" >> "${TARGET}/${TRACE}"
fi
rm -rf $tmpDirBase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment