Rsync+inotify实现数据实时同步

需求描述

​ 访问一个异地机房(没有,没有固定公网ip)的实时数据

解决思路

​ 在目的服务器(华为云)安装rsync,配置数据同步模块;源服务器(异地机房)安装rsync和inotify-tools,执行脚本进行文件实时检测同步

操作步骤

​ 目标server:

yum install rsync -y

vim /etc/rsyncd.conf
uid = root 
gid = root 
use chroot = no 
max connections = 10 
strict modes = yes
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
log file = /var/log/rsyncd.log 
[logs] 
path = /data/logs/cdbht/
comment = web file
ignore errors 
read only = no 
write only = no 
hosts allow =  
hosts deny =  
list = false
uid = root 
gid = root 
auth users = loguser 
secrets file = /etc/rsync.pwd
echo "user:password" /etc/rsync.pwd
chmod 600 /etc/rsync.pwd

systemctl start rsyncd
systemctl enable rsyncd

​ 源server:

yum install rsync inotify-tools.x86_64 -y
echo "password" /etc/rsync.pwd
chmod 600 /etc/rsync.pwd

vim rsync.sh #添加同步脚本
#!/bin/bash 
host=目标server公网ip
src=/data1/logs/    
des=logs
user=user
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files 
do 
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pwd $src $user@$host::$des 
echo "${files} was rsynced" >>/var/rsync.log 2>&1 
done
systemctl start rsyncd
systemctl enable rsyncd

chmod +x rsync.sh
nohup ./rsync.sh &

​ 部署站点:

#安装nginx
……
#站点配置文件
 server {
        listen 80;
        server_name xx.xx.com;
        rewrite ^(.*) https://$server_name$1 permanent;
   }

 server {
        listen       443 ssl;
     server_name  xx.xx.com;
    access_log  /var/log/nginx/access.xx.xx.com.log;
        root   /data/logs;
        index  index.html index.htm;
       
        ssl_certificate   cert/server.crt;
        ssl_certificate_key  cert/server.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    charset      utf-8,gbk;
        
        auth_basic "Please input password";
        auth_basic_user_file /etc/nginx/.passwd;
       
        location ~*.log {
    add_header Content-Type text/plain;
    }
}

mime.types文件text/plain后面添加log

​ 登录验证:

 yum -y install httpd-tools
 htpasswd -c /etc/nginx/.passwd iot
 [password]    #输入密码
 
 #server下增加以下配置
        auth_basic "Please input password";
        auth_basic_user_file /etc/nginx/.passwd;

你可能感兴趣的:(Rsync+inotify实现数据实时同步)