rsync 命令以及脚本使用

rsync是什么?

rsync 是一个远程同步工具

下载

你的集群每一台都需要下载!(也就是你需要同步的机器)

yum install -y xsync

如果其他不下载就是报错的这样(使用脚本的情况下,注意这里是提示 rsync没有找到,不是xsync)

[root@master bin]# xsync /opt/test/
==================== master ====================
sending incremental file list

sent 97 bytes  received 17 bytes  76.00 bytes/sec
total size is 10  speedup is 0.09
==================== slave1 ====================
bash: rsync: 未找到命令
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]
==================== slave2 ====================
bash: rsync: 未找到命令
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2]

优点与scp对比

优点:rsync主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。
与scp对比:rsync 要比scp复制速度快,因为 rsync 是复制有差异性的文件(它会自己检查),而scp 则是把所有的文件统一复制过去,达到的是一个 统统覆盖的意义

纯命令 Chat- GPT-3.5 演示:

rsync 是一个用于文件同步和备份的工具,可以在本地或远程系统之间高效地进行文件的复制和同步。以下是 rsync 命令的基本用法示例以及一些常用选项:

rsync [options] source destination
其中 source 表示源文件或目录的路径,destination 表示目标文件或目录的路径。

常用的选项包括:

-a:归档模式,用于保持文件的属性、权限和时间等。
-v:详细输出模式,显示正在复制的文件和目录。
-r:递归复制目录及其内容。
-z:传输时压缩数据,可以减少传输时间。
--delete:删除目标中不存在于源的文件。
--exclude:排除指定的文件或目录。
-e:指定远程 shell 的路径,通常用于远程传输。

以下是一些示例:

本地文件同步:

rsync -av /path/to/source /path/to/destination

本地目录同步:

rsync -av /path/to/source/ /path/to/destination/

从本地到远程服务器同步:

rsync -av /path/to/source username@remote_server:/path/to/destination

从远程服务器到本地同步:

rsync -av username@remote_server:/path/to/source /path/to/destination

同步时排除特定文件或目录:

rsync -av --exclude='*.log' /path/to/source /path/to/destination

使用压缩传输:

rsync -avz /path/to/source /path/to/destination

请注意,rsync 的选项和用法非常丰富,你可以根据需要进行组合和定制。在使用之前,建议查阅相关文档或执行 man rsync 命令来获取更详细的信息和帮助。

脚本

脚本来自这位仁兄: https://blog.csdn.net/qq_63756815/article/details/127016471
1、在 /bin 目录下创建 xsync文件脚本
2、需要修改的是在 第二步,修改为自己的主机名
3、修改 文件权限为 777,也可以单加一个可执行权限(x):
chmod 777 xsync
4、

#!/bin/bash
 
#1. 判断参数个数
if [ $# -lt 1 ]
then
    echo Not Enough Arguement!
    exit;
fi
 
#2. 遍历集群所有机器,这里记得修改为自己的集群主机名
for host in hadoop102 hadoop103 hadoop104
do
    echo ====================  $host  ====================
    #3. 遍历所有目录,挨个发送
 
    for file in $@
    do
        #4. 判断文件是否存在
        if [ -e $file ]
            then
                #5. 获取父目录
                pdir=$(cd -P $(dirname $file); pwd)
 
                #6. 获取当前文件的名称
                fname=$(basename $file)
                ssh $host "mkdir -p $pdir"
                rsync -av $pdir/$fname $host:$pdir
            else
                echo $file does not exists!
        fi
    done
done

演示:
脚本:

[root@master bin]# xsync /opt/test/
==================== master ====================
sending incremental file list

sent 97 bytes  received 17 bytes  76.00 bytes/sec
total size is 10  speedup is 0.09
==================== slave1 ====================
sending incremental file list
test/
test/a1.txt
test/a2.txt

sent 200 bytes  received 58 bytes  172.00 bytes/sec
total size is 10  speedup is 0.04
==================== slave2 ====================
sending incremental file list
test/
test/a1.txt
test/a2.txt

sent 200 bytes  received 58 bytes  516.00 bytes/sec
total size is 10  speedup is 0.04

纯命令:

[root@master bin]# echo 2222 > /opt/test/a2.txt 
[root@master bin]# rsync -av /opt/test/ slave1:/opt/test/
sending incremental file list
a2.txt

sent 137 bytes  received 41 bytes  118.67 bytes/sec
total size is 10  speedup is 0.06

这里 我专门试了试会不会自动给我创建目录(因为我没学过脚本),发现是会的
但是 如果是纯命令的话,你必须和scp命令相同的语法,就是绝对路径,且只会复制文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

·惊鸿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值