欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell 批量同步

程序员文章站 2024-03-14 09:26:58
...

批量同步脚本

#!/bin/bash
#lijunmin 20171124
#批量同步脚本

flush()
{
if [ ! -f rsync.list ];then
	echo -e "\033[31mPlease Create rsync.list Files,The rsync.list contents as follows: \033[0m"
cat <<EOF
192.168.20.125	src_dir	des_dir
192.168.20.123  src_dir des_dir
EOF
	exit
fi
	rm -fr rsync.list.swp; cat rsync.list |grep -v "#" >rsync.list.swp
	COUNT=`cat rsync.list.swp |wc -l`
	NUM=0
while ((${NUM} < $COUNT))
do
	NUM=`expr $NUM + 1`
	Line=`sed -n "${NUM}P" rsync.list.swp`
	SRC=`echo $Line |awk '{print $2}'`
	DES=`echo $Line |awk '{print $3}'`
	IP=`echo $Line |awk '{print $1}'`
	rsync -aP --delete ${SRC}/ [email protected]${IP}:${DES}/
done
}

restart()
{
	rm -fr restart.list.swp; cat rsync.list |grep -v "#" >>restart.list.swp
        COUNT=`cat restart.list.swp |wc -l`
        NUM=0
while ((${NUM} < $COUNT))
do
        NUM=`expr $NUM + 1`
        Line=`sed -n "${NUM}P" restart.list.swp`
        Command=`echo $Line |awk '{print $2}'`
        IP=`echo $Line |awk '{print $1}'`
        ssh -l root $IP "$Command;echo -e '---------------------------------\nThe $IP Exec Command : sh $Command success!'"
done
}
case $1 in
	flush)
	flush
	;;
	restart)
	restart
	;;
	*)
	echo -e "\033[31mUsage: $0 command, example{flush | restart} \033[0m"
	;;
esac

 

相关标签: rsync