环境描述:现有两台服务器,要实现mp3文件的实时同步。可以使用rsync来做同步,但不能实现实时,为了实现实时同步,我们使用inotify来监控需要同步的目录发生的改变,inotify只是内核中的一个监控文件变化的模块,提供了监控文件变化的API,而要连接这个API,需要安装inotify-tools工具。可以实现文件的新增,删除,修改,改变属性等,功能很强大。现在需要一台服务器做为内容发布端,来把改变的文件,通过rsync实时的同步到内容服务器。
==========================================
- # Section 1: Global settings
- port = 873
- uid = root
- gid = root
- use chroot = yes
- read only = no
- max connections = 7
- pid file = /var/run/rsyncd.pid
- log file = /var/log/rsyncd.log
- hosts allow = *
- transfer logging = yes
- log format = %t %a %m %f %b
- syslog facility = local3
- timeout = 300
- # Section 2: Directory to be synced
- [mp3]
- path = /data/mp3
- list = false
- ignore errors = yes
- auth users = syncuser
- secrets file = /etc/rsyncd/rsyncd.pass
- #!/bin/bash
- #
- DESTHOST=1.1.1.1
- DESTHOSTDIR=/data/mp3/
- SRCDIR=/data/mp3/
- inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $SRCDIR | while read DATE TIME DIR FILE; do
- FILECHANGE=${DIR}${FILE}
- rsync -avz --password-file=/etc/rsyncd/rsyncd.pass $FILECHANGE syncuser@${DESTHOST}::mp3 &>/dev/null && \
- echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> /var/log/mp3sync.log
- done