温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-10 08:36:22 来源:网络 阅读:1319 作者:liwenhn 栏目:建站服务器

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

系统环境: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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI