Tag Archives: rsync

rsync配置以及一些小提示

最近搞镜像同步, 用到了rsync. 这里做一些简单的备忘记录.

Rsyncd

1 安装
在服务器上安装rsync,
在debian, ubuntu 服务器上可以使用apt-get install rsync
在centos, redhat 服务器上可以使用yum install rsync
或者直接下载rsync源代码: http://www.samba.org/ftp/rsync/rsync-3.0.9.tar.gz (目前最新版本3.0.9)
然后./configure and make and make install

2 配置rsyncd.conf

uid = nobody
gid = nobody
use chroot = no
max connections = 25
read only = yes
port = 873
[MODNAME]
path = /home/htdocs/res
comment = Website static resource (~30G)
list = yes
auth users = wolftankk
ignore errors
secrets file = /etc/rsyncd/MODNAME.scr
exclude = tmp/

然后设置MODNAME.scr
[注意] 一定要将MODNAME.scr 设置为600属性

wolftankk:mypass

更具体的配置 可以参考http://www.samba.org/ftp/rsync/rsyncd.conf.html

3 启动

/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf

将此命令加入启动脚本中, 服务器将会在每次启动时候自动启动rsyncd

Rsync

在镜像服务器上设置同步

测试链接

rsync -avzP wolftankk@172.16.1.30::
如果链接成功, 将会列出此服务器上的 模块以及注释, 如下

[MODNAME]
comment = Website static resource (~30G)

[备注]
-a参数: 相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件

在使用-a参数的时候, 会将服务器A的owner以及group同步到本地, 有可能会导致你镜像文件的读写权限导致问题.
我们可以使用–no-OPTIONS 参数,比如我不需要同步文件(夹)的owner和group, 那么加入参数 –no-o 和 –no-g

屏蔽列表, 屏蔽方式用两种一种是直接写在配置里面, 另一种是写在一个但是的配置文件中.
1. exclude
不包含文件夹 –exclude=tmp/
不包含文件 –exclude=include/database.php

2. exclude-from
vim /etc/rsync.ignore

tmp/
include/database.php
download/test/*
static/css/*.sass

设置cron

* */1 * * * /usr/bin/rsync -az --password-file=/etc/rsync.pass --exclude-from=/etc/rsync.ignore --no-o --no-g wolftankk@172.16.1.30::MODNAME /home/htdocs/res

每小时同步一次.

在rsync.pass种设定密码, 并且必须要设置为600权限

mypass

详细参数可以参考: http://www.samba.org/ftp/rsync/rsync.html