10.28 rsync 工具介绍
rsync 是一个实用的同步工具
需求: A 目录的数据实时更新, 将 A 的数据拷贝到 B 目录, 每隔一小时拷贝一次
使用 cp 命令:
1 在 A 目录中找出最近 1 小时新增的内容拷贝到 B(比较麻烦)
2 直接拷贝 A 目录的所有内容将 B 目录覆盖 (浪费时间和磁盘 IO)
cp 命令不适用
rsync 可以实现增量更新 (rsync 只会同步新增和有内容变更的文件);
支持远程同步 (将数据同步到不同 ip 的另一台机器上);
安装 rsync
[root@hyc-01-01 ~]# yum install -y rsync
将 passwd 同步到 tmp 目录下并改名为 1.txt
[root@hyc-01-01 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
- passwd
- sent 938 bytes received 35 bytes 1,946.00 bytes/sec
- total size is 846 speedup is 0.87
-a 包含多个选项
-v 可视化, 可以看到拷贝的过程
将文件同步到远程机器的特定目录
[root@hyc-01-01 ~]# rsync -av /etc/passwd root@192.168.31.128:/tmp/1234.txt
The authenticity of host '192.168.31.128 (192.168.31.128)' can't be established.
ECDSA key fingerprint is SHA256:0SErfGbbc3AfFcxC92Tav9X/T/bOn8wfnvum/wnw5Xs.
ECDSA key fingerprint is MD5:b7:d4:e4:4a:4a:33:29:99:1a:2e:45:94:d9:40:17:fb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.31.128' (ECDSA) to the list of known hosts.
root@192.168.31.128's password:
sending incremental file list
- passwd
- sent 938 bytes received 35 bytes 67.10 bytes/sec
- total size is 846 speedup is 0.87
其中 root@可以省略, 此时使用执行当前命令的用户作为登录用户
将远程机器的某文件拷贝到当前机器某个目录下
- [root@hyc-01-01 ~]# rsync -av root@192.168.31.128:/etc/passwd /tmp/passwddd
- root@192.168.31.128's password:
receiving incremental file list
- passwd
- sent 43 bytes received 938 bytes 130.80 bytes/sec
- total size is 846 speedup is 0.86
- [root@hyc-01-01 ~]# ls /tmp/passwddd
- /tmp/passwddd
- 7.18 10.28-10.31
来源: http://www.bubuko.com/infodetail-2690923.html