rsync������ͬ���ļ���Ŀ¼�Ĺ���
inotify�����ԴĿ¼�ı仯������rsync���仯�����ͬ����Ŀ��Ŀ¼
����������
ϵͳ��rhel 6.3
Դ��������10.1.1.1
Ŀ�ķ�������10.1.1.2
Ŀ�ģ���Դ�����ϵ�/testdirĿ¼ʵʱͬ����Ŀ�ķ�������/testdirĿ¼
iptables����ǽ,selinux�ر�
��Ŀ�ķ�����172.16.1.2�ϲ�����
1����װrsync��������
# yum install -y rsync xinetd
�༭rsync�����ļ�������sync�����Զ�����
# vim /etc/xinetd.d/rsync
service rsync
{
disable = no //��disabled=yes��Ϊno
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
����xinetd����(��xinted����rsync����)
/etc/init.d/xinetd start
2������rsyncd.conf�����ļ�
# 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.pass //�û���֤�ļ�,�����û�������
motd file=/etc/rsyncd.motd //rsync����ʱ���滶ӭ��Ϣ���ļ�
[testdir] //ͬ��Ŀ¼�������,������ͬ��Ŀ¼ͬ��
path=/testdir //���Ŀ¼λ��
comment=testdir
uid=root //����rsync����Ȩ��Ϊroot
gid=root //����rsync����Ȩ��Ϊroot
port=873 //Ĭ�϶˿ں�
use chroot=no //Ĭ��Ϊtrue����ʾ��ͬ���������ļ�����Ϊno��ʾͬ��������
read only=no //����rsync������ļ�Ϊ��дȨ��
list=no //����ʾrsync�������Դ�б�
max connections=200 //���������
timeout=600 //��ʱʱ��
auth users=root //ִ�����ͬ�����û���,�����ö������ͬ�û����ö��Ÿ���
hosts allow=172.16.1.1 //����������ͬ���Ŀͻ���IP��ַ,���IPʹ�ö��Ÿ���
3�������û���֤�ļ�
# vim /etc/rsync.pass
root:redhat
��ʽ �û���:���룬һ��һ���û���Ϣ
4���ֱ������ļ����û���֤�ļ���Ȩ����Ϊ600
# chmod 600 /etc/rsyncd.conf
# chmod 600 /etc/rsync.pass
5����������rsync����
# /etc/init.d/xinetd restart
��Դ�������ϲ�����
1����װrsync�ͻ������
# yum install -y rsync xinetd
�༭rsync�����ļ�������sync�����Զ�����
# vim /etc/xinetd.d/rsync
service rsync
{
disable = no //��disabled=yes��Ϊno
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
����xinetd����(��xinted����rsync����)
/etc/init.d/xinetd start
2������������֤�ļ������ļ�ֻ����Ŀ�ķ������ϵ���֤�û�����
# vim /etc/passwd.txt
redhat
# chmod 600 /etc/passwd.txt
����Դ��������Ŀ�ķ���������Ƿ�ͬ���ɹ�
�ֱ���Դ��������Ŀ�ķ������ϴ���/testdirĿ¼����Դ��������/testdir�д��������ļ���ʹ������ͬ������鿴Ŀ�ķ������Ƿ����ִ������ļ��������Ŀ��������Ͽ��Բ鿴��Դ�������������ļ�������ͬ���ɹ�
# rsync -avH --port=873 --progress --delete /testdir/ root@172.16.1.2::testdir --password-file=/etc/passwd.txt
����˵����
-a���ݹ鴫���ļ����������ļ���������
-v: ��ʾ��ϸ���
-H: ����Ӳ����
--progress����ʾ���ݹ��
--delete��ͬ��ʱ����ɾ��Ŀ�ķ�������Ŀ¼��Դ��������û�е��ļ�
/testdir/: Դ���Ŀ¼�����һ��/��ʾͬ��ʱֻͬ�����ļ���Ŀ¼; ����/����ʾ���/testdirĿ¼����ͬ����Ŀ�������
root@172.16.1.2::testdir�� rootͬ���û�, Ŀ��������е�Ŀ¼ֻ��ҪдĿ¼��Ƽ���
--password-file=/etc/passwd.txt�� ָ�������ļ�
��Դ�������ϰ�װinotify�����ʵʱ���ԴĿ¼�ı仯ʵ��ʵʱͬ��
1���鿴�������ں��Ƿ�֧��inotify��2.6.13�ں˺�ʼ֧��
# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Dec 10 09:26 max_queued_events
-rw-r--r-- 1 root root 0 Dec 10 09:26 max_user_instances
-rw-r--r-- 1 root root 0 Dec 10 09:26 max_user_watches
2����װinotify
# tar zxf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.14
# ./configure --prefix=/usr/local/inotify
# make
# make install
3���Ļ�������PATH����Ϊinotify����Ŀ��ļ���ͷ�ļ�����������
# vim /etc/profile
export PATH=$PATH:/usr/local/inotify/bin
# source /etc/profile
����inotify�Ŀ��ļ�
# echo "/usr/local/inotify/lib" > /etc/ld.so.conf.d/inotify.conf
# ldconfig
����inotify��ͷ�ļ�
ln -s /usr/local/inotify/include /usr/include/inotify
4����inotify�ں˲���(Ĭ�ϵ�inotify����ֵ̫С)
# vim /etc/sysctl.conf
fs.inotify.max_queued_events=9999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
fs.inotify.max_queued_events = 9999999
fs.inotify.max_user_watches = 99999999
fs.inotify.max_user_instances = 65535
����˵����
max_queued_events��inotify������ȣ�ֵ̫С�����¼���ļ���ȷ
max_user_watches: Ҫͬ�����ļ������Ŀ¼; ��ֵҪ����find /testdir -type d | wc -l��ֵ
max_user_instances��ÿ���û�����inotifyʵ�����ֵ
5�������ű������inotify�ļ���¼���ʵʱ����rsyncͬ��
# vim rsync.sh
#!/bin/bash
srcdir=/testdir/
dstdir=testdir
excludedir=/usr/local/inotify/exclude.list
rsyncuser=root
rsyncpassdir=/etc/passwd.txt
dstip=10.1.1.2
while true
do
/usr/local/inotify/bin/inotifywait -rq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib -t 1 $srcdir
rsync -avH --delete $srcdir --timeout=100 $rsyncuser@$dstip::$dstdir --password-file=$rsyncpassdir >/dev/null 2>&1
done
exit 0
�ýű�ֻ������Ŀ�������ֻ��һ��������������ڶ�����ڽű���ʹ��forѭ��ͬ�������Ŀ�������
# chmod a+x /root/rsync.sh
��Դ������Ŀ¼�д����ļ����ֶ����иýű����鿴Ŀ����������Ƿ�ͬ�����´������ļ��������Բ鿴��������ʵʱͬ���ɹ�
ͨ�����ýű������Զ����� ����ͨ��ƻ�����ʱ���иýű������Զ�ʵ��ʵʱ
�����Զ�������
# vim /etc/rc.d/rc.local
sh /root/rsync.sh &
�ƻ�����
# crontab -e
*/10 * * * * sh /root/rsync.sh &
�ű���inotifywait����˵����
-m��ʼ�ռ���Ŀ¼״̬�仯
-r���ݹ����
-q����ʾ״̬�仯��Ϣ
-format�� ָ�������ʽ�����õĸ�ʽ���磺%w����ʾ�����¼���Ŀ¼ %f����ʾ�����¼����ļ� %e����ʾ������¼� %T��ʹ����-timefmt�����ʱ���ʽ
-timefmt��ָ��ʱ���ʽ������-formatѡ���е�%T��ʽ
-e close_write,modify,delete,create,attrib,move�����õļ���¼�
���ij��� ������ЦЦ���� ���ͣ�����ر����˳���http://hashlinux.blog.51cto.com/9647696/1760489