您的位置:首页 > 运维架构 > Linux

linux下文件同步脚本

2016-03-16 11:45 543 查看
文件传输在linux下是很容易通过scp协议和命令实现的,本脚本是同步集群的文件夹的文件,也可以单独同步某个机器。

各个机器直接最好设置无密登陆,否则中间会不断的要求输入登陆密码。

脚本如下:

#! /bin/bash
#目的主机的登录名
USERNAME=hadoop

function synfiles()
{

host=$2
dir=$(ls $1); #获取需要同步文件列表
for file in $dir
do
fs=$1/$file;
echo $fs;
if [ -f $fs ];
then
scp  $fs $USERNAME@$host:$fs;
elif [ -d $fs ];
then
scp -r  $fs $USERNAME@$host:$fs;
fi

done
}
function gethost(){
path=$1
#获取同步主机列表,目前是从/etc/host中获取
hosts=$(awk '/slave/ {print $2}' /etc/hosts)
for host in $hosts
do
echo 'beginning synchronize $host files'
synfiles $path $host
done

}
dir=
if [ ! $1 ];then
dir=/var/log
else
dir=$1
fi
echo 'synic files in'$dir
#同步指定的主机
if [ $2 ];then
host=$2
echo "beginning synchronize ${host} files"
synfiles $path $2
else
#同步默认设置的主机列表
gethost $dir
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: