由于rsync通过增量的方式进行文件同步,效率非常高,所以很多人喜欢使用rsync进行文件同步或者备份,这里记录一下rsync的搭建方式。
Shell # yum install xinetdrsync
1. 打开主机防火墙的873端口。
-A INPUT -m state --state NEW -s 10.0.0.0/8 -m tcp -p tcp --dport 873 -jACCEPT
一般rsync是内网服务,所以尽可能不要开发给外网访问,所以打开防火墙的时候最好限制IP来源。
2. Rsync配置文件/etc/rsyncd.conf 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| uid = root
gid = root
use chroot = no
max connections = 100
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[testNode1] #自定义的一个名称,后面文件传送的时候会用到,这个文件里面可以有多个这种段,用来指定不同的同步目录。
path=/backup/slave_db_backup # 文件所在的路径
ignore errors
read only = no # 是否只读
list = no
hosts allow = 10.0.0.0/255.0.0.0 # 只允许指定IP的客户端访问这个目录
auth users = backup #认证用户名称,可自定义
secretsfile = /etc/rsyncd.password #认证密码文件,可自定义
|
3. Rsync密码文件配置 /etc/rsyncd.password (文件名称和上面的配置对应)
用户名称和上面配置的一致。 /etc/rsyncd.password文件所有者是root,文件的权限必须是600,否则会报错。 1
| -rw-r--r-- 1 root root 1583 Jun 29 10:47 /etc/rsyncd.conf
|
4. Xinetd 配置文件/etc/xinetd.d/rsync。
disable修改为no。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
|
5. 设置 xinetd 开机启动。
1
2
| Shell # /etc/init.d/xinetd restart
Shell # chkconfig xinetd on
|
1. 建立客户端的密码文件。
文件名称可以随便其定义,我这里也使用rsyncd.password作为文件名称,内容就是前面配置的用户密码,请注意,没有用户名称。 密码文件的所有者必须是你要在客户端运行rsync命令的用户,而且密码文件的权限必须是600。
2. 命令简单说明。
Shell # /usr/bin/rsync-aSvH --password-file=./rsyncd.password ./* backup@10.1.3.10:: testNode1
注意,ip地址后面的是两个冒号,冒号后面的名称和在/etc/rsyncd.conf 文件内配置的一致。
#对同步速度进行限制
rsync -avzPSH --bwlimit=1000 --password-file=./rsyncd.password backup@10.1.3.9::testNode1 /data/dir/
#限制只同步指定的文件,其他文件全部不同步
rsync -avSH --password-file=$WorkDir/rsync.passwd --include="xxx.sql.tar.gz" --exclude="/*" backup@10.1.3.2::database /home/dir
--include 要放在 --exclude 前面
Rsync命令的各个选项含义,请查看man手册。
|