【CentOS 7笔记47】,rsync文件同步工具#171205

hellopasswd


文件同步工具rsync

[root@localhost ~]# yum install -y rsync
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.tongji.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================
 Package              Arch                  Version                      Repository           Size
===================================================================================================
Installing:
 rsync                x86_64                3.0.9-18.el7                 base                360 k

Transaction Summary
===================================================================================================
Install  1 Package

Total download size: 360 k
Installed size: 732 k
Downloading packages:
rsync-3.0.9-18.el7.x86_64.rpm                                               | 360 kB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : rsync-3.0.9-18.el7.x86_64                                                       1/1 
  Verifying  : rsync-3.0.9-18.el7.x86_64                                                       1/1 

Installed:
  rsync.x86_64 0:3.0.9-18.el7                                                                      

Complete!

[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 1506 bytes  received 31 bytes  3074.00 bytes/sec
total size is 1432  speedup is 0.93

rsync常用选项

  • -a包含-rtplgoD
  • -r同步目录时要加上,类似cp时的-r选项
  • -v同步时显示一些信息,让我们知道同步的过程
  • -l保留软链接
  • -L加上该选项后,同步软链接时会把源文件给同步
  • -p保持文件的权限属性
  • -o保持文件的属主
  • -g保持文件的属组
  • -D保持设备文件信息
  • -t保持文件的时间属性
  • --delete删除DEST中SRC没有的文件
  • --exclude过滤指定文件,如--exclude "logs"会把文件名包含logs的文件或目录过滤掉,不同步
  • -P显示同步过程,比如速率,比-v更加详细
  • -u加上该选项后,如果DEST中的文件比SRC新,则不同步
  • -z传输时压缩

-av

[root@localhost ~]# ls 
111  anaconda-ks.cfg
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
1.txt
2.txt
3.txt

sent 192 bytes  received 72 bytes  528.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls 
111  anaconda-ks.cfg
[root@localhost ~]# ls /tmp/
111_dest  ipt.txt


--delete

[root@localhost ~]# touch /tmp/111_dest/new.txt
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 75 bytes  received 15 bytes  180.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls /tmp/111_dest/
1.txt  2.txt  3.txt  new.txt

[root@localhost ~]# touch /tmp/111_dest/new.txt
[root@localhost ~]# rsync -av --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 75 bytes  received 15 bytes  180.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls /tmp/111_dest/
1.txt  2.txt  3.txt

--exclude

[root@localhost ~]# rm -rf /tmp/111_dest/*
[root@localhost ~]# rsync -av --exclude "1*" /root/111/ /tmp/111_dest/
sending incremental file list
./
2.txt
3.txt

sent 139 bytes  received 53 bytes  384.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls /tmp/111_dest/
2.txt  3.txt

-avP

[root@localhost ~]# rm -rf /tmp/111_dest/*
[root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
1.txt
          18 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
2.txt
          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)
3.txt
           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)

sent 240 bytes  received 72 bytes  624.00 bytes/sec
total size is 36  speedup is 0.12


-avPu

[root@localhost ~]# echo "hello" > 111/1.txt 
[root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
1.txt
           6 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)

sent 121 bytes  received 31 bytes  304.00 bytes/sec
total size is 24  speedup is 0.16
[root@localhost ~]# cat /tmp/111_dest/1.txt 
hello
[root@localhost ~]# echo "hello" >> !$
echo "hello" >> /tmp/111_dest/1.txt
[root@localhost ~]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list

sent 72 bytes  received 12 bytes  168.00 bytes/sec
total size is 24  speedup is 0.29
[root@localhost ~]# cat /tmp/111_dest/1.txt 
hello
hello
[root@localhost ~]# cat 111/1.txt 
hello

rsync通过ssh同步

  • rsync -av test1/ 192.168.133.132:/tmp/test2/
  • rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
  • rsync通过服务的方式同步
  • 要编辑配置文件/etc/rsyncd.conf
  • 启动服务rsync --daemon
  • 格式:rsync -av test1/ 192.168.133.130::module/dir/

Linux主机A:192.168.9.134

[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/
The authenticity of host '192.168.9.223 (192.168.9.223)' can't be established.
ECDSA key fingerprint is 36:5f:cc:8b:04:f9:7b:2e:0f:8c:39:38:37:c3:d2:3a.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes      
Warning: Permanently added '192.168.9.223' (ECDSA) to the list of known hosts.
root@192.168.9.223's password: 
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]

主机B必须也要安装rsync包

[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/
root@192.168.9.233's password: 
sending incremental file list
created directory /root/111_new
./
1.txt
           6 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
2.txt
          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)
3.txt
           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)

sent 228 bytes  received 72 bytes  120.00 bytes/sec
total size is 24  speedup is 0.08


Linux主机B:192.168.9.223

[root@centos11233 ~]# ls
111_new  anaconda-ks.cfg

推文件与拉文件

Linux主机A:192.168.9.134

[root@localhost ~]# rsync -avP root@192.168.9.233:/root/111_new/ /root/111_dest/
root@192.168.9.233's password: 
receiving incremental file list
created directory /root/111_dest
./
1.txt
           6 100%    5.86kB/s    0:00:00 (xfer#1, to-check=2/4)
2.txt
          12 100%   11.72kB/s    0:00:00 (xfer#2, to-check=1/4)
3.txt
           6 100%    5.86kB/s    0:00:00 (xfer#3, to-check=0/4)

sent 71 bytes  received 233 bytes  67.56 bytes/sec
total size is 24  speedup is 0.08
[root@localhost ~]# ls
111  111_dest  anaconda-ks.cfg

指定端口-e "ssh -p 端口号"

[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd root@192.168.9.233:/root/1.txt
root@192.168.9.233's password: 
sending incremental file list
passwd
        1432 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 1506 bytes  received 31 bytes  614.80 bytes/sec
total size is 1432  speedup is 0.93

扩展

[root@localhost ~]# ssh -p 22 192.168.9.233
root@192.168.9.233's password: 
Last failed login: Wed Dec  6 03:34:39 CST 2017 from 192.168.9.134 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Dec  6 00:33:06 2017 from 192.168.9.1
[root@centos11233 ~]# 
[root@centos11233 ~]# exit
logout
Connection to 192.168.9.233 closed.


修改于 171205

转载于:https://my.oschina.net/hellopasswd/blog/1585347

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值