惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Vercel News
Vercel News
L
LangChain Blog
B
Blog RSS Feed
Y
Y Combinator Blog
IT之家
IT之家
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
V
V2EX
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
有赞技术团队
有赞技术团队
D
Docker
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
月光博客
月光博客

张小凯的博客

再见2025,你好2026! 下一代提示词工程语言POML简明教程 开了一个新的专门学习日语的博客 一、并行编程导论与CUDA入门 一些免费的GPU资源 debian12部署kubernetes-1.28集群 RSS订阅工具Folo使用 多平台消息推送工具ntfy使用 AppleScript介绍与简单实战 uv使用 gemini-cli使用 跑步一年多的一些总结和感想 通过GithubActions拉取并推送Docker镜像到国内云 使用AnythingLLM+SillconFlow+Milvus快速搭建个人云端知识库 さよなら2024、こんにちは2025! Excel通过身份证号列计算性别 开源的个人书籍管理系统Talebook 2024年安装Docker的方法 Python项目Linter、Formatter和Github-Actions配置
分享两个服务器实用脚本:xsync和xcall
2025-07-21 · via 张小凯的博客

    1. 分享两个服务器实用脚本:xsync和xcall
      1. 文件同步:xsync
        1. 1、前置依赖
        2. 2、编写脚本
        3. 3、使用
      2. 命令执行:xcall
    2. 附录

如果同时需要维护多台服务器,可能会需要在多台服务器之间同步文件、执行命令。

本文介绍了两个简单的脚本实现这一功能!

源代码:


分享两个服务器实用脚本:xsync和xcall

文件同步:xsync

1、前置依赖

xsync 依赖于 rsync 工具,可以通过 yum 或者 apt 简单的安装:

apt或yum install -y rsync

此外,还需要配置 SSH 无密码登录!


2、编写脚本

脚本内容:

xsync

#!/bin/bash
# Dependency:
#  1. rsync: yum/apt install -y rsync
#  2. password-less SSH login
#

# 0. Define server list
servers=("server-1" "server-2" "server-3")

# 1. check param num
if [ $# -lt 1 ]; then
  echo "Not Enough Arguement!"
  exit 1
fi

# 2. traverse all machines
for host in "${servers[@]}"; do
  echo "====================  $host  ===================="
  # 3. traverse dir for each file
  for file in "$@"; do
    # 4. check file exist
    if [ -e "$file" ]; then
      # 5. get parent dir
      pdir=$(cd -P "$(dirname "$file")" && pwd)
      # 6. get file name
      fname=$(basename "$file")
      ssh "$host" "mkdir -p $pdir"
      rsync -av "$pdir/$fname" "$host:$pdir"
    else
      echo "$file does not exist!"
    fi
  done
done

使用时,上面的文件中 servers 数组中的配置,为你服务器集群!


3、使用

增加可执行权限、并将文件放在 PATH 下;

然后直接使用,例如:

xsync ~/.bashrc

命令执行:xcall

和 xsync 类似,编写:

xcall

#!/bin/bash
# Dependency: password-less SSH login
#

# Define server array (easily extensible)
servers=(
  "server-1"
  "server-2"
  "server-3"
)

# Check if command arguments are provided
if [ $# -eq 0 ]; then
  echo "Error: Please provide a command to execute" >&2
  exit 1
fi

# Execute command across all servers
for server in "${servers[@]}"; do
  echo "--------- $server ----------"

  # Execute remote command and handle errors
  if ssh "$server" "$*"; then
    echo "✓ Command executed successfully"
  else
    echo "✗ Command failed on server: $server" >&2
    # Uncomment below line to exit script on first failure
    # exit 1
  fi
done

使用也是类似:

增加可执行权限、并将文件放在 PATH 下;

然后直接使用,例如:

xcall ls

附录

源代码: