linux 学习笔记-035-日常运维-rsync 介绍及常用选项,rsync 通过 ssh 同步

发布于 2018-01-30  377 次阅读


rsync 工具介绍

多机器互传及备份文件,本地机器不同目录互传文件

可以实现增量备份,需要安装 rsync 包

把文件从一个目录同步到另一目录并命名:

[root@am-01:~#] rsync -av /etc/passwd /tmp/1.txt

sending incremental file list

passwd



sent 1346 bytes  received 31 bytes  2754.00 bytes/sec

total size is 1272  speedup is 0.92

把 A 机器文件同步到 B 机器(这里在本机做实验,格式一样的)

格式:rsync -av 需拷贝的文件 B 机器用户名@B 机器 IP 地址:B 机器目录或文件名

格式详解:

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
[root@am-01:~#] rsync -av /etc/passwd root@172.17.1.240:/tmp/2.txt

The authenticity of host '172.17.1.240 (172.17.1.240)' can't be established.

ECDSA key fingerprint is 7b:ec:d2:f6:f1:9f:f6:78:e1:4c:42:2f:cb:1b:37:dc.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.17.1.240' (ECDSA) to the list of known hosts.

root@172.17.1.240's password:

sending incremental file list

passwd



sent 1346 bytes  received 37 bytes  102.44 bytes/sec

total size is 1272  speedup is 0.92

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

例子:

01:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest

[root@am-01:~#] ls

001.txt.bz2  1.txt  321.tar      3.txt      aminglinux.log     awk     grep  test.txt

002.txt      2.txt  321.tar.bz2  3.txt.bak  anaconda-ks.cfg.1  bb.txt  sed   yum.log

1            321    321.tar.gz   aa.txt     a.txt              b.txt   test

[root@am-01:~#] cd 321/

[root@am-01:~/321#] ls

1  1.txt  2  234  2.txt  aming3  aminglinux111.bak  aminglinux.bak  d6z

[root@am-01:~/321#] ls /tmp/

1.cap      1.txt  aminglinux  yum_save_tx.2018-01-04.22-47.mxafCK.yumtx

1.tar.bz2  2.txt  d6z         yum_save_tx.2018-01-04.22-50.R1nBlL.yumtx

1.tar.gz   3.txt  test

[root@am-01:~/321#] rsync -av /root/321/ /tmp/321_dest/

sending incremental file list

created directory /tmp/321_dest

./

1.txt

2.txt

aming3 -> /tmp/aming2/

aminglinux.bak

aminglinux111.bak

1/

2/

234/

234/1.txt

234/2.txt

234/aming3 -> /tmp/aming2/

234/aminglinux.bak

234/aminglinux111.bak

234/1/

234/2/

d6z/

d6z/1.txt.bz2

d6z/2.txt

d6z/3.txt

d6z/4.txt



sent 5422888 bytes  received 273 bytes  10846322.00 bytes/sec

total size is 5421341  speedup is 1.00

02:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest,使用-L 参数,同步软链接时会把源文件给同步

[root@am-01:~/321#] ll /root/321

总用量 0

drwxr-xr-x. 2 root root    6 12 月 22 18:24 1

-rw-r--r--. 1 root root    0 12 月 22 18:24 1.txt

drwxr-xr-x. 2 root root    6 12 月 22 18:24 2

drwxr-xr-x. 4 root user1 104 12 月 20 01:14 234

-rw-r--r--. 1 root root    0 12 月 22 18:24 2.txt

lrwxrwxrwx. 1 root root   12 12 月 22 18:24 aming3 -> /tmp/aming2/

-rw-r--r--. 1 root root    0 12 月 22 18:24 aminglinux111.bak

-rw-r--r--. 1 root root    0 12 月 22 18:24 aminglinux.bak

drwxr-xr-x. 2 root root   58 1 月   3 19:46 d6z

[root@am-01:~/321#] ll /tmp/321_dest/

总用量 0

drwxr-xr-x 2 root root    6 12 月 22 18:24 1

-rw-r--r-- 1 root root    0 12 月 22 18:24 1.txt

drwxr-xr-x 2 root root    6 12 月 22 18:24 2

drwxr-xr-x 4 root user1 104 12 月 20 01:14 234

-rw-r--r-- 1 root root    0 12 月 22 18:24 2.txt

lrwxrwxrwx 1 root root   12 12 月 22 18:24 aming3 -> /tmp/aming2/

-rw-r--r-- 1 root root    0 12 月 22 18:24 aminglinux111.bak

-rw-r--r-- 1 root root    0 12 月 22 18:24 aminglinux.bak

drwxr-xr-x 2 root root   58 1 月   3 19:46 d6z

[root@am-01:~/321#] rsync -avL /root/321/ /tmp/321_dest/

sending incremental file list

234/aming3/

aming3/



sent 400 bytes  received 26 bytes  852.00 bytes/sec

total size is 5421317  speedup is 12726.10

[root@am-01:~/321#] ll /tmp/321_dest/

总用量 0

drwxr-xr-x 2 root root    6 12 月 22 18:24 1

-rw-r--r-- 1 root root    0 12 月 22 18:24 1.txt

drwxr-xr-x 2 root root    6 12 月 22 18:24 2

drwxr-xr-x 5 root user1 104 12 月 20 01:14 234

-rw-r--r-- 1 root root    0 12 月 22 18:24 2.txt

drwxr-xr-x 2 root root    6 1 月  25 00:07 aming3

-rw-r--r-- 1 root root    0 12 月 22 18:24 aminglinux111.bak

-rw-r--r-- 1 root root    0 12 月 22 18:24 aminglinux.bak

drwxr-xr-x 2 root root   58 1 月   3 19:46 d6z

03:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest,使用-L 参数,同步软链接时会把源文件给同步,使用参数--delete,删除 DEST 中 SRC 没有的文件

[root@am-01:~#] ls 321/

1  1.txt  2  234  2.txt  aming3  aminglinux111.bak  aminglinux.bak  d6z

[root@am-01:~#] ls /tmp/321_dest/

1  1.txt  2  234  2.txt  aming3  aminglinux111.bak  aminglinux.bak  d6z

[root@am-01:~#] touch /tmp/321_dest/new.txt

[root@am-01:~#] ls /tmp/321_dest/

1  1.txt  2  234  2.txt  aming3  aminglinux111.bak  aminglinux.bak  d6z  new.txt

[root@am-01:~#] rsync -avL --delete /root/321/ /tmp/321_dest/

sending incremental file list

./

deleting new.txt



sent 397 bytes  received 23 bytes  840.00 bytes/sec

total size is 5421317  speedup is 12907.90

04:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest,使用-L 参数,同步软链接时会把源文件给同步,是用参数--exclude 过滤指定文件

[root@am-01:~#] rm -rf /tmp/321_dest/*

[root@am-01:~#] ls /tmp/321_dest/

[root@am-01:~#] rsync -avL --exclude "*.txt" --exclude "aming*" /root/321/ /tmp/321_dest/

sending incremental file list

./

1/

2/

234/

234/1/

234/2/

d6z/

d6z/1.txt.bz2



sent 433074 bytes  received 58 bytes  866264.00 bytes/sec

total size is 432788  speedup is 1.00

[root@am-01:~#] ls /tmp/321_dest/

1  2  234  d6z

05:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest,使用-P 选项,显示同步过程

[root@am-01:~#] rm -rf /tmp/321_dest/*

[root@am-01:~#] rsync -avP /root/321/ /tmp/321_dest/

sending incremental file list

./

1.txt

           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=19/21)

2.txt

           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=18/21)

aming3 -> /tmp/aming2/

aminglinux.bak

           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=16/21)

aminglinux111.bak

           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=15/21)

1/

2/

234/

234/1.txt

           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=10/21)

234/2.txt

           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=9/21)

234/aming3 -> /tmp/aming2/

234/aminglinux.bak

           0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=7/21)

234/aminglinux111.bak

           0 100%    0.00kB/s    0:00:00 (xfer#8, to-check=6/21)

234/1/

234/2/

d6z/

d6z/1.txt.bz2

      432788 100%   47.69MB/s    0:00:00 (xfer#9, to-check=3/21)

d6z/2.txt

     1662843 100%   36.88MB/s    0:00:00 (xfer#10, to-check=2/21)

d6z/3.txt

     1662843 100%   20.33MB/s    0:00:00 (xfer#11, to-check=1/21)

d6z/4.txt

     1662843 100%   14.16MB/s    0:00:00 (xfer#12, to-check=0/21)



sent 5422888 bytes  received 273 bytes  10846322.00 bytes/sec

total size is 5421341  speedup is 1.00

06:把/root/321/目录同步到/tmp/目录下并且修改目录名字为 321_dest,使用-P 选项,显示同步过程,使用-u 选项,如果目标中的文件比源的新,则不同步

[root@am-01:~#] cd /tmp/321_dest/

[root@am-01:/tmp/321_dest#] ls

1  1.txt  2  234  2.txt  aming3  aminglinux111.bak  aminglinux.bak  d6z

[root@am-01:/tmp/321_dest#] vim 1.txt

sdasdsdad

asdasdadad

asdsdsdd

[root@am-01:/tmp/321_dest#] rsync -avPu /root/321/ /tmp/321_dest/

sending incremental file list

./



sent 411 bytes  received 21 bytes  864.00 bytes/sec

total size is 5421341  speedup is 12549.40

[root@am-01:/tmp/321_dest#] cat 1.txt

sdasdsdad

asdasdadad

asdsdsdd

[root@am-01:/tmp/321_dest#] cat /root/321/1.txt

rsync 通过 ssh 同步

和 ssh 有关系,所以需要输入对方机器的密码

01:把本机的/etc/passwd 文件同步到目的机器的/tmp/目录下,并指定文件名为 aming.txt

[root@am-01:~#] rsync -av /etc/passwd 172.17.1.241:/tmp/aming.txt

The authenticity of host '172.17.1.241 (172.17.1.241)' can't be established.

ECDSA key fingerprint is 7b:ec:d2:f6:f1:9f:f6:78:e1:4c:42:2f:cb:1b:37:dc.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.17.1.241' (ECDSA) to the list of known hosts.

root@172.17.1.241's password:

sending incremental file list

passwd



sent 1346 bytes  received 31 bytes  110.16 bytes/sec

total size is 1272  speedup is 0.92

[root@am-02:~#] cd /tmp/

[root@am-02:/tmp#] ls

1.cap      1.tar.gz  2.txt  aminglinux  d6z   yum_save_tx.2018-01-04.22-47.mxafCK.yumtx

1.tar.bz2  1.txt     3.txt  aming.txt   test  yum_save_tx.2018-01-04.22-50.R1nBlL.yumtx

[root@am-02:/tmp#] cat 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

avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

polkitd:x:999:998:User for polkitd:/:/sbin/nologin

tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

aming:x:1000:1000::/home/aming:/bin/bash

ntp:x:38:38::/etc/ntp:/sbin/nologin

user1:x:1001:1001::/home/user1:/bin/bash

user2:x:1002:1002::/home/user2:/bin/bash

user3:x:1003:1005::/home/user3:/bin/bash

user4:x:1004:1005::/home/aming111:/sbin/nologin

user5:x:1005:1006::/home/user5:/bin/bash

user6:x:1007:1007::/home/user6:/bin/bash

tcpdump:x:72:72::/:/sbin/nologin

[root@am-02:/tmp#] ll aming.txt

-rw-r--r-- 1 root root 1272 1 月  17 18:46 aming.txt

02:把目的机器的/tmp/aming.txt 同步到本机的/tmp/目录下,并指定文件名为 123.txt

[root@am-01:~#] ls /tmp/

1.cap      321_dest    test

1.tar.bz2  3.txt       yum_save_tx.2018-01-04.22-47.mxafCK.yumtx

1.tar.gz   aming2      yum_save_tx.2018-01-04.22-50.R1nBlL.yumtx

1.txt      aminglinux

2.txt      d6z

[root@am-01:~#] rsync -avP 172.17.1.241:/tmp/aming.txt /tmp/123.txt

root@172.17.1.241's password:

receiving incremental file list

aming.txt

        1272 100%    1.21MB/s    0:00:00 (xfer#1, to-check=0/1)



sent 30 bytes  received 1354 bytes  251.64 bytes/sec

total size is 1272  speedup is 0.92

[root@am-01:~#] ls /tmp/

123.txt  1.tar.bz2  1.txt  321_dest  aming2      d6z   yum_save_tx.2018-01-04.22-47.mxafCK.yumtx

1.cap    1.tar.gz   2.txt  3.txt     aminglinux  test  yum_save_tx.2018-01-04.22-50.R1nBlL.yumtx

[root@am-01:~#] ll /tmp/123.txt

-rw-r--r-- 1 root root 1272 1 月  17 18:46 /tmp/123.txt

03:把本机的/etc/passwd 文件同步到目的机器的/tmp/目录下,并指定文件名为 aming.txt,使用-e 选项指定 ssh 的端口

[root@am-01:~#] rsync -avP -e "ssh -p 22" /etc/passwd 172.17.1.241:/tmp/aming.txt

root@172.17.1.241's password:

sending incremental file list



sent 31 bytes  received 12 bytes  9.56 bytes/sec

total size is 1272  speedup is 29.58

扩展

使用 ssh 登陆目的机器:

[root@am-01:~#] ssh -p 22 172.17.1.241

root@172.17.1.241's password:

Last login: Wed Jan 24 23:58:05 2018 from 172.17.1.1

[root@am-02:~#]