八周二次课

八周二次课
10.28 rsync工具介绍
10.29/10.30 rsync经常使用选项
10.31 rsync经过ssh同步
shell

10.28 rsync工具介绍vim

  • 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@tianqi-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwdbash

sent 908 bytes  received 31 bytes  1878.00 bytes/sec
total size is 834  speedup is 0.89
[root@tianqi-01 ~]# 服务器

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

好比,拷贝到对方机器root用户下 。而后在root后加@IP,: 冒号分开,再跟文件的根路径,而后输入用户密码。ssh

  • rsync -av /etc/passwd root@192.168.11.136:/tmp/1.txt //将/etc/passwd文件拷贝到root用户192.168.11.136IP地址下,并用 : 冒号分开,再跟文件的根路径。

[root@tianqi-01 ~]# rsync -av /etc/passwd root@192.168.11.136:/tmp/1.txt
The authenticity of host '192.168.11.136 (192.168.11.136)' can't be established.
ECDSA key fingerprint is SHA256:PKJdyfN44llC2Qcso4YHJZY3t0J2e5X9ieo6aAqESDs.
ECDSA key fingerprint is MD5:9a:f4:73:a0:b0:03:dc:3d:3d:bc:82:99:a9:77:7d:ec.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.11.136' (ECDSA) to the list of known hosts.
root@192.168.11.136's password: 
sending incremental file list工具

sent 31 bytes  received 12 bytes  0.79 bytes/sec
total size is 834  speedup is 19.40
[root@tianqi-01 ~]# post

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经常使用选项spa

  • 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 传输时压缩,zip

rsync命令,同步目录

  • rsync -av /root/111 /tmp/111_dest //同步一个目录
    • 在同步目录的时候,在目录的最后面加一个斜杠/

[root@tianqi-01 ~]# ls
111  anaconda-ks.cfg  shell
[root@tianqi-01 ~]# ls /tmp/
1.txt
ipt.txt
ks-script-nmOxqc
systemd-private-6ccef82c50a846f89ae3067d5221e3dd-vgauthd.service-bPHCKX
systemd-private-6ccef82c50a846f89ae3067d5221e3dd-vmtoolsd.service-cq1HUx
systemd-private-7099e9c34e244f709586a6e09bf81c9f-vgauthd.service-LumV5m
systemd-private-7099e9c34e244f709586a6e09bf81c9f-vmtoolsd.service-V1e07b
systemd-private-846951eecfef4f91b8a2418c0476aaa3-vgauthd.service-qQIUrB
systemd-private-846951eecfef4f91b8a2418c0476aaa3-vmtoolsd.service-E7ezhw
systemd-private-a87ee16c09514da291c40bb760f53134-vgauthd.service-UFKYFZ
systemd-private-a87ee16c09514da291c40bb760f53134-vmtoolsd.service-OBbET1
systemd-private-bd23fa9ede5b41dd9a61a49f5262e569-vgauthd.service-z7ntVY
systemd-private-bd23fa9ede5b41dd9a61a49f5262e569-vmtoolsd.service-7A9nPi
yum.log
[root@tianqi-01 ~]# rsync -av /root/111/ /tmp/111_dest/        //同步一个目录
sending incremental file list
created directory /tmp/111_dest
./
a.txt
b.txt.net

sent 139 bytes  received 53 bytes  384.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 ~]# 3d

在加入参数 -L 后,会把参数 -l 的含义给覆盖掉

  • -L会把软连接所指向的源文件给拷贝过去

[root@tianqi-01 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list

sent 58 bytes  received 12 bytes  140.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 ~]# 

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

  • rsync -avL --delete /root/111/ /tmp/111_dest/
    • --delete会把多余的文件除去

[root@tianqi-01 ~]# ls 111/
a.txt  b.txt
[root@tianqi-01 ~]# ls /tmp/111_dest/
a.txt  b.txt
[root@tianqi-01 ~]# touch /tmp/111_dest/new.txt
[root@tianqi-01 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/

sending incremental file list
./
deleting new.txt

sent 61 bytes  received 15 bytes  152.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 ~]# ls !$            //新建立的new.txt已经删除
ls /tmp/111_dest/
a.txt  b.txt
[root@tianqi-01 ~]# 

rsync命令,过滤全部txt文件

[root@tianqi-01 ~]# rm -rf /tmp/111_dest/*

[root@tianqi-01 ~]# ls 111/
1  a.txt  b.txt
[root@tianqi-01 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
1/

sent 46 bytes  received 19 bytes  130.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 ~]# 

  • 可屡次过滤文件

[root@tianqi-01 ~]# ls 111/
1  aming  a.txt  b.txt
[root@tianqi-01 ~]# rm -rf /tmp/111_dest/*
[root@tianqi-01 ~]# rsync -avL --exclude "*.txt" --exclude "aming*" /root/111/ /tmp/111_dest/

sending incremental file list
./
1/

sent 50 bytes  received 19 bytes  138.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 ~]# 

  • 在添加文件后,再次同步,会只同步里面不相同的文件,而相同的文件则不会再次同步

[root@tianqi-01 ~]# cd 111
[root@tianqi-01 111]# touch 6.dest 123
[root@tianqi-01 111]# rsync -avL --exclude "*.txt" --exclude "aming*" /root/111/ /tmp/111_dest/

sending incremental file list
./
123
6.dest

sent 152 bytes  received 54 bytes  412.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 111]# 

rsync命令,参数-P

  • rsync -avP /root/111/ /tmp/111_dest/
    • 在传输过程当中,会告诉你传输了多少,传输的速度是多少

[root@tianqi-01 111]# rm -rf /tmp/111_dest/*
[root@tianqi-01 111]# rsync -avP /root/111/ /tmp/111_dest/

sending incremental file list
./
123
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=5/7)
6.dest
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=4/7)
a.txt
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=3/7)
aming
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/7)
b.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=1/7)
1/

sent 322 bytes  received 114 bytes  872.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 111]# 

rsync命令,参数-u

[root@tianqi-01 111]# cd !$
cd /tmp/111_dest/
[root@tianqi-01 111_dest]# ls
1  123  6.dest  aming  a.txt  b.txt
[root@tianqi-01 111_dest]# vim a.txt            //在a.txt中添加内容
[root@tianqi-01 111_dest]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
a.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/7)

sent 163 bytes  received 35 bytes  396.00 bytes/sec
total size is 0  speedup is 0.00

[root@tianqi-01 111_dest]# cat a.txt 
[root@tianqi-01 111_dest]# vim a

aming  a.txt  
[root@tianqi-01 111_dest]# vim a.txt 
[root@tianqi-01 111_dest]# rsync -avPu /root/111/ /tmp/111_dest/

sending incremental file list
./

sent 124 bytes  received 16 bytes  280.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 111_dest]# cat a.txt 
afdafa
svsfwef
sgwesd
srwf
[root@tianqi-01 111_dest]# cat /root/111/a.txt 
[root@tianqi-01 111_dest]# 

rsync命令,参数-z

  • 在远程传输不少文件的时候,加上-z 参数,能够节省带宽,增长速度的

[root@tianqi-01 111_dest]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
a.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/7)

sent 157 bytes  received 32 bytes  378.00 bytes/sec
total size is 0  speedup is 0.00
[root@tianqi-01 111_dest]# 

10.31 rsync经过ssh同步

  • 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命令,将文件传输到另外一台虚拟机

  1. 在终端打开两个不一样ip的虚拟机,而且两个虚拟机是能够互通ping通的

在CentOS7.4的虚拟机中,ping另外一台虚拟机

[root@tianqi-01 111_dest]# ping 192.168.11.137
PING 192.168.11.137 (192.168.11.137) 56(84) bytes of data.
64 bytes from 192.168.11.137: icmp_seq=1 ttl=64 time=0.593 ms
64 bytes from 192.168.11.137: icmp_seq=2 ttl=64 time=0.250 ms
^C
--- 192.168.11.137 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.250/0.421/0.593/0.172 ms
[root@tianqi-01 111_dest]# 

在CentOS7.2的虚拟机中,ping另外一台虚拟机

[root@tianqi ~]# ping 192.168.11.136
PING 192.168.11.136 (192.168.11.136) 56(84) bytes of data.
64 bytes from 192.168.11.136: icmp_seq=1 ttl=64 time=0.407 ms
64 bytes from 192.168.11.136: icmp_seq=2 ttl=64 time=0.262 ms
^C
--- 192.168.11.136 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.262/0.334/0.407/0.074 ms
[root@tianqi ~]# 

在CentOS7.4的终端虚拟机上,将文件传输到CentOS7.2的虚拟机上

  • 前提:在两个虚拟机上都安装rsync包——>yum install -y rsync

在CentOS7.4的终端虚拟机上,将文件传输到CentOS7.2的虚拟机上

[root@tianqi-01 111_dest]# rsync -av /etc/passwd 192.168.11.137:/tmp/aming.txt
root@192.168.11.137's password:     //输入192.168.11.137虚拟机的密码,就是CentOS7.2的密码
sending incremental file list
passwd

sent 908 bytes  received 31 bytes  208.67 bytes/sec
total size is 834  speedup is 0.89
[root@tianqi-01 111_dest]# 

在CentOS7.2虚拟机上查看

[root@tianqi tmp]# ls /tmp/aming.txt
/tmp/aming.txt
[root@tianqi tmp]# cat !$
cat /tmp/aming.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
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
[root@tianqi tmp]# 

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

  • 将CentOS7.2机器中的文件传输到本机上

[root@tianqi-01 111_dest]# rsync -avP 192.168.11.137:/tmp/aming.txt /tmp/123.txt
root@192.168.11.137's password: 
receiving incremental file list
aming.txt
         834 100%  814.45kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 916 bytes  57.33 bytes/sec
total size is 834  speedup is 0.88
[root@tianqi-01 111_dest]# 

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

  • rsync -avP -e "ssh -p 22" /etc/passwd 192.168.11.137:/tmp/hanfeng.txt //指定对方的22端口,就能够链接对面的22端口

[root@tianqi-01 111_dest]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.11.137:/tmp/aming.txt 
root@192.168.11.137's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  2.46 bytes/sec
total size is 834  speedup is 19.40
[root@tianqi-01 111_dest]# 

ssh命令

  • ssh -p 22 192.168.11.137 //链接ip为192.168.11.137的虚拟机
    • 输入exit退出

[root@tianqi-01 111_dest]# ssh -p 22 192.168.11.137
root@192.168.11.137's password: 
Last login: Sat Jan 27 16:09:21 2018 from 192.168.11.1
[root@tianqi ~]# exit
logout
Connection to 192.168.11.137 closed.
[root@tianqi-01 111_dest]# 

友情连接:阿铭Linux

相关文章
相关标签/搜索