linux 文件同步工具 - rsync
rsync, 它是一个同步工具他非常实用, 也非常重要我们几乎每天都要用它, 比如传输文件, 远程备份数据他也可以在本机备份, 作用类似与命令 cp, 但又区别于 cp 比如我们要拷贝一个文件 A, 将它命名为 B 但 A 每小时更新一次, 如果用 cp 命令将会很麻烦这时我们用 rsync 就可以 很好的解决这个问题, 他可以增量拷贝, 它只拷贝新增的文件
本机同步拷贝
我们要将 passwd 这个文件同步到 / tmp/1.txt 中
- [root@linletao-001 ~]# rsync -av /etc/passwd /tmp/1.txt
- sending incremental file list
- passwd
- sent 1233 bytes received 31 bytes 2528.00 bytes/sec
- total size is 1159 speedup is 0.92
这样就完成了
远程同步拷贝
我们要将 passwd 这个文件远程同步到 root 用户下的 / tmp/1.txt 中
- [root@linletao-001 ~]# rsync -av /etc/passwd root@192.168.1.106:/tmp/1.txt
- root@192.168.1.106s password:
- sending incremental file list
- sent 31 bytes received 12 bytes 9.56 bytes/sec
- total size is 1159 speedup is 26.95
这就是远程同步的格式, 我们需要在目标文件前面加上用户名和 IP, 在用户名和 IP 之间用 @隔开 IP 和目标文件之间要用: 隔开
rsync 还有其他格式
rsync [OPTION](选项) SRC(源目录) DEST(目标文件或者目录)
rsync [OPTION] SRC(源目录) [user@]host:DEST(源目录), 其中 [user@] 是用户名, host 是目标 IP 我们也可以简写不加用户名, 这样就是当前用户
rsync [OPTION] [user@]host:SRC DEST
这是将远程的目标文件拷贝到本机上
rsync 常用选项
-a 包含 - rtplgoD 平时我们加 - a 就足够了
-r 同步目录时要加上, 类似 cp 时的 - r 选项
-v 可视化
-l 保留软连接如果源目录有软连接文件, 加上 l 的话就会把软连接文件本身拷贝到目标文件, 两边相同
-L 同步软链接时会把源文件给同步
-p 保持文件属性
-o 保持文件的属主
-g 保持文件属组
-D 保持设备文件信息
-t 保持文件的时间属性
--delete 删除 DEST 中 SRC 没有的文件删除目标目录中源目录没有的文件比如同步 a 文件到 b 文件, 如果要这两个要保持一模一样得到状态, 假如 b 设备内有一些 a 目录没有的文件, 那么我们就要加 --delete, 来让他们同步
--exclude 过滤指定文件, 如 --exclude logs 会把文件名包含 logs 的文件或者目录过滤掉, 不同步他也支持通配
-P 显示同步过程, 比如速率, 比 - v 更加详细
-u 加上该选项后, 如果 DEST(目标目录)中的文件比 SRC(源目录)新, 则不同步比如 a 设备中的 1 文件比 b 设备的 1 文件新, 那么就不会同步
-z 传输时压缩这样节省带宽
举例说明
将 root 下的 111 目录同步到 / tmp 下并且改名字叫 111_dest
rsync -av /root/111$tmp/111_dest/, 这里提一个要求, 以后要同步目录的话就在目录的最后面加一个 "/", 目标后面也要加 /
- [root@linletao-001 111]# rsync -av /root/111$tmp/111_dest/
- sending incremental file list
- created directory /tmp/111_dest
- ./
- .111
- 1.txt
- 2.txt
- llt.txt -> test
- sent 127 bytes received 18 bytes 290.00 bytes/sec
- total size is 3248 speedup is 22.40
然后我们在参数中加 L
- [root@linletao-001 ~]# rsync -avL /root/111$tmp/111_dest/
- sending incremental file list
- sent 116 bytes received 12 bytes 256.00 bytes/sec
- total size is 3244 speedup is 25.34
L 它的作用就是将软连接所指向的源文件复制过来
我们要将 111 目录中的文件同步到 / tmp/111_dest, 但是 / tmp/111_dest 多了一个 new.txt 文件, 如果要使两边同步, 就要加参数 --delete
首先我们先查看一下两边的文件是否同步
- [root@linletao-001 ~]# ls 111/
- 1.txt 2.txt llt.txt test.txt
- [root@linletao-001 ~]# ls /tmp/111_dest/
- 1.txt 2.txt llt.txt new.txt test.txt
我们发现 / 111_dest / 多了一个 new.txt 文件
然后我们开始同步, 加参数 --delete 让两边同步
- [root@linletao-001 ~]# rsync -avL --delete /root/111$tmp/111_dest/
- sending incremental file list
- ./
- deleting new.txt
- sent 119 bytes received 15 bytes 268.00 bytes/sec
- total size is 3278 speedup is 24.46
我们发现同步的过程已经删除了 new.txt 这个文件
然后我们查询一下
- [root@linletao-001 ~]# ls /tmp/111_dest/
- 1.txt 2.txt llt.txt test.txt
我们发现 / tmp/111_dest 中的 new.txt 文件也被删除了这就是 --delete 的作用
我们要将 111 中格式为 txt 文件给过滤出来
- [root@linletao-001 111]# rsync -avL --exclude "*.txt" /root/111$tmp/111_dest/
- sending incremental file list
- ./
- 456
- 789/
- sent 128 bytes received 38 bytes 332.00 bytes/sec
- total size is 1065 speedup is 6.42
这样就将格式为 txt 的文件给过滤出来的而且它支持写多个 --exclude
显示同步的详细过程
- [root@linletao-001 ~]# rsync -avP /root/111$tmp/111_dest/
- sending incremental file list
- ./
- 1.txt
- 1159 100% 0.00kB/s 0:00:00 (xfer#1, to-check=5/8)
- 2.txt
- 1020 100% 996.09kB/s 0:00:00 (xfer#2, to-check=4/8)
- 456
- 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
- llt.txt -> test.txt
- test.txt
- 17 100% 16.60kB/s 0:00:00 (xfer#4, to-check=1/8)
- 789/
- sent 2538 bytes received 98 bytes 5272.00 bytes/sec
- total size is 3269 speedup is 1.24
这样就将每一个文件同步的详细过程给列出来了
还有一种情况, 我们的目标文件要新于源文件, 那么在同步的是后就会将源文件同时更改, 如果这样, 也许会造成源文件损坏而无法使用功能, 这样的情况下我们在同步时加一个参数 u 就可以保护我们的源文件了
rsync -avPu /root/111$tmp/111_dest/
在我们远程同步比较大的文件时加一个 z, 这样可以在同步过程中压缩文件, 这样可以节省带宽
rsync -avPz /root/111$tmp/111_dest/
rsync 通过 ssh 方式同步
比如我们要将 a 设备的 / etc/passwd 同步到 b 设备的 / tmp/aming.txt 中条件是 b 设备也要安装 rsync 这个工具
- [root@linletao-001 ~]# rsync -av /etc/passwd 192.168.1.109:/tmp/aming.txt
- root@192.168.1.109s password:(输入目标设备的 root 密码)
- sending incremental file list
- passwd
- sent 1233 bytes received 31 bytes 280.89 bytes/sec
- total size is 1159 speedup is 0.92
这就可以了, 这叫做推文件
我们也可以从 b 设备的文件同步到 a 设备, 格式就是先将 b 设备的 ip 和要同步的目录写到前面, 而本机不用写 ip, 直接写目标目录即可
- [root@linletao-001 ~]# rsync -avP 192.168.1.109:/tmp/aming.txt /tmp/123.txt
- root@192.168.1.109s password:(输入本机 root 密码)
- receiving incremental file list
- aming.txt
- 1159 100% 1.11MB/s 0:00:00 (xfer#1, to-check=0/1)
- sent 30 bytes received 1241 bytes 363.14 bytes/sec
- total size is 1159 speedup is 0.91
这样就可以了这也叫做拉文件
还有一种情况, 对方的端口并不是 22, 这时候我们加一个选项 - e ssh -p 22, 这样就可以指定端 22 口了
- [root@linletao-001 ~]# rsync -avP -e "ssh -p 22" 192.168.1.109:/tmp/aming.txt /tmp/123.txt
- root@192.168.1.109s password:
- receiving incremental file list
- sent 11 bytes received 39 bytes 7.69 bytes/sec
- total size is 1159 speedup is 23.18
通过 ssh -p 加对方 ip 即可连接对方
- [root@linletao-001 ~]# ssh -p 22 192.168.1.109
- root@192.168.1.109s password:
- Last login: Tue Mar 27 20:53:10 2018 from 192.168.1.106
来源: http://www.bubuko.com/infodetail-2540988.html