Skip to content

Instantly share code, notes, and snippets.

@lueo
Forked from skunkie/reposync.sh
Last active December 3, 2017 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lueo/7a25ab34e47d209b14975b2c951752df to your computer and use it in GitHub Desktop.
Save lueo/7a25ab34e47d209b14975b2c951752df to your computer and use it in GitHub Desktop.
This bash script synchronizes local repositories with external repositories, RPM, reposync, createrepo
#!/bin/bash
# requires bash version 4
# This script synchronizes local repositories with
# external repositories
#
############################################################
# PREREQUISITES
############################################################
for util in reposync createrepo; do
if [ -z $(which $util) ]; then
echo "$util is not found"
echo -n "please verify that packages"
echo " yum-utils and createrepo are installed"
exit 1
fi
done
configfile=$(dirname $(readlink -f $0))/'reposync.ini'
if [ ! -f $configfile ]; then
echo "config file ${configfile} is not found"
exit 1
fi
############################################################
# READ CONFIG FILE
############################################################
while IFS='= ' read k v; do
if [[ $k == \[*\] ]]; then
section=$(echo "$k" | tr -d "[] ")
declare -A $section
elif [[ $v ]]; then
if [ ! $(echo $v | grep "^#") ]; then
eval $section[$k]=$(echo "$v" | tr -d " ")
fi
fi
done < $configfile
# path to the root of all the repositories
reposroot=${settings[reposroot]}
# proxy settings
export http_proxy=${settings[proxy]}
export https_proxy=${settings[proxy]}
############################################################
# FUNCTIONS
############################################################
usage(){
echo "
Usage: $0 [OPTION]
-h --help show this help
-n --name [repo1] [repo2] synchronize only the repository(s) with the given name(s)
-l --list show the names of all the repositories
-a --all --syncall synchronize all the repositories
"
}
hashHasKey() {
# returns 1 if the key exists, otherwise 0
eval 'local keys=${!'$1'[@]}';
eval "case '$2' in
${keys// /|}) return 0 ;;
* ) return 1 ;;
esac";
}
hashGetValue() {
# return value of the key, otherwise 0
eval 'local keys=${!'$1'[@]}';
eval "case '$2' in
${keys// /|}) echo \${$1[$2]};return 0 ;;
* ) return 1 ;;
esac";
}
############################################################
# USER INTERACTION
############################################################
case $# in
0)
usage
exit 0
;;
1)
case $1 in
-l|--list)
echo -e "\nAvailable repositories:\n"
(IFS=$'\n'; echo -e "${!reposlist[*]}\n")
exit 0
;;
-a|--all|--syncall)
;;
-h|--help)
usage
exit 0
;;
*)
echo "$1 is an incorrect or incomplete option"
usage
exit 1
;;
esac
;;
2)
case $1 in
-n|--name)
if hashHasKey reposlist $2; then
value=$(hashGetValue reposlist $2)
declare -A reposlist=( [$2]=$value )
else
echo "$2 is an incorrect name of the repository, exiting"
exit 0
fi
;;
*)
echo "$@ are incorrect options"
usage
exit 1
;;
esac
;;
*)
case $1 in
-n|--name)
wrongname=false
declare -A temp
for name in ${@:2}; do
if ! hashHasKey reposlist $name; then
echo "$name is an incorrect name of the repository"
wrongname=true
else
value=$(hashGetValue reposlist $name)
temp[$name]=$value
fi
done
if $wrongname; then
echo "exiting"
exit 1
else
reposlist=()
for idx in "${!temp[@]}"; do
reposlist[$idx]=${temp[$idx]}
done
fi
;;
*)
echo "$@ are incorrect options"
usage
exit 1
;;
esac
;;
esac
############################################################
# SYNCHRONIZE
############################################################
for repo in ${!reposlist[@]}; do
echo "[Repository] $repo"
if [ ${reposlist[$repo]} != "true" ] &&
[ ${reposlist[$repo]} != "false" ]; then
${reposlist[$repo]}
continue
fi
if ${reposlist[$repo]}; then
reposync -d -l -r $repo -p $reposroot
fi
if [ -f $reposroot/$repo/comps.xml ]; then
createrepo -s sha256 --update $reposroot/$repo -g $reposroot/$repo/comps.xml
else
createrepo -s sha256 --update $reposroot/$repo
fi
done
@lueo
Copy link
Author

lueo commented Dec 3, 2017

  1. show current progress
  2. remove "--newest-only" for reposync
  3. change "sha256" for createrepo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment