rsync+inotify 数据实时同步备份

一、rsync

rsync – a fast, versatile, remote (and local) file-copying tool.

rsync synopsis:

rsync 支持三种传输模式:

rsync连接远程主机进行同步或备份时有两种途径:使用远程shell程序(如ssh或rsh)进行连接,,或使用TCP直接连接rsync daemon。 当源路径或目的路径的主机名后面包含一个冒号分隔符时,rsync使用远程shell传输;当源路径或目的路径的主机名后面包含两个冒号,或使用rsync://URL时,rsync使用TCP直接连接rsync daemon。 特别的,如果只指定了源路径,而没有指定目的路径,rsync将会显示源路径中的文件列表,类似于使用命令ls -l。rsync把本地端看作client,把远程端当成server。注意:不要把server与rsync daemon混淆!daemon一定是server,而server却不一定是daemon,也可能是远程shell的衍生进程。

Local:rsync[OPTION…]SRC…[DEST]Accessviaremoteshell:Pull:rsync[OPTION…][USER@]HOST:SRC…[DEST]Push:rsync[OPTION…]SRC…[USER@]HOST:DESTAccessviarsyncdaemon:Pull:rsync[OPTION…][USER@]HOST::SRC…[DEST]rsync[OPTION…]rsync://[USER@]HOST[:PORT]/SRC…[DEST]Push:rsync[OPTION…]SRC…[USER@]HOST::DESTrsync[OPTION…]SRC…rsync://[USER@]HOST[:PORT]/DESTUsageswithjustoneSRCargandnoDESTargwilllistthesourcefilesinsteadofcopying.rsync -av /src/foo /dest# create foo directory in /dest/rsync -av /src/foo/ /dest/foo # cp /src/foo/* /dest/foo

Some modules on the remote daemon may require authentication. If so, you will receive a password prompt when you connect. You can avoid the password prompt by setting the environment variable RSYNC_PASSWORD to the password you want to use or using the–password-fileoption. This may be useful when scripting rsync.

WARNING: On some systems environment variables are visible to all users. On those systems using–password-fileis recommended.

rsync 的安装

forCentOS:#yum-yinstallrsyncforubuntu/debian#sudoapt-getinstallrsync

有两种方式。其一是通过xinetd进程来管理;其二作为一个独立的进程启动。

经由 xinetd 控管:

第一步:安装xinetd#yum-yinstallxinetd第二步:编辑配置文件,启动rsync#vim/etc/xinetd.d/rsyncdisable=yes#将disable=yes改为no修改为:disable=no第三步:重新启动xinetd进程#servicexinetdrestart查看状态:[root@skype~]#ss-tulpn|grep873tcpLISTEN064:::873:::*users:((“xinetd”,1446,5))

作为独立进程启动:

运行的时候,加上–daemon选项#/usr/bin/rsync–daemon–config=/etc/rsync.d/rsyncd.conf

下面我们将涉及到三个文件 rsyncd.conf,rsyncd.secrets 和rsyncd.motd。

rsyncd.conf 是rsync服务器主要配置文件。rsyncd.secrets是登录rsync服务器的密码文件。rsyncd.motd是定义rysnc 服务器信息的,也就是用户登录时,所显示的信息。

我们需要手动建立这三个文件:

[root@skype~]#mkdir/etc/rsync.d[root@skype~]#touch/etc/rsync.d/rsyncd.{conf,secrets,motd}[root@skype~]#tree/etc/rsync.d/etc/rsync.d|–rsyncd.conf|–rsyncd.motd`–rsyncd.secrets为了安全性,我们必须把/etc/rsync.d/secrets权限设置为600[root@skype~]#chmod600/etc/rsync.d/rsyncd.secrets[root@skype~]#ls-l/etc/rsync.d/rsyncd.secrets-rw——-.1rootroot0Apr1915:54/etc/rsync.d/rsyncd.secrets

rsyncd.secrets 格式:

user1:123456# 用户名:密码, 一行表示一个用户,此用户和系统用户无关

这个密码文件的文件属性设为root拥有, 且权限要设为600, 否则无法备份成功!

rsyncd.conf 格式

#GlobalSettings全局设定uid=nobody:以哪个用户的身份运行或获取数据的gid=nobody:用户都以来宾帐号的方式运行usechroot=no:在服务运行时要不要把他锁定在家目录maxconnections=10:做为服务器端最大并发连接数strictmodes=yes:表示是否工作在严格模式下,严格检查文件权限等相关信息pidfile=/var/run/rsyncd.pid:定义pid文件路径logfile=/var/log/rsyncd.log:定义日志文件存放路径的#Directorytobesynced定义共享目录的模块[my_data_rsync]要同步的目录名称,多个目录名称是不能重名的path=/myrsync/data:定义目录的路径ignoreerrors=yes:表示如果中间复制过程有一个文件出错了是要继续复制还是中止复制,yes表示继续复制,no表示中止复制readonly=no:如果打算让别人仅仅是来拉取数据的,yes就可以了,如果打算让别人推送过来做备份的那就为no,表示客户端是否可以推送的writeonly=no:只允别人在里面写数据,但不可以拉取数据hostsallow=172.16.251.244:做白名单的,是否允许哪些主机可以访问的hostsdeny=*:黑名单的说明:1、二者都不出现时,默认为允许访问2、只出现hostsallow,定义白名单,但没有被匹配到的主机由默认规则处理,即为允许3、只出现hostsdeny,定义黑名单,出现在名单中的都被拒绝4、二者同时出现,先检查hostsallow,如果匹配就allow,否则检查hostsdeny,如果匹配则拒绝,如是二者都不匹配,则由默认规则处理,即为允许list=false:是否允许用户列出文件列表的uid=root:以哪个用户身份去获取文件的gid=rootauthusers=myuser:做用户验证的,只允许哪个用户来复制secretsfile=/etc/.rsync.passwd:存放验证用户的密码的心有多大,舞台就有多大

rsync+inotify 数据实时同步备份

相关文章:

你感兴趣的文章:

标签云: