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
相关推荐
17(无规则自律)15 分钟前
LubanCat 2烧录一个新镜像后开发环境搭建
linux·嵌入式硬件·考研·软件工程
DeeplyMind29 分钟前
第14章 挂载宿主机目录(Bind Mount)(最常用,重要)
运维·docker·云原生·容器·eureka
FJW0208141 小时前
《Nginx 进阶实战:配置详解、站点发布与常用功能大全》(2)
运维·nginx
『往事』&白驹过隙;1 小时前
浅谈PC开发中的设计模式搬迁到ARM开发
linux·c语言·arm开发·设计模式·iot
DeeplyMind2 小时前
第19章 Docker Compose进阶
运维·docker·容器
srhtrnbdfg3 小时前
Discuz!NT负载均衡方案
运维·负载均衡
Hello.Reader4 小时前
从 0 到 1 理解硬盘数据恢复工具原理与工程实现
linux·运维·服务器·网络·数据库
『往事』&白驹过隙;5 小时前
C/C++中的格式化输出与输入snprintf&sscanf
linux·c语言·c++·笔记·学习·iot·系统调用
Je1lyfish5 小时前
CMU15-445 (2026 Spring) Project#1 - Buffer Pool Manager
linux·数据库·c++·后端·链表·课程设计·数据库架构
好好学习天天向上~~5 小时前
12_Linux学习总结_进程地址空间(虚拟地址)
linux·学习