您的位置:首页 > 其它

10.28 rsync工具介绍 - 10.29/10.30 rsync常用选项 - 10.31 rsync通过ssh同步

2017-09-19 22:29 666 查看
- 10.28 rsync工具介绍
- 10.29/10.30 rsync常用选项
- 10.31 rsync通过ssh同步

# 10.28 rsync工具介绍
-/A目录 --> /B目录(A目录更新了一个文件,每次更新都需要把A目录拷贝到B目录),如果用cp命令 比较浪费时间,耗费磁盘空间,磁盘压力 读写之类的,

-使用rsync -av /etc/passwd /tmp/1.txt
-a选项就是包含了好几个选项  ,v 是可视化,可以看到拷贝的过程
```
[root@aminglinux-001 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd

sent 1471 bytes  received 31 bytes  3004.00 bytes/sec
total size is 1397  speedup is 0.93
[root@aminglinux-001 ~]#
```
-同时支持远程去拷贝,同步
-rsync -av /etc/passwd root@192.168.202.130:/tmp/1.txt

-rsync 格式      SRC(源文件) DEST(目标目录)
-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

# 10.29 rsync 常用选项 上
-rsync常用选项

-a 包含-rtplgoD

-r同步目录时要加上,类似cp时的-r选项

-v同步是显示一些信息,让我们知道同步的过程

-l保留软链接

-L加上该选项后,同步软链接时会把源文件给同步

-p保持文件的权限属性

-o保持文件的属主

-g保持文件的属组

-D保持设备文件信息

-t保持文件的时间属性

--delete 删除DEST中SRC没有的文件

--exclude 过滤指定文件,如--exclude "logs" 会把文件名包含logs的文件或者目录过滤掉不同步

-P(大P)显示同步过程,比如速率,比-v更加详细

-u加上该选项后,如果DEST中的文件比SRC新,则不同步

-z 传输时压缩

# 10.30 rsync 常用选项 下
-下面来开始测试下
```
[root@aminglinux-001 ~]# ls
111          1.txt      2.txt      aminglinux         biji.txt    test
123          1.txt~     2.txt.bak  anaconda-ks.cfg.1  get-pip.py
1_heard.txt  1.txt.bak  3.txt      awk                grep
1_sorft.txt  234        4.txt      bb.txt             sed
[root@aminglinux-001 ~]# ls 111
12.tx~  12.txt  12_txt.swp  222  4913  aming3
[root@aminglinux-001 ~]# ls -l 111
总用量 16
-rw-r--r--. 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r--. 1 root root    65 7月  23 20:32 12.txt
-rw-r--r--. 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x. 2 root root     6 7月  23 20:23 222
-rw-r--r--. 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx. 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]# ls /tmp/
1.cap
1.txt
my.ipt
systemd-private-fab301f859c4489e8be3f5afe72fed8b-vmtoolsd.service-ItQGyG
[root@aminglinux-001 ~]#
```

-把/root/111/目录 同步到 /tmp/111_dest/ 下并且改名 111_dest
-av的作用就是 拷贝目录、时间属性、属主属组,软链接,通通拷贝到111_test目录下去吗
```
[root@aminglinux-001 ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
.12.txt.swp
.12.txt.swx
12.txt
12.tx~
12_txt.swp
4913
aming3 -> /tmp/aming2
222/

sent 12774 bytes  received 136 bytes  25820.00 bytes/sec
total size is 12364  speedup is 0.96
[root@aminglinux-001 ~]#
```

-下面来对比下俩个目录下的文件,软链接也同步过去了,这就时一个小写的l ,
```
[root@aminglinux-001 ~]# ls -l 111/
总用量 16
-rw-r--r--. 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r--. 1 root root    65 7月  23 20:32 12.txt
-rw-r--r--. 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x. 2 root root     6 7月  23 20:23 222
-rw-r--r--. 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx. 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]# ls -l /tmp/111_dest/
总用量 16
-rw-r--r-- 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r-- 1 root root    65 7月  23 20:32 12.txt
-rw-r--r-- 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x 2 root root     6 7月  23 20:23 222
-rw-r--r-- 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]#
```

