shell自动同步移动文件脚本

在企业中,服务器很多,又要做到集群,比如说网站目录,做到同步。在刚搭建环境的时候不可能一个文件一个文件的去写或者一个一个的拷到服务器上,我这儿呢就写了一个自动移动文件的脚本,把需要的文件复制到刚搭建的服务器即可。

#!/bin/bash
#Auto change server files
#Author kanghui 2016-06-17
#Source server 192.168.33.10
#Target server 192.168.33.14
if      [ ! -f ip.txt ];then
        echo -e "\033[33mplease create ip.txt files,the ip.txt contents as follows: \033[0m"
cat <<EOF
192.168.149.128
192.168.149.129
EOF
        exit
fi

if
        [ -z "$1" ];then
#这个会提示你出入源文件目录目录,这儿我们可以做一个免秘钥登陆
#ssh-keygen 一直回车,然后把秘钥拷贝到目标服务器上
#ssh-copy-id -i /root/.ssh.id_rsa.pub 192.168.33.14 回车yes就不会再输入密码了
#ssh-copy-id -i /root/.ssh.id_rsa.pub 192.168.33.10 在本机也执行这条命令
#然后执行脚本 install.log /tmp (install.log 是源文件 /tmp是目标目录,是不是超级简单)
        echo -e "\033[32m usage: $0 command ,example{Src_Files|Src_Dir} \033[0m"
        exit
fi
count=`cat ip.txt |wc -l`
rm -rf ip.txt.swp
i=0
#i小于count 那就执行里面的命令,0满足了要求然后+1
while ((i< $count)) 
do

i=`expr $i + 1`
#打印这一行,在他前面添加标记,就是1 
sed "${i}s/^/&${i} /g" ip.txt >>ip.txt.swp
IP=`awk -v I="$i" '{if(I=$1)print $2}' ip.txt.swp`
scp -f $1 root@${IP}:$2
#下面这调代码是让两个目录保持一致,如果要使用下面这条那就把上面这条注释掉开启下面这条即可
#比如执行命令sh auto_scp.sh /tmp/ /tmp/ 这儿时原目录和目标目录,如果提示命令没找到那就yu个rsync即可
#rsync -aP --delete $1 root@${IP}:$2
done

或者



 

猜你喜欢

转载自kangh.iteye.com/blog/2305665