7.18 10.28-10.31
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.
[email protected]'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 [email protected]:/etc/passwd /tmp/passwddd
[email protected]'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