RSYNC:
rsync(Remote sync)是linux系統自帶的鏡像備份工具,支持本地複製和遠程複製。
sync類型:
- sync:同步
- async:異步
- rsync:遠程同步
- inotify:可以監控目錄,文件系統,刪除,創建,修改文件屬性
特點:
- 增量複製,第一次數據同步爲全量複製,之後爲增量複製。
- 支持匿名複製,也支持身份驗證。
- 可以鏡像目錄樹,文件系統。
格式:
#客戶端拉取
rsync [option] src user@ip_address dest push
#服務端推送
rsync [option] user@ip_address src dest pull
option:選項
-a:代表以下所有的選項(-v除外)。
-r:遞歸同步。
-o:同步數據時不會修改文件的屬主。
-g:同步數據時不會修改文件的屬組。
-p:同步數據時不會修改文件的權限。
-z:同步數據時對文件進行壓縮。
-v:顯示同步的詳細信息。
--delete:同步鎖,如果目標目錄中與源目錄中的文件有不一致,則自動刪除不一致的內容。
-L:同步數據時,如果存在軟鏈接文件,則同步軟鏈接的源文件,不同步鏈接文件。
-l:同步鏈接文件。
src:源目地址
dest:目標地址
user:用戶
ip_address:ip地址
主機 | 網絡信息 |
---|---|
Host1 | ens33:192.168.43.23 |
Host2 | ens33;192.168.43.136 |
遞歸同步文件:
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
9d:c3:2f:4f:8c:c6:65:18:a5:31:f4:35:f5:40:38:83 root@localhost
The key's randomart image is:
+--[ RSA 2048]----+
| .+..o=..|
| E*+. o.|
| o .o .|
| o + |
| S * o |
| . B |
| = + |
| . + |
| . |
+-----------------+
[root@localhost ~]# ssh-copy-id [email protected]
The authenticity of host '192.168.43.136 (192.168.43.136)' can't be established.
ECDSA key fingerprint is 47:04:b9:ed:39:e9:58:4d:2b:b3:39:76:ad:7a:c2:d3.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# ssh [email protected]
Last login: Wed Nov 20 13:50:29 2019 from desktop-a1km2p9
[root@localhost ~]# exit
logout
Connection to 192.168.43.136 closed.
[root@localhost ~]# mkdir /rsync_dir
[root@localhost ~]# rpm -qa | grep rsync
rsync-3.0.9-17.el7.x86_64
[root@localhost ~]# mkdir /rsync_dir
Host2:
[root@localhost ~]# mkdir /rsync_dir
Host1:
[root@localhost ~]# rsync -rv /rsync_dir/* [email protected]:/rsync_dir/
sending incremental file list
a/
a/b/
a/b/c/
sent 65 bytes received 24 bytes 178.00 bytes/sec
total size is 0 speedup is 0.00
Host2:
[root@localhost ~]# mkdir /rsync_dir
[root@localhost ~]# cd /rsync_dir/
[root@localhost rsync_dir]# ls
a
[root@localhost rsync_dir]# cd a
[root@localhost a]# ls
b
[root@localhost a]# cd b
[root@localhost b]# ls
c
同步鏈接文件:
Host1:
[root@localhost ~]# cd /rsync_dir/
[root@localhost rsync_dir]# touch aa
[root@localhost rsync_dir]# ln -s aa ee
[root@localhost rsync_dir]# ls
a aa ee
[root@localhost rsync_dir]# rsync -lv /rsync_dir/* [email protected]:/rsync_dir/
skipping directory a
aa
ee -> aa
sent 83 bytes received 34 bytes 234.00 bytes/sec
total size is 2 speedup is 0.02
Host2:
[root@localhost b]# cd ../../
[root@localhost rsync_dir]# ls
a aa ee
同步權限:
Host1:
[root@localhost rsync_dir]# chmod 777 aa
[root@localhost rsync_dir]# rsync -pv /rsync_dir/* [email protected]:/rsync_dir/
skipping directory a
aa
skipping non-regular file "ee"
sent 77 bytes received 31 bytes 216.00 bytes/sec
total size is 2 speedup is 0.02
Host2:
[root@localhost rsync_dir]# ll
total 0
drwxr-xr-x. 3 root root 15 Nov 20 13:54 a
-rwxrwxrwx. 1 root root 0 Nov 20 13:58 aa
lrwxrwxrwx. 1 root root 2 Nov 20 13:56 ee -> aa
同步屬主和屬組:
Host1:
[root@localhost rsync_dir]# useradd rsync
[root@localhost rsync_dir]# chown rsync:rsync aa
[root@localhost rsync_dir]# cat /etc/passwd | grep rsync
rsync:x:1001:1001::/home/rsync:/bin/bash
[root@localhost rsync_dir]# rsync -gov /rsync_dir/* [email protected]:/rsync_dir/
skipping directory a
aa
skipping non-regular file "ee"
sent 101 bytes received 31 bytes 264.00 bytes/sec
total size is 2 speedup is 0.02
Host2:
[root@localhost rsync_dir]# ll
total 0
drwxr-xr-x. 3 root root 15 Nov 20 13:54 a
-rwxrwxrwx. 1 1001 1001 0 Nov 20 14:02 aa
lrwxrwxrwx. 1 root root 2 Nov 20 13:56 ee -> aa
如果同步的數據有不一致的內容則刪除:
Host2:
[root@localhost rsync_dir]# touch cc
Host1:
[root@localhost rsync_dir]# touch bb
[root@localhost rsync_dir]# rsync -av --delete /rsync_dir/ [email protected]:/rsync_dir/
sending incremental file list
./
deleting cc
aa
bb
a/
a/b/
a/b/c/
sent 240 bytes received 68 bytes 616.00 bytes/sec
total size is 2 speedup is 0.01
Host2:
[root@localhost rsync_dir]# ls
a aa bb ee
安裝inotify
[root@localhost ~]# tar -zxvf inotify-tools-3.14.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
[root@localhost inotify-tools-3.14]# ln -s /usr/local/inotify/bin/* /usr/local/bin/
選項:
- -m:一直處於監控狀態。
- -r:遞歸監控。
- -q:將監控目錄信息顯示在終端上。
- -e:指定監控的事件。
- –format:指定輸出事件的格式。
事件:
- create:創建。
- move:移動。
- delete:刪除。
- modify:修改。
- close_write:修改文件內容。
- close_nowrite:查看只讀文件。
事件格式:
-
%w:產生監控的路徑。
-
%f:監控爲目錄,輸出目錄名。
-
%e:事件名稱。
-
%T:輸出當前的事件。
[root@localhost inotify-tools-3.14]# inotifywait -mrq --format %w%f -e create,delete,close_write /rsync_dir
[root@localhost ~]# touch /rsync_dir/www #新開一個終端來創建文件
[root@localhost inotify-tools-3.14]# inotifywait -mrq --format %w%f -e create,delete,close_write /rsync_dir #返回上一個終端查看輸出內容
/rsync_dir/www
/rsync_dir/www
整合:
[root@localhost inotify-tools-3.14]# vim /etc/rsyncd.conf
port = 873
address = 192.168.43.23
uid = root
gid = root
use chroot = no #當其他用戶訪問該主機,沒有root的權限
max connections = 0
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes #開啓日誌
log file = /var/log/rsync.log #定義日誌文件存放路徑
#timeout = 900
ignore nonreadable = yes #忽略沒有權限的用戶
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #壓縮文件不進行壓縮
[test] #同步模塊的名稱
path = /rsync_dir #真實路徑
comment = rsync_dir #說明
read only = no #關閉只讀
[root@localhost inotify-tools-3.14]# systemctl restart rsyncd
[root@localhost inotify-tools-3.14]# netstat -anput | grep 873
tcp 0 0 192.168.43.23:873 0.0.0.0:* LISTEN 40416/rsync
[root@localhost inotify-tools-3.14]# cd /
[root@localhost /]# vim rsyncd.sh
[root@localhost /]# vim rsyncd.sh
#!/bin/bash
path=/rsync_dir
client_ip=192.168.43.136
/usr/local/bin/inotifywait -mrq --format %w%f -e create,delete,close_write /rsync_dir | while read file
do
if [ -f $file ];then
/usr/bin/rsync -av --delete $file root@$client_ip:$path
else
/usr/bin/cd $path && /usr/bin/rsync -av --delete ./ root@$client_ip:$path
fi
done
[root@localhost /]# chmod +x rsyncd.sh
[root@localhost /]# ./rsyncd.sh &
[1] 40563
[root@localhost /]# touch /rsync_dir/testfile
[root@localhost /]# sending incremental file list
testfile
sent 76 bytes received 31 bytes 71.33 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 37 bytes received 12 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00