数据同步RSYNC

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章