nighty

折腾的年华
posts - 37, comments - 143, trackbacks - 0, articles - 0

rsync同步文件的安装笔记

Posted on 2013-04-12 12:23 寒武纪 阅读(1323) 评论(0)  编辑  收藏 所属分类: 服务器部署
系统为ubuntu server,二台机器A和B,IP为A 192.168.1.111,B 192.168.1.222
A为rsync server,启动为守护进程,B为备份机,做为rsync client,最后用crontab做一个简单的作业,定时在B上执行同步文件的功能
A的安装和配置如下:
1.  apt-get install rsync   可能提示系统已经安装有了
2. 配置文件/etc/rsyncd.conf
    默认安装时是不会有这个配置文件的,但是可以 cp /usr/share/doc/rsync/examples/rsyncd.conf /etc  把它示例中的配置文件拷贝过来
    vi /etc/rsyncd.conf    这里参数有点多,但是有些可以先不管,关注重点的
    [ftp]  这里是模块,可以配置多个,这个是系统默认给出的一个配置,下面给一个本机上的配置示例:
--------------------------------------------------------------------------------------------------
# so omit the "pid file" line completely in that case.
pid file=/var/run/rsyncd.pid
#syslog facility=daemon
#socket options=
# MODULE OPTIONS
[share]
comment = public archive
path = /var/www/pub
use chroot = no
max connections=2
# lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = no
list = yes
uid = nobody
gid = nogroup
# exclude = 
# exclude from = 
# include =
# include from =
auth users = rsync
secrets file = /etc/rsyncd.secrets
strict modes = yes
hosts allow = 192.168.1.222
# hosts deny =
ignore errors = yes
ignore nonreadable = yes
transfer logging = yes
log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
---------------------------------------------------------------------------------------------
这里,最上面的是pid文件位置。然后配置了一个模块名叫做share,最大连接数是2,read only = no,指定为非只读(不然同步时会有权限问题)
而后面的auth users = rsync 是指定一个同步的账户名叫做rsync,这个账户的认证文件是/etc/rsyncd.secrets,当然我们要创建这个文件

3.  创建 /etc/rsyncd.secrets文件,内容为: rsync:123  表示rsync这个用户的密码是123 然后修改文件的权限 chmod 600 /etc/rsyncd.secrets

4.  rsync server做为守护进程
     vi /etc/default/rsync
     可以看到开头处这样声明:
------------------------------------
# start rsync in daemon mode from init.d script?
#  only allowed values are "true", "false", and "inetd"
#  Use "inetd" if you want to start the rsyncd from inetd,
#  all this does is prevent the init.d script from printing a message
#  about not starting rsyncd (you still need to modify inetd's config yourself).
RSYNC_ENABLE=inetd
-------------------------------------------
   做为守护进程,可以设置为true或是xinetd方式来启动。于是我们安装inetd   sudo apt-get install xinetd
   安装好后配置inetd的配置文件  vi /etc/xinetd.d/rsync ,输入如下内容:
---------------------------------------------------
service rsync
{
    disable = no
    socket_type = stream
    wait = no
    user = root
    server = /usr/bin/rsync
    server_args = --daemon
    log_on_failure += USERID
}
-------------------------------------------------------
然后启动xinetd,/etc/init.d/xinetd restart,A服务器的rsyncd server就完成了!

5.  B服务器由于是client,不需要配置,也不需要安装xinetd,直接可以通过命令行执行
rsync --delete -azvv rsync@192.168.1.111::share /var/www/pub/
这个命令就可以直接连接到192.168.111的rsync账户,它会提示你输入密码,就是A中的secrets文件中的密码,然后同步share模块到本机的/var/www/pub目录,你可以事前在A机器上创建一个文件如test.txt,随便写点内容,然后执行些命令,看是不是B上多了这样一个文件?如果是,则表示已经连接成功。你接下来就可以做crontab了!


刚进场的时候戏就落幕

只有注册用户登录后才能发表评论。


网站导航: