Skip to content

Instantly share code, notes, and snippets.

@jacky9813
Created June 14, 2020 12:49
Show Gist options
  • Save jacky9813/730f7d3692b29b32acfe95c6d23ac906 to your computer and use it in GitHub Desktop.
Save jacky9813/730f7d3692b29b32acfe95c6d23ac906 to your computer and use it in GitHub Desktop.
Bash script for mirroring yum repositories registered in the system
#!/usr/bin/env bash
BASEDIR=$(pwd)
PROGRAMDIR=$(dirname $(readlink -f "${BASH_SOURCE[0]}"))
REPOSINFODIR=/etc/yum.repos.d
SYNCSTARTTIME=$(date "+%Y%m%d%H%M%S")
SYNCLOGDIR=$BASEDIR/logs/sync/$SYNCSTARTTIME
GLOBALLOGFILE=SyncLog.log
ALLREPOS=false
VERBOSE=false
SHOWREPOS=false
ARCH=''
VERSION="2020.0614.2040"
CMDLINE="${BASH_SOURCE[0]}"
helpmsg() {
cat << EOF
Yum Mirroring Tool (version: $VERSION) by JackyCCC
Timestamp: 2020-06-14 20:47:00 UTC+0800
Usage: ${BASH_SOURCE[0]} [Options]
Options:
-h, --help Show this help message
--version Show version of this program
--allrepos Syncronize to all repositories, even if disabled
--enabledrepos Syncronize to repositories that is enabled in yum
--showrepos It shows the repositories in syncronization list, without actually syncronizing.7
--arch=<ARCH> Specifies target system architecture
--basedir=<PATH> Set a base path for syncronization (default: <Current directory>)
--logdir=<PATH> Specifies where log files should be put in (default: <Current directory>/logs/sync/<syncronization start time>)
--reposinfodir=<PATH> Specifies the location for information about all yum repositories (default: /etc/yum.repos.d)
--verbose Pass verbose to reposync tool
This script requires following packages:
createrepo_c yum-utils
Please make sure these packages are installed in the system before running.
EOF
} # helpmsg
get_date () {
echo $(date "+%Y-%m-%d %H:%M:%S UTC%z")
} # get_date
get_repolist () {
# make parameter 1 as true to return all repositories, include those disabled
for f in $(ls $REPOSINFODIR); do
repos=$(grep -oP "^\[\K[^\]]*" $REPOSINFODIR/$f)
for repo in $repos; do
if $1 == true; then
echo $repo
else
status=$(yum repoinfo $repo 2>/dev/null | grep -oP "Repo-status *: *\K\w*")
if [ $status == "enabled" ] 2>/dev/null ; then
echo $repo
fi
fi
done
done
} # get_repolist
sync () {
echo "[$(get_date)] Creating log directory if not existed"
mkdir -p $SYNCLOGDIR
echo "[$(get_date)] Running syncronization with parameter: $CMDLINE" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
echo "[$(get_date)] Parsing Repository" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
for repo in $(get_repolist $ALLREPOS); do
LOGFILE=${repo}.log
echo "[$(get_date)] Starting sync with repo $repo" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
echo "[$(get_date)] View syncronize log in $BASEDIR/logs/sync/$LOGFILE" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
args="--delete --downloadcomps --repoid=$repo --newest-only --download-metadata --download-path=$BASEDIR/repos/"
if [ $VERBOSE == true ] ; then # Check if $VERBOSE is true
args=$(echo "$args --verbose")
fi
if [ ! -z "$ARCH" ]; then # Check if ARCH is set to non-blank value
args=$(echo "$args --arch=$ARCH")
fi
echo "[$(get_date)] reposync $args > >(tee -a $SYNCLOGDIR/$LOGFILE) 2> >(tee -a $SYNCLOGDIR/$LOGFILE >&2)" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
reposync $args > >(tee -a $SYNCLOGDIR/$LOGFILE) 2> >(tee -a $SYNCLOGDIR/$LOGFILE >&2)
echo "[$(get_date)] reposync returned with status ${PIPESTATUS[0]}" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
done
echo "[$(get_date)] Repository Syncronization completed" | tee -a $SYNCLOGDIR/$GLOBALLOGFILE
} #sync
# Parsing options
for i in "$@"; do
CMDLINE=$(echo "$CMDLINE $i")
case $i in
-h|--help)
helpmsg
exit 0
shift
;;
--basedir=*)
BASEDIR=${i#*=}
shift
;;
--logdir=*)
SYNCLOGDIR=${i#*=}
shift
;;
--allrepos)
ALLREPOS=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
--reposinfodir=*)
REPOSINFODIR=${i#*=}
shift
;;
--showrepos)
SHOWREPOS=true
shift
;;
--version)
echo $VERSION
exit 0
shift
;;
*)
echo "Unknown option $i" >&2
;;
esac
done
# Action time
if [ $SHOWREPOS == true ] ; then
get_repolist $ALLREPOS
else
sync
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment