rsync+inotify的实时文件同步
一:inotify介绍
inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件
rsync介绍:
rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。
rsync 包括如下的一些特性:
能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口;
支持匿名rsync 同步文件,是理想的镜像工具
主机IP:zpf 192.168.1.225 rsync-slave (这个是文件备份的服务器端)
zpf2 192.168.1.226 rsync-master(这个是文件需要同步的客户端)
首先在zpf主机上安装rsync服务:
yum -y install rsync*
配置文件
rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)
服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,需要手动创建
touch /etc/rsyncd.conf #创建rsyncd.conf,这是rsync服务器的配置文件。
#touch /etc/rsync.pass #创建rsync.pass ,这是用户密码文件。
#chmod 600 /etc/rsyncd/rsync.pass #将rsync.pass这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!
#touch /etc/rsyncd.motd ###这个可有可无,就是显示欢迎信息
之后添加rsync用户:
useradd rsync -s /sbin/nologin -M
创建rsync 服务端的工作模块目录
mkdir /backup
chown rsync:rsync /backup
创建密码文件,这个需要是系统的用户:
echo "rsync:123456" >/etc/rsync.pass ###这个文件的权限是需要是600
chmod 600 /etc/rsync.pass
之后修改下rsync的配置文件,写入如下内容:
port=873
uid=root
gid=root
usechroot=no
max connections =100
#syslog facility = local5
read only = false
pid file =/var/run/rsyncd.pid
log file =/var/log/rsyncd.log
lock file=/var/run/rsync.lock
[backup]
path=/backup
comment= backup files
read only=no
write only=no
list=false
auth users=rsync
secrets file =/etc/rsync.passwd
hosts allow =*
hosts deny =
配置完成后,在服务端启动rsync进程:
此处是yum安装的。所以rsync直接是在系统中的一个命令,若是编译安装,则安装实际路径执行:
rsync --daemon --config=/etc/rsyncd.conf ###以守护进程的方式在后台运行,配置文件是/etc/rsyncd.conf
之后配置客户端的rsync。客户端直接安装即可,但是由于服务端中配置了用户密码验证,所以在此也需要输入密码,为了方便,我们可将密码直接写入文件中,直接使用。
echo "123456" > /etc/rsync.pass ##这个文件也需要将权限改成600
chmod 600 /etc/rsync.pass
最后,我们可以进行同步:
1,这个是将zpf本地主机的内容备份至远程zpf主机, --delete删除dst中src中没有的 --progress显示备份过程
rsync -av /home/test --delete --progress --exclude "*access*" --exclude "debug*" rsync@zpf::backup --password-file=/etc/rsync.pass
源目标 用户,登录的主机,备份的路径 密码文件(本地主机)
2,这个是将远程zpf主机的内容全部备份至本地主机
rsync -av --delete --progress --exclude "*access*" --exclude "debug*" rsync@zpf::backup /home/test/ --password-file=/etc/rsync.pass
用户,登录的主机,需备份的文件 备份到本地 密码文件(本地主机上的)
上面的两个都是备份和源文件全部一样的,有不同的数据会被删除
3,如下则是将本地主机的内容增量备份至远程zpf主机
rsync -av /home/test rsync@zpf::backup --password-file=/etc/rsync.pass
4,如下是将远程主机zpf的内容增量备份之本地主机
rsync -av rsync@zpf::backup /home/test --password-file=/etc/rsync.pass
inotify的部署配置:
查看当前系统是否支持inotify
ll /proc/sys/fs/inotify/
如果支持就会有如下输出:
[root@zpf backup]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_queued_events
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_user_instances
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_user_watches
[root@zpf backup]#
如果显示上述的三个文件,则内核支持inotify
拓展:
/proc/sys/fs/inotify/max_queued_evnets
表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances
表示每一个real user ID可创建的inotify instatnces的数量上限。
/proc/sys/fs/inotify/max_user_watches
表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
下载源码包:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
解压缩,直接编译安装即可:
./configure --prefix=/usr/local/inotify
make && make install
安装完成。安装在/usr/local/inotify
使用帮助查看inotify的帮助信息:
/usr/local/intofy/inotifywait --help
-r|--recursive Watch directories recursively. #递归查询目录
-q|--quiet Print less (only print events). #打印监控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始终保持事件监听状态
--excludei Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。
--timefmt strftime-compatible format string for use with %T in --format string. #指定时间输出的格式
--format Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event [ -e|--event ... ] Listen for specific event(s). If omitted, all events are listened for. #通过此参数可以指定需要监控的事件,如下所示:
Events:
access file or directory contents were read #文件或目录被读取。
modify file or directory contents were written #文件或目录内容被修改。
attrib file or directory attributes changed #文件或目录属性被改变。
close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式。
open file or directory opened #文件或目录被打开。
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。
move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。
create file or directory created within watched directory #文件或目录被创建在当前目录
delete file or directory deleted within watched directory #文件或目录被删除
unmount file system containing file or directory unmounted #文件系统被卸载
那么rsync+inotify结合的脚本:inotify_rsync.sh
#!/bin/bash
#para
#inotify-slave的ip地址
host=192.168.1.225
#本地监控的目录
src=/home/test/
#inotify-slave的rsync服务的模块名
#dst=backup
#rsync备份文件的目录
det=backup
#inotify-slave的rsync服务的虚拟用户
user=rsync
#本地调用rsync服务的密码文件
rsync_passfile=/etc/rsync.pass
#inotify的安装目录
inotify_home=/usr/local/inotify
#judge
if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
# rsync -av --delete $src $user@$zpf2::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
rsync -av $src --delete $user@$host::$det --password-file=${rsync_passfile} >/dev/null 2>&1
done
上面的脚本功能是将/home/test/下的内容备份到backup模块中,可以修改,在此rsync的守护进程运行在一台主机,inotify以及rsync备份运行于另一台主机
一:inotify介绍
inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件
rsync介绍:
rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。
rsync 包括如下的一些特性:
能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口;
支持匿名rsync 同步文件,是理想的镜像工具
主机IP:zpf 192.168.1.225 rsync-slave (这个是文件备份的服务器端)
zpf2 192.168.1.226 rsync-master(这个是文件需要同步的客户端)
首先在zpf主机上安装rsync服务:
yum -y install rsync*
配置文件
rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)
服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,需要手动创建
touch /etc/rsyncd.conf #创建rsyncd.conf,这是rsync服务器的配置文件。
#touch /etc/rsync.pass #创建rsync.pass ,这是用户密码文件。
#chmod 600 /etc/rsyncd/rsync.pass #将rsync.pass这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!
#touch /etc/rsyncd.motd ###这个可有可无,就是显示欢迎信息
之后添加rsync用户:
useradd rsync -s /sbin/nologin -M
创建rsync 服务端的工作模块目录
mkdir /backup
chown rsync:rsync /backup
创建密码文件,这个需要是系统的用户:
echo "rsync:123456" >/etc/rsync.pass ###这个文件的权限是需要是600
chmod 600 /etc/rsync.pass
之后修改下rsync的配置文件,写入如下内容:
port=873
uid=root
gid=root
usechroot=no
max connections =100
#syslog facility = local5
read only = false
pid file =/var/run/rsyncd.pid
log file =/var/log/rsyncd.log
lock file=/var/run/rsync.lock
[backup]
path=/backup
comment= backup files
read only=no
write only=no
list=false
auth users=rsync
secrets file =/etc/rsync.passwd
hosts allow =*
hosts deny =
配置完成后,在服务端启动rsync进程:
此处是yum安装的。所以rsync直接是在系统中的一个命令,若是编译安装,则安装实际路径执行:
rsync --daemon --config=/etc/rsyncd.conf ###以守护进程的方式在后台运行,配置文件是/etc/rsyncd.conf
之后配置客户端的rsync。客户端直接安装即可,但是由于服务端中配置了用户密码验证,所以在此也需要输入密码,为了方便,我们可将密码直接写入文件中,直接使用。
echo "123456" > /etc/rsync.pass ##这个文件也需要将权限改成600
chmod 600 /etc/rsync.pass
最后,我们可以进行同步:
1,这个是将zpf本地主机的内容备份至远程zpf主机, --delete删除dst中src中没有的 --progress显示备份过程
rsync -av /home/test --delete --progress --exclude "*access*" --exclude "debug*" rsync@zpf::backup --password-file=/etc/rsync.pass
源目标 用户,登录的主机,备份的路径 密码文件(本地主机)
2,这个是将远程zpf主机的内容全部备份至本地主机
rsync -av --delete --progress --exclude "*access*" --exclude "debug*" rsync@zpf::backup /home/test/ --password-file=/etc/rsync.pass
用户,登录的主机,需备份的文件 备份到本地 密码文件(本地主机上的)
上面的两个都是备份和源文件全部一样的,有不同的数据会被删除
3,如下则是将本地主机的内容增量备份至远程zpf主机
rsync -av /home/test rsync@zpf::backup --password-file=/etc/rsync.pass
4,如下是将远程主机zpf的内容增量备份之本地主机
rsync -av rsync@zpf::backup /home/test --password-file=/etc/rsync.pass
inotify的部署配置:
查看当前系统是否支持inotify
ll /proc/sys/fs/inotify/
如果支持就会有如下输出:
[root@zpf backup]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_queued_events
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_user_instances
-rw-r--r-- 1 root root 0 Aug 18 13:57 max_user_watches
[root@zpf backup]#
如果显示上述的三个文件,则内核支持inotify
拓展:
/proc/sys/fs/inotify/max_queued_evnets
表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
/proc/sys/fs/inotify/max_user_instances
表示每一个real user ID可创建的inotify instatnces的数量上限。
/proc/sys/fs/inotify/max_user_watches
表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
下载源码包:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
解压缩,直接编译安装即可:
./configure --prefix=/usr/local/inotify
make && make install
安装完成。安装在/usr/local/inotify
使用帮助查看inotify的帮助信息:
/usr/local/intofy/inotifywait --help
-r|--recursive Watch directories recursively. #递归查询目录
-q|--quiet Print less (only print events). #打印监控事件的信息
-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始终保持事件监听状态
--excludei Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。
--timefmt strftime-compatible format string for use with %T in --format string. #指定时间输出的格式
--format Print using a specified printf-like format string; read the man page for more details.
#打印使用指定的输出类似格式字符串
-e|--event [ -e|--event ... ] Listen for specific event(s). If omitted, all events are listened for. #通过此参数可以指定需要监控的事件,如下所示:
Events:
access file or directory contents were read #文件或目录被读取。
modify file or directory contents were written #文件或目录内容被修改。
attrib file or directory attributes changed #文件或目录属性被改变。
close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式。
open file or directory opened #文件或目录被打开。
moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。
move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。
create file or directory created within watched directory #文件或目录被创建在当前目录
delete file or directory deleted within watched directory #文件或目录被删除
unmount file system containing file or directory unmounted #文件系统被卸载
那么rsync+inotify结合的脚本:inotify_rsync.sh
#!/bin/bash
#para
#inotify-slave的ip地址
host=192.168.1.225
#本地监控的目录
src=/home/test/
#inotify-slave的rsync服务的模块名
#dst=backup
#rsync备份文件的目录
det=backup
#inotify-slave的rsync服务的虚拟用户
user=rsync
#本地调用rsync服务的密码文件
rsync_passfile=/etc/rsync.pass
#inotify的安装目录
inotify_home=/usr/local/inotify
#judge
if [ ! -e "$src" ] || [ ! -e "${rsync_passfile}" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
# rsync -av --delete $src $user@$zpf2::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
rsync -av $src --delete $user@$host::$det --password-file=${rsync_passfile} >/dev/null 2>&1
done
上面的脚本功能是将/home/test/下的内容备份到backup模块中,可以修改,在此rsync的守护进程运行在一台主机,inotify以及rsync备份运行于另一台主机
编辑回复