rhel/centos 相关的rsync脚本分享(实用)

以下脚本 可以同步官方的centos源与 epel 源! 目前生产环境使用正常!更改相关目录和排除选项即可!! 🙂


#!/bin/bash
# base value
#YUM_SITE="rsync://mirrors.kernel.org/centos/"
#YUM_SITE="rsync://mirrors.ustc.edu.cn/centos/"
YUM_SITE="rsync://mirrors.kernel.org/centos/"
#YUM_SITE="rsync://mirrors.ustc.edu.cn/centos/"
#YUM_SITE="rsync://us-msync.centos.org/CentOS/"
#YUM_SITE="rsync://dl.fedoraproject.org/fedora-epel/"


LOCAL_PATH="/share_disk_B/centos/"
LOCAL_VER="5 5* 6 6* 7 7*"
BW_limit=4096
LOCK_FILE="/var/log/yum_server.pid"
RSYNC_PATH=""

# check update yum server  pid
MY_PID=$$
if [ -f $LOCK_FILE ]; then
    get_pid=`/bin/cat $LOCK_FILE`
    get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
    if [ $get_system_pid -eq 0 ] ; then
        echo $MY_PID>$LOCK_FILE
    else
        echo "Have update yum server now!"
        exit 1
    fi
else
    echo $MY_PID>$LOCK_FILE
fi

# check rsync tool
if [ -z $RSYNC_PATH ]; then
    RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
    if [ -z $RSYNC_PATH ]; then
        echo 'Not find rsync tool.'
        echo 'use comm: yum install -y rsync'
    fi
fi

# sync yum source
for VER in $LOCAL_VER;
do 
    # Check whether there are local directory
    if [ ! -d "$LOCAL_PATH$VER" ] ; then
        echo "Create dir $LOCAL_PATH$VER"
        `/bin/mkdir -p $LOCAL_PATH$VER`
    fi
    # sync yum source
    echo "Start sync $LOCAL_PATH$VER"
    $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit --exclude "isos" $YUM_SITE$VER $LOCAL_PATH
done

# clean lock file
`/bin/rm -rf $LOCK_FILE`

echo 'sync end.'
exit 1

添加到定时任务,自动执行!

#for centos
0 */2 * * * /bin/sh /opt/rsync-yum-centos.sh >>/opt/rsync-yum-centos.log

发表评论