CentOS7利用rsync搭建内网yum源镜像服务器

rsync同步基本说明

rsync的目的是实现本地主机和远程主机上的文件同步(包括本地推到远程,远程拉到本地两种同步方式),也可以实现本地不同路径下文件的同步,但不能实现远程路径1到远程路径2之间的同步(scp可以实现)。

不考虑rsync的实现细节,就文件同步而言,涉及了源文件和目标文件的概念,还涉及了以哪边文件为同步基准。例如,想让目标主机上的文件和本地文件保持同步,则是以本地文件为同步基准,将本地文件作为源文件推送到目标主机上。反之,如果想让本地主机上的文件和目标主机上的文件保持同步,则目标主机上的文件为同步基准,实现方式是将目标主机上的文件作为源文件拉取到本地。当然,要保持本地的两个文件相互同步,rsync也一样能实现,这就像Linux中cp命令一样,以本地某文件作为源,另一文件作为目标文件,但请注意,虽然rsync和cp能达到相同的目的,但它们的实现方式是不一样的。

既然是文件同步,在同步过程中必然会涉及到源和目标两文件之间版本控制的问题,例如是否要删除源主机上没有但目标上多出来的文件,目标文件比源文件更新(newer than source)时是否仍要保持同步,遇到软链接时是拷贝软链接本身还是拷贝软链接所指向的文件,目标文件已存在时是否要先对其做个备份等等。

rsync同步过程中由两部分模式组成:决定哪些文件需要同步的检查模式以及文件同步时的同步模式。

  1. 检查模式是指按照指定规则来检查哪些文件需要被同步,例如哪些文件是明确被排除不传输的。默认情况下,rsync使用”quick check”算法快速检查源文件和目标文件的大小、mtime(修改时间)是否一致,如果不一致则需要传输。当然,也可以通过在rsync命令行中指定某些选项来改变quick check的检查模式,比如”–size-only”选项表示”quick check”将仅检查文件大小不同的文件作为待传输文件。rsync支持非常多的选项,其中检查模式的自定义性是非常有弹性的。
  2. 同步模式是指在文件确定要被同步后,在同步过程发生之前要做哪些额外工作。例如上文所说的是否要先删除源主机上没有但目标主机上有的文件,是否要先备份已存在的目标文件,是否要追踪链接文件等额外操作。rsync也提供非常多的选项使得同步模式变得更具弹性。

相对来说,为rsync手动指定同步模式的选项更常见一些,只有在有特殊需求时才指定检查模式,因为大多数检查模式选项都可能会影响rsync的性能。

安装步骤

安装nginx

1
yum -y install nginx

配置nginx

1
2
3
4
5
vim /usr/local/nginx/conf/nginx.conf #编辑配置文件,在http {下面添加以下内容:

autoindex on; #开启nginx目录浏览功能
autoindex_exact_size off; #文件大小从KB开始显示
autoindex_localtime on; #显示文件修改时间为服务器本地时间

重启nginx

1
systemctl restart nginx

此时可在浏览器输入服务器IP地址查看是否开启了目录浏览

同步镜像源

安装rsync同步软件

1
yum -y install rsync xinetd

创建目录

1
2
mkdir -p /usr/local/nginx/html/centos #CentOS官方标准源
mkdir -p /usr/local/nginx/html/epel #第三方epel源

确定源同步地址

此处选择清华大学TUNA镜像源

创建同步源脚本

1
2
3
4
5
6
7
8
9
mkdir -p /home/crontab #创建目录

vim /home/crontab/yum_rsync.sh #添加以下代码

#!/bin/bash

/usr/bin/rsync -avrt rsync://mirrors.tuna.tsinghua.edu.cn/centos/ --exclude-from=/usr/local/nginx/html/exclude_centos.list /usr/local/nginx/html/centos/

/usr/bin/rsync -avrt rsync://mirrors.tuna.tsinghua.edu.cn/epel/ --exclude-from=/usr/local/nginx/html/exclude_epel.list /usr/local/nginx/html/epel/

给脚本添加执行权限

1
chmod +x /home/crontab/yum_rsync.sh

创建不需要同步的文件列表

1
2
3
4
5
cd /usr/local/nginx/html/   #进入目录

touch exclude_centos.list #创建文件

touch exclude_epel.list #创建文件

把不需要同步的目录写到上面对应的文件中,每行一个目录,如:

1
2
3
4
5
6
vim exclude_epel.list

4/
4AS/
4ES/
4WS/

添加定时脚本任务

1
2
3
4
5
6
vim /etc/crontab  #在最后一行添加以下代码

0 1 * * * root /home/crontab/yum_rsync.sh #设置每天凌晨1点整开始执行脚本
:wq! #保存退出

service crond restart #重启

注意:等待脚本执行完毕,首次同步,耗费的时间比较长!

创建repo配置文件

CentOS7-centos配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
baseurl=http://192.168.100.250/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://192.168.100.250/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=http://192.168.100.250/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://192.168.100.250/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
baseurl=http://192.168.100.250/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://192.168.100.250/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://192.168.100.250/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://192.168.100.250/centos/RPM-GPG-KEY-CentOS-7

CentOS7-epel配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://192.168.100.250/epel/7/$basearch
#mirrorlist=http://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=http://192.168.100.250/epel/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://192.168.100.250/epel/7/$basearch/debug
#mirrorlist=http://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=http://192.168.100.250/epel/RPM-GPG-KEY-EPEL-7
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://192.168.100.250/epel/7/SRPMS
#mirrorlist=http://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=http://192.168.100.250/epel/RPM-GPG-KEY-EPEL-7
gpgcheck=1

测试

1
2
3
4
yum clean all
yum makecache
yum repolist
yum instll ntp
--------------------本文结束,感谢您的阅读--------------------

本文标题:CentOS7利用rsync搭建内网yum源镜像服务器

文章作者:弓昭

发布时间:2019年01月08日 - 21:38

最后更新:2020年04月08日 - 22:20

原始链接:https://gongzhao1.gitee.io/CentOS7利用rsync搭建内网yum源镜像服务器/

联系邮箱:gongzhao1@foxmail.com