当前位置: 代码迷 >> 综合 >> rsync+inotify 进行数据同步
  详细解决方案

rsync+inotify 进行数据同步

热度:69   发布时间:2024-01-09 05:49:05.0

说明:将192.168.1.234的/data/pkgs目录中的数据同步到192.168.1.237的/data/backup目录

====================================================================================================
一、安装rsync (目的端 192.168.1.237)
# wget -c http://www.samba.org/ftp/rsync/rsync-3.0.9.tar.gz
# tar -xvzf rsync-3.0.9.tar.gz
# cd rsync-3.0.9
# ./configure -prefix=/usr/local/rsync -disable-ipv6
# make
# make install

# mkdir -p /usr/local/rsync/var
# mkdir -p /usr/local/rsync/etc

# vim /usr/local/rsync/etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 30
log file = /usr/local/rsync/var/rsyncd.log
pid file = /usr/local/rsync/var/rsyncd.pid
lock file = /usr/local/rsync/var/rsyncd.lock
motd file = /usr/local/rsync/etc/rsyncd.motd
port = 873
timeout = 300
hosts allow = 192.168.1.234

[webdata]
read only = no
list = yes
ignore errors
comment = backup web data
path = /data/backup
auth users = backup
secrets file = /usr/local/rsync/etc/rsyncd.pwd

# chmod 600 /usr/local/rsync/etc/rsyncd.conf

# echo "welcome to here" > /usr/local/rsync/etc/rsyncd.motd

# echo "backup:QWEIOP123890" >> /usr/local/rsync/etc/rsyncd.pwd
# chmod 600 /usr/local/rsync/etc/rsyncd.pwd

# mkdir -p /data/backup
# /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/etc/rsyncd.conf

====================================================================================================
二、安装rsync (源端 192.168.1.234)
# wget -c http://www.samba.org/ftp/rsync/rsync-3.0.9.tar.gz
# tar -xvzf rsync-3.0.9.tar.gz
# cd rsync-3.0.9
# ./configure -prefix=/usr/local/rsync -disable-ipv6
# make
# make install

# mkdir -p /usr/local/rsync/etc
# echo "QWEIOP123890" >> /usr/local/rsync/etc/rsyncd.pwd
# chmod 600 /usr/local/rsync/etc/rsyncd.pwd

====================================================================================================
三、安装inotify (源端 192.168.1.234)
# wget -c http://sourceforge.net/projects/inotify-tools/files/latest/download?source=files
# tar -xvzf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure -prefix=/usr/local/inotify
# make
# make install

# vim /usr/local/inotify/rsync.sh 
#!/bin/bash

src='/data/pkgs/'
user='backup'
dst='webdata'
ip=192.168.1.237
passwd='/usr/local/rsync/etc/rsyncd.pwd'

inotifyd='/usr/local/inotify/bin/inotifywait'
rsyncd='/usr/local/rsync/bin/rsync'

${inotifyd} -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file
do
    ${rsyncd} -vzrtopg --delete --progress --bwlimit=500 ${src} ${user}@${ip}::${dst} --password-file=${passwd}
done

# chmod +x /usr/local/inotify/rsync.sh
# echo 'nohup /usr/local/inotify/rsync.sh &' >> /etc/rc.local

测试:
# nohup /usr/local/inotify/rsync.sh&
然后在/data/pkgs目录下随便创建一个文件

====================================================================================================
四、相关内核参数修改
默认的几个内核参数:
fs.inotify.max_user_instances = 128
//指定了每一个real user ID可创建的inotify instatnces的数量上限

fs.inotify.max_queued_events = 16384
//调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,
//超出这个值得事件被丢弃,但会触发IN_Q_OVERFLOW事件
//文件系统变化越频繁,这个值就应该越大

fs.inotify.max_user_watches = 8192
//指定了每个inotify instance相关联的watches的上限,
//大小取决于要备份的这部分文件系统包含多少目录

# echo 'fs.inotify.max_user_instances = 20480' >> /etc/sysctl.conf
# echo 'fs.inotify.max_queued_events = 409600' >> /etc/sysctl.conf
# echo 'fs.inotify.max_user_watches = 40960' >> /etc/sysctl.conf

# sysctl -p

本文章转载于http://sofar.blog.51cto.com/353572/775239

  相关解决方案