Linux基础(day37)

10.28 rsync工具介绍

Linux文件同步工具-rsync

  • rsync -av /etc/passwd /tmp/1.txt
  • rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt
  • rsync格式
  • rsync [OPTION] … SRC DEST
  • rsync [OPTION] … SRC [user@]host:DEST
  • rsync [OPTION] … [user@]host:SRC DEST
  • rsync [OPTION] … SRC [user@]host::DEST
  • rsync [OPTION] … [user@]host::SRC DEST

rsync工具介绍

  • 安装rsync包——>yum install -y rsync
  • rsync命令,它是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
    • 比如,有A、B目录,想要把A目录里面的数据拷贝到B目录下去,并且A目录中数据一直在更新
    • 需求,每小时拷贝一次
    • 实现,可使用rsync命令
      • 优势:实现增量的拷贝,并支持远程
  • rsync -av /etc/passwd /tmp/1.txt //把/etc/passwd文件拷贝到/tmp下并改名为1.txt
    • -a参数中包含了很多选项,后面会详细介绍
    • -v查看到可视化过程
      • 查看到发送了多少个字节
      • 多少字节每秒
      • 文件一共有多大
      • 速度是多少

rsync命令

[root@hanfeng ~]# rsync -av /etc/passwd /tmp/1.txt    //把/etc/passwd文件拷贝到/tmp下并改名为1.txt 
sending incremental file list
passwd

sent 957 bytes  received 31 bytes  1976.00 bytes/sec
total size is 883  speedup is 0.89
[root@hanfeng ~]# 

rsync命令,远程命令同步/拷贝

-比如,拷贝到对方机器root用户下 - 然后在root后加IP,并用 : 冒号分开,再跟文件的根路径

- 并输入root用户的密码
  • rsync -av /etc/passwd root@192.168.202.130:/tmp/1.txt //将/etc/passwd文件拷贝到root用户192.168.202.130IP地址下,并用 : 冒号分开,再跟文件的根路径
[root@hanfeng ~]# rsync -av /etc/passwd root@192.168.202.130:/tmp/1.txt    //将/etc/passwd文件拷贝到root用户192.168.202.130IP地址下,并用 : 冒号分开,再跟文件的根路径
root@192.168.202.130's password:         //这里写用户的密码
sending incremental file list

sent 31 bytes  received 12 bytes  0.43 bytes/sec
total size is 883  speedup is 20.53
[root@hanfeng ~]# 

rsync格式

  • rsync [OPTION] … SRC DEST
    • [OPTION]表示它的选项
    • SRC表示源目录
    • DEST表示目标目录,或者是目标文件
  • rsync [OPTION] … SRC [user@]host:DEST //拷贝到远程的服务器上去
    • user@可省略,那就会默认当前终端的用户
  • rsync [OPTION] … [user@]host:SRC DEST //先写远程的机器/目录,然后拷贝到本地的目录下
  • rsync [OPTION] … SRC [user@]host::DEST //这里的两个冒号,可以是目标,可以是源
  • rsync [OPTION] … [user@]host::SRC DEST

10.29/10.30 rsync常用选项

Linux文件同步工具-rsync

  • rsync常用选项
    • -a 包含-rtplgoD参数选项
    • -r 同步目录时要加上,类似cp时的-r选项
    • -v 同步时显示一些信息,让我们知道同步的过程
    • -l 保留软连接
      • 若是拷贝的原目录里面有一个软链接文件,那这个软链接文件指向到了另外一个目录下
      • 在加上-l,它会把软链接文件本身拷贝到目标目录里面去
    • -L 加上该选项后,同步软链接时会把源文件给同步
    • -p 保持文件的权限属性
    • -o 保持文件的属主
    • -g 保持文件的属组
    • -D 保持设备文件信息
      • /dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用
    • -t 保持文件的时间属性
    • --delete 删除DEST中SRC没有的文件
    • --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
    • -P 显示同步过程,比如速率,比-v更加详细
    • -u 加上该选项后,如果DEST中的文件比SRC新,则不同步
      • update
    • -z 传输时压缩

rsync命令,同步目录

  • rsync -av /root/111 /tmp/111_dest //同步一个目录
    • 在同步目录的时候,在目录的最后面加一个斜杠/
[root@hf-01 ~]# ls 111
a.txt  haha  hanfeng
[root@hf-01 ~]# ls /tmp/
[root@hf-01 ~]# rsync -av /root/111/ /tmp/111_dest/    //同步一个目录
sending incremental file list
created directory /tmp/111_dest
./
a.txt
haha/
hanfeng/

sent 256312 bytes  received 42 bytes  512708.00 bytes/sec
total size is 256144  speedup is 1.00
[root@hf-01 ~]# 

  • 在加入参数 -L 后,会把参数 -l 的含义给覆盖掉
    • -L会把软链接所指向的源文件给拷贝过去
[root@hf-01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list

sent 88 bytes  received 14 bytes  204.00 bytes/sec
total size is 256144  speedup is 2511.22
[root@hf-01 ~]# 

rsync命令,删除目标中源文件中没有的内容

  • rsync -avL --delete /root/111/ /tmp/111_dest/
    • --delete会把多余的文件除去
[root@hf-01 ~]# ls 111/
a.txt  haha  hanfeng
[root@hf-01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng
[root@hf-01 ~]# touch /tmp/111_dest/new.txt
[root@hf-01 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 91 bytes  received 17 bytes  216.00 bytes/sec
total size is 256144  speedup is 2371.70
[root@hf-01 ~]# ls /tmp/111_dest/
a.txt  haha  hanfeng
[root@hf-01 ~]#  

rsync命令,过滤所有txt文件

[root@hf-01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
2.txt.swp
4913
haha/
hanfeng/

sent 184 bytes  received 61 bytes  490.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 ~]# 
  • 可多次过滤文件
[root@hf-01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@hf-01 ~]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
4913
haha/
hanfeng/

sent 131 bytes  received 42 bytes  346.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 ~]# 

  • 在添加文件后,再次同步,会只同步里面不相同的文件,而相同的文件则不会再次同步
[root@hf-01 ~]# cd 111
[root@hf-01 111]# touch 6.dest 123
[root@hf-01 111]# rsync -avL --exclude "*.txt" --exclude="2*" /root/111/ /tmp/111_dest/
sending incremental file list
./
123
6.dest

sent 187 bytes  received 55 bytes  484.00 bytes/sec
total size is 0  speedup is 0.00
[root@hf-01 111]# 

rsync命令,参数-P

  • rsync -avP /root/111/ /tmp/111_dest/
    • 在传输过程中,会告诉你传输了多少,传输的速度是多少
[root@hf-01 ~]# !rm
rm -rf /tmp/111_dest/*
[root@hf-01 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
123
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
2.txt.swp
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=5/8)
4913
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=4/8)
6.dest
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=3/8)
a.txt
      256144 100%   53.26MB/s    0:00:00 (xfer#5, to-check=2/8)
haha/
hanfeng/

sent 256522 bytes  received 118 bytes  513280.00 bytes/sec
total size is 256144  speedup is 1.00
[root@hf-01 ~]# 

rsync命令,参数-u

[root@hf-01 ~]# cd /tmp/111_dest/
[root@hf-01 111_dest]# ls
123  2.txt.swp  4913  6.dest  a.txt  haha  hanfeng
[root@hf-01 111_dest]# vim 4913
在4913中添加内容

[root@hf-01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 145 bytes  received 17 bytes  324.00 bytes/sec
total size is 256144  speedup is 1581.14
[root@hf-01 111_dest]# cat 4913
dsgsdfascs
dsafszcdrw
etfbcgrhc
cbcvbtyegvdgdh
gxdgdfhch
[root@hf-01 111_dest]# cat /root/111/4913
[root@hf-01 111_dest]# 

rsync命令,参数-z

  • 在远程传输很多文件的时候,加上-z 参数,可以节省带宽,增加速度的
[root@hf-01 111_dest]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
4913
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=4/8)

sent 178 bytes  received 33 bytes  422.00 bytes/sec
total size is 256144  speedup is 1213.95
[root@hf-01 111_dest]# 

10.31 rsync通过ssh同步

Linux文件同步工具-rsync

  • 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/

rsync命令,将文件传输到另一台虚拟机

  1. 在终端打开两个不同ip的虚拟机,并且两个虚拟机是可以互通ping通的
在hf的虚拟机中,ping另一台虚拟机
[root@hf-01 ~]# ping 192.168.74.130
PING 192.168.74.130 (192.168.74.130) 56(84) bytes of data.
64 bytes from 192.168.74.130: icmp_seq=1 ttl=64 time=1.42 ms
64 bytes from 192.168.74.130: icmp_seq=2 ttl=64 time=0.873 ms
^C
--- 192.168.74.130 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1004ms
rtt min/avg/max/mdev = 0.873/1.147/1.422/0.276 ms
[root@hf-01 ~]# 


在hf-02的虚拟机上,ping第一台虚拟机
[root@hf-02 ~]# ping 192.168.74.129
PING 192.168.74.129 (192.168.74.129) 56(84) bytes of data.
64 bytes from 192.168.74.129: icmp_seq=1 ttl=64 time=1.05 ms
64 bytes from 192.168.74.129: icmp_seq=2 ttl=64 time=0.725 ms
^C
--- 192.168.74.129 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.725/0.891/1.058/0.169 ms
[root@hf-02 ~]# 

  1. 在hf的终端虚拟机上,将文件传输到hf-02的虚拟机上
    • 前提:在两个虚拟机上都安装rsync包——>yum install -y rsync
在hf的虚拟上,传文件到hf-02的虚拟机上
[root@hf-01 ~]# rsync -av /etc/passwd 192.168.74.130:/tmp/hanfeng.txt
root@192.168.74.130's password:          //输入192.168.74.130虚拟机的密码,就是hf-02的密码
sending incremental file list
passwd

sent 1100 bytes  received 31 bytes  323.14 bytes/sec
total size is 1026  speedup is 0.91
[root@hf-01 ~]# 


在hf-02虚拟机上查看
[root@hf-02 ~]# ls /tmp/hanfeng.txt
/tmp/hanfeng.txt
[root@hf-02 ~]# cat !$
cat /tmp/hanfeng.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:1000:1000::/home/mysql:/bin/bash
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@hf-02 ~]# 

rsync命令,将另一台虚拟机文件传输到本机上

  • 将hf-02机器中的文件传输到本机上
[root@hf-01 ~]# rsync -avP 192.168.74.130:/tmp/hanfeng.txt /tmp/12345.txt
root@192.168.74.130's password:        
receiving incremental file list
hanfeng.txt
        1026 100% 1001.95kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 1110 bytes  78.62 bytes/sec
total size is 1026  speedup is 0.90
[root@hf-01 ~]# 

rsync命令,参数-e 指定端口传输文件

  • rsync -avP -e "ssh -p 22" /etc/passwd 192.168.74.130:/tmp/hanfeng.txt //指定对方的22端口,就可以连接对面的22端口
[root@hf-01 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.74.130:/tmp/hanfeng.txt
root@192.168.74.130's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  7.82 bytes/sec
total size is 1026  speedup is 23.86
[root@hf-01 ~]# 

ssh命令

  • ssh -p 22 192.168.74.130 //连接ip为192.168.74.130的虚拟机
    • 输入exit退出
[root@hf-01 ~]# ssh -p 22 192.168.74.130
root@192.168.74.130's password: 
Last login: Wed Dec  6 05:14:27 2017 from 192.168.74.1
[root@hf-02 ~]# exit
登出
Connection to 192.168.74.130 closed.
[root@hf-01 ~]# 

转载于:https://my.oschina.net/u/3707314/blog/1585086

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值