Shell常用脚本:文件或目录一键同步到多台服务器

注意: 将本地文件,同步到【/opt/module/script/xsyncByFileIp.txt】里面的目标机器

xsyncByFile.sh

sh 复制代码
#!/bin/bash

# 入参参数个数
argsCount=$#

if(($argsCount==0)); then

echo "同步失败:请输入待同步的文件或者目录"
exit;
fi


echo "待同步的文件:$1"

# 绝对路径
syncArgFile=$1
# 仅获取文件名,不带父路径
syncArgFileName=`basename $syncArgFile`


#if[! -e "$syncArgFile"]; then
if [ ! -e "$syncArgFile" ]; then
echo "同步失败:入参的文件或目录【$syncArgFile】不存在,请检查"
exit
fi


# 获取入参文件的父目录,以及进行入参文件所在的目录
syncArgFileNameParentDirPath=`cd -P $(dirname $syncArgFile) && pwd`
echo "当前所在目录:$syncArgFileNameParentDirPath"


xsyncByFileIpPath=/opt/module/script/xsyncByFileIp.txt
xsyncByFileIpContentRow=`cat xsyncByFileIp.txt  | wc -l`

if(($xsyncByFileIpContentRow==0)); then

echo "同步失败:请在$xsyncByFileIpPath 填入需要同步到的目标机器,每行内容是一个目标机器"
exit
fi



while read ip; do
  echo -e "\n\n===============$ip===============" 
  echo "正在将文件同步到目标机器【$ip】"

  rsync -rvl $syncArgFileNameParentDirPath/$syncArgFileName root@$ip:$syncArgFileNameParentDirPath

  echo -e "文件同步到目标机器【$ip】完成"
  echo -e "======================================" 
done < "$xsyncByFileIpPath"



echo -e  "\n\n同步结束\n\n"

使用示例:

sh 复制代码
sh xsyncByFile.sh /test
相关推荐
哈基咪怎么可能是AI4 分钟前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行17 小时前
Linux和window共享文件夹
linux
木心月转码ing1 天前
WSL+Cpp开发环境配置
linux
蝎子莱莱爱打怪2 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀2 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应2 天前
vi编辑器使用
linux·后端·操作系统
何中应2 天前
Linux进程无法被kill
linux·后端·操作系统
何中应2 天前
rm-rf /命令操作介绍
linux·后端·操作系统
何中应2 天前
Linux常用命令
linux·操作系统
葛立国2 天前
从 / 和 /dev 说起:Linux 文件系统与挂载点一文理清
linux