-  -L 是同步软链接时把源文件给同步
```
[root@aminglinux-001 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
symlink has no referent: "/root/111/aming3"

sent 141 bytes  received 13 bytes  308.00 bytes/sec
total size is 12353  speedup is 80.21
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
[root@aminglinux-001 ~]# ls -l 111
总用量 16
-rw-r--r--. 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r--. 1 root root    65 7月  23 20:32 12.txt
-rw-r--r--. 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x. 2 root root     6 7月  23 20:23 222
-rw-r--r--. 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx. 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]# touch /tmp/aming2   (由于之前的软链接文件已损坏,所以创建一个软链接文件)
[root@aminglinux-001 ~]# ls -l 111
总用量 16
-rw-r--r--. 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r--. 1 root root    65 7月  23 20:32 12.txt
-rw-r--r--. 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x. 2 root root     6 7月  23 20:23 222
-rw-r--r--. 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx. 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]#
```
-再来试下
```
[root@aminglinux-001 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
aming3

sent 193 bytes  received 32 bytes  450.00 bytes/sec
total size is 12353  speedup is 54.90
[root@aminglinux-001 ~]#

[root@aminglinux-001 ~]# ls -l /tmp/111_dest/
总用量 16
-rw-r--r-- 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r-- 1 root root    65 7月  23 20:32 12.txt
-rw-r--r-- 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x 2 root root     6 7月  23 20:23 222
-rw-r--r-- 1 root root     0 7月  23 20:29 4913
-rw-r--r-- 1 root root     0 9月  13 22:27 aming3
[root@aminglinux-001 ~]#
[root@aminglinux-001 ~]# ls -l 111
总用量 16
-rw-r--r--. 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r--. 1 root root    65 7月  23 20:32 12.txt
-rw-r--r--. 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x. 2 root root     6 7月  23 20:23 222
-rw-r--r--. 1 root root     0 7月  23 20:29 4913
lrwxrwxrwx. 1 root root    11 7月  24 21:17 aming3 -> /tmp/aming2
[root@aminglinux-001 ~]#
```
-在aming2里面写入一些东西
```
[root@aminglinux-001 ~]# echo "12133keidkd" > /tmp/aming2
```
-再重新拷贝下,再看下这个aming3文件
```
[root@aminglinux-001 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
aming3

sent 209 bytes  received 32 bytes  482.00 bytes/sec
total size is 12365  speedup is 51.31
[root@aminglinux-001 ~]# ls -l /tmp/111_dest/
总用量 20
-rw-r--r-- 1 root root     0 7月  23 20:29 12.tx~
-rw-r--r-- 1 root root    65 7月  23 20:32 12.txt
-rw-r--r-- 1 root root 12288 7月  23 20:29 12_txt.swp
drwxr-xr-x 2 root root     6 7月  23 20:23 222
-rw-r--r-- 1 root root     0 7月  23 20:29 4913
-rw-r--r-- 1 root root    12 9月  13 22:32 aming3
[root@aminglinux-001 ~]# ls -l /tmp/111_dest/aming3
-rw-r--r-- 1 root root 12 9月  13 22:32 /tmp/111_dest/aming3
[root@aminglinux-001 ~]# cat !$
cat /tmp/111_dest/aming3
12133keidkd
[root@aminglinux-001 ~]#
```
- [ ] 这就是-L的作用,它可以把软链接所指向的源文件给拷贝过去,

--delete 删除DEST中SRC没有的文件
-111目录 和 111_dest 目录下面的文件是一样的,现在在后者目录创建一个新的文件 new.txt的文件
```
[root@aminglinux-001 ~]# ls 111/
12.tx~  12.txt  12_txt.swp  222  4913  aming3
[root@aminglinux-001 ~]# ls /tmp/111_dest/
12.tx~  12.txt  12_txt.swp  222  4913  aming3

[root@aminglinux-001 ~]# touch /tmp/111_dest/new.txt
[root@aminglinux-001 ~]#
```
-对于源文件111目录来讲,不管你多不多其他文件,只要你对方目录下面有我的文件就可以,但是有时候要求比较严谨,不允许添加任何文件,需要和111目录文件保持一致,-delete 把111_test 中 111目录下没有的文件 给过滤掉
```
[root@aminglinux-001 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt

sent 157 bytes  received 16 bytes  346.00 bytes/sec
total size is 12365  speedup is 71.47
[root@aminglinux-001 ~]#
[root@aminglinux-001 ~]# ls !$
ls /tmp/111_dest/
12.tx~  12.txt  12_txt.swp  222  4913  aming3
[root@aminglinux-001 ~]#

```
- [ ] 过程中有一个deleting new.txt 可以看到把这个new.txt文件给删除了 ,问了和源文件 统一

--exclude 过滤指定文件,如--exclude "logs" 会把文件名包含logs的文件或者目录过滤掉不同步
-做之前把之前111_dest/目录里的文件删掉
```
[root@aminglinux-001 ~]# rm -rf /tmp/111_dest/*
```
-现在我要做一个过滤,把所有的txt文件全部过滤出来,不需要你拷贝txt文件,
```
[root@aminglinux-001 ~]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/

sending incremental file list
./
12.tx~
12_txt.swp
4913
aming3
222/

sent 12605 bytes  received 95 bytes  25400.00 bytes/sec
total size is 12300  speedup is 0.97
[root@aminglinux-001 ~]#
```
- [ ] 没有出现12.txt ,因为添加了--exclude *.txt
- --include 还可以多个添加,再加一个把以aming开头的文件也过滤掉
```
[root@aminglinux-001 ~]# !rm
rm -rf /tmp/111_dest/*

[root@aminglinux-001 ~]# rsync -avL --exclude "*.txt" --exclude "aming*" /root/111/ /tmp/111_dest/
sending incremental file list
./
12.tx~
12_txt.swp
4913
222/

sent 12535 bytes  received 76 bytes  25222.00 bytes/sec
total size is 12288  speedup is 0.97
[root@aminglinux-001 ~]#
```

- -P(大P)显示同步过程,比如速率,比-v更加详细
```
[root@aminglinux-001 ~]# !rm
rm -rf /tmp/111_dest/*
[root@aminglinux-001 ~]# rsync -avP  /root/111/ /tmp/111_dest/
sending incremental file list
./
12.txt
65 100%    0.00kB/s    0:00:00 (xfer#1, to-check=5/9)
12.tx~
0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=4/9)
12_txt.swp
12288 100%   11.72MB/s    0:00:00 (xfer#3, to-check=3/9)
4913
0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/9)
aming3 -> /tmp/aming2
222/

sent 12696 bytes  received 98 bytes  8529.33 bytes/sec
total size is 12364  speedup is 0.97
[root@aminglinux-001 ~]#
```

-u加上该选项后,如果DEST中的文件比SRC新,则不同步
```

[root@aminglinux-001 ~]# cd /tmp/111_dest/
[root@aminglinux-001 111_dest]# ls
12.tx~  12.txt  12_txt.swp  222  4913  aming3
[root@aminglinux-001 111_dest]# vim 4913

kdkkdkdkdkdkdkdkkd
lalslslslslslslsls
aaaaaaaaaaaaaaaaaa
~

~
~
-- 插入 --
[root@aminglinux-001 111_dest]# rsync -avP  /root/111/ /tmp/111_dest/
sending incremental file list
./
4913
0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/9)

sent 212 bytes  received 35 bytes  494.00 bytes/sec
total size is 12364  speedup is 50.06
[root@aminglinux-001 111_dest]#
```
-正常拷贝,更新   目标4913文件里的内容 被源文件4913给覆盖了,所以里面东西没有了
```
[root@aminglinux-001 111_dest]# cat 4913
[root@aminglinux-001 111_dest]#
```
-现在因为目标4913 文件更加新,有一个特殊情况,就是想要目标目录的文件更新一些,有些特殊原因,没有办法编辑111目录下的4913文件,我只需要更新我的目标目录 ,备份目录下的文件就可以了
-如果不加特殊选项u 那也就意味着,所编辑的4913 要被覆盖掉,需要加一个u选项,就可以起到保护的作用,保护目标目录下面给我们更改 更新股的文件,但是、/root/111/4913 依然还是旧的
-使用rsync -avPu  /root/111/ /tmp/111_dest/
```
[root@aminglinux-001 111_dest]# vim 4913
[root@aminglinux-001 111_dest]#

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

sent 173 bytes  received 16 bytes  378.00 bytes/sec
total size is 12364  speedup is 65.42
[root@aminglinux-001 111_dest]# cat 4913
kdkkdkdkdkdkdkdkkd
lalslslslslslslsls
aaaaaaaaaaaaaaaaaa
[root@aminglinux-001 111_dest]# cat /root/111/4913
[root@aminglinux-001 111_dest]#
```

