用shell脚本实现文件、代码同步上线

step1.首先expect编写一个脚本文件rsync.expect,实现文件同步的脚本。
[root@localhost ~/syncList]#vim syncFile.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set file [lindex $argv 1]
set passwd [lindex $argv 2]
 
#核心命令,同步多个文件
spawn rsync -avR --file-from=$file /  root@$host:/
 
expect{
"yes/no" {send "yes "}
"password:" {send "$passwd "}
}
expect eof
[root@localhost ~/syncList] #chmod a+x syncFile.expect
 
step2.然后再编辑一个文本文件,这个文件用来放需要同步的文件列表:
[root@localhost ~/syncList]$ cat /tmp/fileList.txt
/usr/local/nginx/conf/vhost/dedecms.com.conf
/usr/local/nginx/conf/vhost/discu.com.conf
/usr/local/nginx/conf/vhost/zrlog.com.conf
/usr/local/sbin/nginx_log_rotate.sh
/var/spool/cron/root
 
step3还需要编辑一个ip.txt文件,用于存放需要同步的目标机器的IP地址,例如我需要将文件都同步这几个机器上:
[root@localhost ~/syncList]$ cat /root/webServerIP.txt
192.168.200.122
192.168.200.123
192.168.200.124
192.168.200.125
192.168.200.126
192.168.200.127
192.168.200.128
192.168.200.129
192.168.200.130
192.168.200.131
 
step4.再编写一个shell脚本syncFile.sh,遍历出ip.list文件内容然后交给syncFile.expect脚本去执行:
#!/bin/bash
ipList="$1"
fileList="$2"
password="$3"
for ip in `cat $ipList`
do
    #第二个参数就是需要同步的文件列表
    ./syncFile.expect $ip $fileList $password
done
 
step5.执行syncFile.sh脚本即可实现批量同步多个文件:
[root@localhost ~/syncList]$ sh ./syncFile.sh "/root/webServerIP.txt" "/tmp/fileList.txt" 
原文地址:https://www.cnblogs.com/sisul/p/9033275.html