rsync+inotify 实时备份

目标服务器:

yum -y install rsync
vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.password

[backup]
path = /mnt2/backup/39.97.84.214
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users =  root

#说明:
vim /etc/rsyncd.conf  //增加以下内容
log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.password #用户认证配置文件,里面存放用户名称和密码,必须手动创建这个文件
 
 [backup]  #自定义同步名称
   path = /mnt2/backup/39.97.84.214 #rsync服务端存放路径,客户端的数据将同步到此目录
  comment = sync etc from client
  uid = root  #设置rsync运行权限为root
  gid = root   #设置rsync运行权限为root
  port = 873    #默认端口为873
  ignore errors  #表示出现错误忽视错误
  use chroot = no  #默认为true ,修改为no,增加对目录软链接的备份
  read only = no #设置rsync服务端为读写权限
  list = no  #不显示rsync服务端资源列表
  max connections = 200 #最大连接数
  timeout = 600  #设置超时时间
  auth users = admin  #执行数据同步的用户名,可以设置多个,用英文逗号隔开
 # hosts allow =  #允许进行数据同步的IP地址,可以设置多个,用英文逗号隔开
  #hosts deny = 192.168.24.188  #禁止进行数据同步的IP地址,可以设置多个,用英文逗号隔开

echo 'root:518' > /etc/rsync.password
chmod 600 /etc/rsync*
systemctl start rsyncd

源服务器:

yum install -y rsync
echo '518' > /etc/rsync.pass
chmod 600 /etc/rsync.pass
mkdir /tmp/test
cd /tmp/test/
echo 222 >ee.txt
ehco 333 >23.txt
#传输到目标服务器,注意防火墙
rsync -avH --port 873 --progress --delete /tmp/test/ root@39.107.83.68::backup --password-file=/etc/rsync.pass
#查看系统内核支持inotify吗
[root@iZ2zehl1ro7yd08bzf0h3sZ runLog]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jun 15 13:15 max_queued_events
-rw-r--r-- 1 root root 0 Jun 15 13:15 max_user_instances
-rw-r--r-- 1 root root 0 Jun 15 13:15 max_user_watches
#安装inotify
yum -y install make gcc gcc-c++ inotify-tools
[root@iZ2zehl1ro7yd08bzf0h3sZ runLog]# 
 
#/bin/bash
host=39.107.83.68
src=/data/www/bibf-site/api-server/catalina/runLog
des=backup  #目标服务器模块名
password=/etc/rsync.pass
user=root
inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
 do
    rsync -avzP --delete  --timeout=100 --password-file=${password}  $src  $user@$host::$des
    echo "${files} was rsynced" >>/var/log/rsync-backup.log 2>&1
 done

同步多个服务器的数据目录:

目标服务器:

[root@svn-zentao-redmine runLog]# cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
log file = /var/log/rsyncd.log 
pidfile = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
secrets file = /etc/rsync.password 
 
[39.97.84.214]  
path = /mnt2/backup/39.97.84.214 
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600 
auth users =  root
 # hosts allow =  #允许进行数据同步的IP地址,可以设置多个,用英文逗号隔开
 #   #hosts deny = 192.168.24.188  #禁止进行数据同步的IP地址,可以设置多个,用英文逗号隔开
[39.97.88.214]  
path = /mnt2/backup/39.97.88.214 
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600 
auth users =  root


[39.97.82.249]  
path = /mnt2/backup/39.97.82.249 
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600
auth users =  root


[39.97.82.171]  
path = /mnt2/backup/39.97.82.171
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600
auth users =  root



[8.210.115.60]  
path = /mnt2/backup/8.210.115.60
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600
auth users =  root


[8.210.146.160]  
path = /mnt2/backup/8.210.146.160
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600
auth users =  root


[8.210.126.24]  
path = /mnt2/backup/8.210.126.24
comment = sync etc from client
uid = root  
gid = root   
port = 873    
ignore errors  
use chroot = no  
read only = no 
list = no  
max connections = 200 
timeout = 600
auth users =  root