-z 传输时压缩  rsync -avPz  /root/111/ /tmp/111_dest/ (远程传输,尤其是文件很多的时候,加上-z 可以节省带宽和增加速度的)
```
[root@aminglinux-001 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=2/9)

sent 206 bytes  received 32 bytes  476.00 bytes/sec
total size is 12364  speedup is 51.95
[root@aminglinux-001 111_dest]#
```

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

-现在有俩台终端1,终端2 俩台机器是可以互相平通
[root@aminglinux-001 ~]# ping 192.168.202.132
PING 192.168.202.132 (192.168.202.132) 56(84) bytes of data.
64 bytes from 192.168.202.132: icmp_seq=1 ttl=64 time=1.14 ms
64 bytes from 192.168.202.132: icmp_seq=2 ttl=64 time=0.362 ms
64 bytes from 192.168.202.132: icmp_seq=3 ttl=64 time=0.502 ms
^C
--- 192.168.202.132 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.362/0.669/1.143/0.340 ms
[root@aminglinux-001 ~]#

-用终端1,把1下面的/etc/passwd 同步到 2终端的 /tmp/aming.txt下
```
[root@aminglinux-001 ~]# rsync /etc/passwd 192.168.202.132:/tmp/aming.txt
root@192.168.202.132's password:
bash: rsync: 未找到命令
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]
[root@aminglinux-001 ~]#
```
-这边有一个报错信息,rsync未找到命令,默认下,是没有这命令的,需要安装一些

-在终端2下面安装下
```
[root@aminglinux-02 ~]# yum install -y rsync
已加载插件:fastestmirror
Running transaction
正在安装    : rsync-3.0.9-17.el7.x86_64                                  1/1
验证中      : rsync-3.0.9-17.el7.x86_64                                  1/1

已安装:
rsync.x86_64 0:3.0.9-17.el7

完毕!
[root@aminglinux-02 ~]#
```
-再去终端1下面同步 -av 显示过程
```
[root@aminglinux-001 ~]# rsync -av /etc/passwd 192.168.202.132:/tmp/aming.txt
root@192.168.202.132's password:
sending incremental file list
passwd

sent 1471 bytes  received 31 bytes  429.14 bytes/sec
total size is 1397  speedup is 0.93
[root@aminglinux-001 ~]#
```

-来cat一下同步的文件
```
[root@aminglinux-02 ~]# 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-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996: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
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
aming:x:1000:1005::/home/aming:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/aming111:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
user6:x:1008:1010::/home/user6:/bin/bash
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
[root@aminglinux-02 ~]#
```
-这个叫做拉文件,rsync -avP 192.168.202.132:/tmp/aming.txt,把文件拉过来
-第一种叫推文件 , rsync -av /etc/passwd 192.168.202.132:/tmp/aming.txt 把文件推过去
```
[root@aminglinux-001 ~]# rsync -avP 192.168.202.132:/tmp/aming.txt /tmp/123.txtroot@192.168.202.132's password:
receiving incremental file list
aming.txt
1397 100%    1.33MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 1479 bytes  335.33 bytes/sec
total size is 1397  speedup is 0.93
[root@aminglinux-001 ~]#
```

-指定端口
```
[root@aminglinux-001 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.202.132:/tmp/aming.txt
root@192.168.202.132's password:
sending incremental file list

sent 31 bytes  received 12 bytes  12.29 bytes/sec
total size is 1397  speedup is 32.49
[root@aminglinux-001 ~]#
```
-ssh 方式传输数据
```
[root@aminglinux-001 ~]# ssh -p 22 192.168.202.132
root@192.168.202.132's password:
Last login: Thu Sep 14 00:02:08 2017 from 192.168.202.1
[root@aminglinux-02 ~]# 登出
Connection to 192.168.202.132 close
```
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  工具介绍 rsync