CentOS   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CentOS 6.x 7.x中用rsync远程同步文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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 x6

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

系统环境:Centos 6.9x64 (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

@H_945_87@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

port = 873

timeout = 200

lock file = /var/run/rsyncd.lock

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

list = 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]# 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]# 123456

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


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


建立一个备份用的文件

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


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

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

named.run


@H_801_638@[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是只同步已经更新的文件,避免没有更新的文件被重复更新一次,不过要注意两者机器的时钟的同步。

�Cprogress是指显示出详细的进度情况,

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

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

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

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

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


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

[root@vmevan test]# 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 �Cprogress �Cdelete /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

大佬总结

以上是大佬教程为你收集整理的CentOS 6.x 7.x中用rsync远程同步文件全部内容,希望文章能够帮你解决CentOS 6.x 7.x中用rsync远程同步文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。