[原创]Rsync搭建和使用

rsync服务的搭建和使用

 

***下载安装:

#wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz

#tar -zxvf rsync-3.1.2.tar.gz

#cd rsync-3.1.2

#./configure --prefix=/usr/local/rsync

#make

#make install

 

第一步:修改rsyncd的配置文件,增加测试目录test
#vim /etc/rsyncd.conf
uid = www-data
gid = www-data
use chroot = yes  #启用chroot,处理软链接源文件不被同步的/rsyncd-munged问题
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log


[test_code]
path = /data/www/html
comment = web resources
read only = no
strict modes = yes
refuse options = delete
auth users = test
secrets file = /etc/rsyncd_auto_rsync_users
hosts allow = xxx.xxx.xxx.xxx
post-xfer exec = /data/sa/reload_nginx.sh  //指定同步完成之后触发执行的脚本


[test]
path = /data/test
max connections = 5  //最大连接数
comment = test
read only = no
strict modes = yes
refuse options = delete  //拒绝删除操作
auth users = test
secrets file = /etc/rsyncd_auto_rsync_users
hosts allow = xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx
transfer logging = yes
log format = %t %o %a %m %u %f %l %b
log file = /var/log/rsyncd_test.log
post-xfer exec = /data/sa/reload_nginx.sh

 

第二步:修改rsync密码文件,添加pic_test模块的用户和密码
root@localhost:/data/sa# cat /etc/rsyncd_auto_rsync_users
test:xxxxxxxxxxxxxx
test1:xxxxx

 

第三步:
重启rsyncd服务
/usr/bin/rsync --no-detach --daemon --config /etc/rsyncd.conf --port=873 &


命令同步示例:

从rsync服务器同步test_code模块文件到本地,除/home/huangjie1/pass_id.list目录以外的文件:
rsync -avuzt --exclude-from '/home/huangjie1/pass_id.list' test@rsync_ip::test_code/* /data/www/html/

 

同步文件时备份参数的使用示例:

1)同步文件,直接加参数-b,会在目标主机的模块目录下备份文件:1.txt~
# rsync -atzvb 1.txt user@xxx.xxx.xxx.xxx::xxx/
2)同步文件,加上--suffix参数指定时间后缀,会在目标主机的模块目录下备份文件:1.txt20180729
# rsync -atzvb --suffix=`date +%Y%m%d` 1.txt user@xxx.xxx.xxx.xxx::xxx/
3)同步目录,只会在目标主机的模块目录下备份修改过的文件
# rsync -atzvb --suffix=`date +%Y%m%d` test123/ user@xxx.xxx.xxx.xxx::xxx/test123
4)同步目录,加上--backup-dir参数,会在目标主机的模块目录下test123目录下创建test123_bak备份目录,只备份修改过的文件,即同步的文件
# rsync -atzvb --suffix=`date +%Y%m%d` --backup-dir=test123_bak test123/ user@xxx.xxx.xxx.xxx::xxx/test123
5)同步目录,加上--backup-dir参数,会在目标主机的模块目录下创建test123_bak备份test123目录,只备份修改过的文件,即同步的文件(注意与上一个的不同,多了一个“/”)
# rsync -atzvb --suffix=`date +%Y%m%d` --backup-dir=/test123_bak test123/ user@xxx.xxx.xxx.xxx::xxx/test123
6)修改sersync的xml配置文件commonParams参数为: <commonParams params="-artuzb --backup-dir=/backup/bak_`date +%Y%m%d%H%M%S` --suffix=`date +%Y%m%d%H%M%S`"/>,原来只有-artuz,会在目标主机的模块目录下创建backuup目录备份test123目录,只备份修改过的文件。(注意:--backup-dir指定的目录在双向同步时要用动态目录,即每次目录不一样,否则会有问题,导致第一次备份成功,以后的备份都会在同一个目录下重复创建一样的目录;单向同步是没有这个问题)

 

扩展:一般和sersync,lrsync,inotify-tools等工具实现自动发现实时同步文件或者双向同步,各有优缺点,可自行查阅资料了解。

 

lsync配置实例:

settings {
logfile = "/var/log/lsyncd_to_sites_bak.log",
nodaemon = true,
statusInterval = 10,
inotifyMode = "CloseWrite or Modify",
maxProcesses = 10
}

sync {
default.rsync,
source = "/usr/local/nginx/conf/sites/",
target = "sites@ip1::sites",
-- or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
excludeFrom = "/etc/lsyncd/exclude.conf",
delete = true,
rsync = {
archive = true,
compress = true,
_extra = {"--password-file=/etc/lsyncd/rsyncd_to_sites.passwd","--port=873","--numeric-ids", "--bwlimit=10000"},
}

}
sync {
default.rsync,
source = "/usr/local/nginx/conf/sites/",
target = "sites@ip2::sites",
-- or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
excludeFrom = "/etc/lsyncd/exclude.conf",
delete = true,
rsync = {
archive = true,
compress = true,
_extra = {"--password-file=/etc/lsyncd/rsyncd_to_sites.passwd","--port=873","--numeric-ids", "--bwlimit=10000"},
}

}

 

启动:

lsyncd --nodaemon /etc/lsyncd/lsyncd_to_sites.conf &

 

posted @ 2018-05-03 15:21  wsjhk  阅读(496)  评论(0编辑  收藏  举报