资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

CentOS6.x7.x中用rsync远程同步文件-创新互联

CentOS 6.x中用rsync远程同步文件

我们提供的服务有:成都网站建设、网站设计、微信公众号开发、网站优化、网站认证、海曙ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的海曙网站制作公司

系统环境:Centos 6.9 x64  (Centos 7.3 x64)

目的:

服务器110.112.200.12中/u01文件夹需要同步复制到110.210.250.58里面进行备份。

将200.12做xinetd 服务器,将其/u01 文件夹复制同步到250.58里面去,250.58做客户端。

一、服务器端的配置

Centos 6.9 x64

在源服务器110.112.200.12中配置

[root@mail test]#  yum -y install xinetd rsync

再修改配置: # 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

}

把原来的disable ,由YES改成NO

[root@mail test]#  vi /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

max connections = 20

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

[backup]

path = /u01

comment = Rsync share test

auth users = ruser

read only = yes

hosts allow =110.210.250.58

hosts deny = *

注意,path = /u01 ;表示要备份的文件夹为/u01

配置同步的帐号、密码

[root@mail test]# vi /etc/rsync_pass

ruser:123456

[root@mail test]# chown root:root /etc/rsync_pass

[root@mail test]# chmod 600 /etc/rsync_pass

[root@mail test]# chkconfig xinetd on

[root@mail test]# service xinetd restart

检查是否出现873端口

[root@mail test]# netstat -natp

tcp  0   0 0.0.0.0:873    0.0.0.0:*   LISTEN    20959/xinetd

注意:如果服务器上装有防火墙记得要打开端口,默认端口是873

另外一种启动、停止方法(不建议使用)

有些人喜欢用命令rsync --daemon --config=/etc/rsyncd.conf来启动rsync,停用它时要用pkill命令来杀掉进程,不如用xinetd来管理启动rsync方便。

再有些人喜欢写个脚本来启动、停止rsync,系统本身有xinetd来管理启动rsync而不用它,有点舍近求远的味道。

[root@mail test]# rsync --daemon --config=/etc/rsyncd.conf

[root@mail test]# pgrep -l rsync

5132 rsync

[root@mail test]# pkill rsync

[root@mail test]# pgrep -l rsync

------------------------------------------------------------------------------------------

如果你是用Centos 7.3 x64做服务端,请看下面的配置

[root@mail test]# rpm -qa|grep rsync

rsync-3.0.9-17.el7.x86_64

4、配置rsync的配置文件

vi /etc/rsyncd.conf

uid = root

gid = root

use chroot = no

port = 873

max connections = 20

timeout = 200

secrets file = /etc/rsync_pass

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log

lock file = /var/run/rsyncd.lock

log format = %t %a %m %f %b

[backup]

path = /u01

comment = Rsync share test

list = yes

auth users = ruser

read only = yes

hosts allow = 110.210.250.58

hosts deny = *

[root@mail test]# yum install xinetd.x86_64

[root@mail test]# rpm -qa xinetd

xinetd-2.3.15-13.el7.x86_64

再修改配置: # 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

}

把原来的disable ,由YES改成NO

安装完成后,将xinetd服务加入开机自启动:

[root@mail test]#  systemctl enable xinetd.service

最后,重新启动服务即可:

[root@mail test]# systemctl restart xinetd

检查是否出现873端口

[root@mail test]# netstat -natp

tcp6   0    0 :::873   :::*    LISTEN    25167/xinetd

加入防火墙允许

[root@mail test]# firewall-cmd --add-service=rsyncd --permanent

[root@mail test]# firewall-cmd --reload

[root@mail test]# systemctl restart firewalld

-------------------------------------------------------------------------------------------

二、客户端的配置

在目标服务器250.58中配置

[root@vmevan test]#  yum -y install rsync

[root@vmevan test]#  vi /etc/rsync_pass

123456

注意,客户端的密码文件只需要密码,而不需要用户名!

[root@vmevan test]# chmod 600 /etc/rsync_pass

建立一个备份用的文件夹

[root@vmevan test]# mkdir /bakcup200.12

在客户端中,有些文件(如named.run)不需要同步,所以要添加到排除列表exclude.list中去。

[root@vmevan test]# vi /etc/exclude.list

named.run

[root@vmevan test]# chmod 600 /etc/exclude.list

[root@vmevan test]# vi /usr/sbin/rsyncdns

#!/bin/bash

# by evan.li  2017.6.21

rsync -vzrtopgu --progress --delete --exclude-from="/etc/exclude.list" --password-file=/etc/rsync_pass  ruser@110.112.200.12::backup /bakcup200.12

[root@vmevan test]# chmod +x /usr/sbin/rsyncdns

每1小时同步

[root@vmevan test]# vi /etc/crontab

0 */1 * * * root /usr/sbin/rsyncdns

1、在客户端中,设定异地主机之间的同步

[root@vmevan test]#  rsync -vzrtopgu --progress --delete --password-file=/etc/rsync_pass  ruser@110.112.200.12::backup /bakcup200.12

或者用

[root@vmevan test]# rsync -zrtopgu --delete --password-file=/etc/rsync_pass  ruser@110.112.200.12::backup  /bakcup200.12

这个命令行中-vzrtopg里的v是verbose,详细的。

z是压缩传输,

r是recursive,递归。

topg都是保持文件原有属性如属主、时间的参数。

u是只同步已经更新的文件,避免没有更新的文件被重复更新一次,不过要注意两者机器的时钟的同步。

–progress是指显示出详细的进度情况,

–delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。

后 面的ruser@110.112.200.12::backup中,之后的backup是模块名, 也就是在/etc/rsyncd.conf中自定义的名称, ruser是指定模块中指定的可以同步的用户名。

最后的/bakcup200.12是备份到本地的目录名。

在这里面,还可以用-e ssh的参数建立起加密的连接。

可以用–password-file=/password/path/file来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

在客户端中,将rsync放入crontab计划任务,每天早上5点同步一次

[root@vmevan test]# vi /etc/crontab

0 5 * * * root /usr/bin/rsync -vzrtopgu --delete --password-file=/etc/rsync_pass  ruser@114.112.200.12::backup  /bakcup200.12

50 6 * * * root /usr/bin/rsync -avu –progress –delete /u01  /u03/backup

2、在服务端(源服务器中)中, 能建立本地目录之间的同步

[root@vmevan test]#  rsync -avu -progress -delete  /u01/555  /mnt/sdb1/u01

将源目录/u01/555,同步放到/mnt/sdb1/u01文件夹下。对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减 。

测试完成于2017.06.21

by evan.li

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:CentOS6.x7.x中用rsync远程同步文件-创新互联
标题URL:http://cdkjz.cn/article/dicphs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220