shell实现部署ftp提供共享yum仓库

shell

    • 脚本实现
    • 脚本测试

脚本实现自动化配置vsftp,并共享opt目录给其它节点机器实现离线源配置

使用脚本需修改以下内容:
ftp_ip为ftp服务器ip
reponame为repo文件名字
dir为仓库目录,默认opt下
houzui为opt目录下的离线仓库,写入到houzui中为数组
使用前还需保持服务器可以下载vsftp服务

脚本实现

#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi

脚本测试

节点 IP
节点1 192.168.200.80
节点2 192.168.200.81
节点3 192.168.200.82

手动配置好离线仓库

[root@localhost ~]# mkdir /opt/{centos,iaas}
[root@localhost ~]# mount /dev/sr0 /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cp -rf /opt/centos/* /opt/iaas/

脚本部署

[root@localhost ~]# cat repo.sh
#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi
[root@localhost ~]# hostname -I
192.168.200.80
[root@localhost ~]# bash repo.sh
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=file:///opt/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                                                           | 3.6 kB  00:00:00
iaas                                                                                             | 3.6 kB  00:00:00
(1/4): centos/group_gz                                                                           | 153 kB  00:00:00
(2/4): centos/primary_db                                                                         | 3.3 MB  00:00:00
(3/4): iaas/group_gz                                                                             | 153 kB  00:00:00
(4/4): iaas/primary_db                                                                           | 3.3 MB  00:00:00
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]#

从节点验证

[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clnen all;yum repolist
Loaded plugins: fastestmirror
No such command: clnen. Please use /usr/bin/yum --help
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.81
[root@localhost ~]#
[root@localhost ~]# bash repo.sh
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                                                           | 3.6 kB  00:00:00
iaas                                                                                             | 3.6 kB  00:00:00
(1/4): iaas/group_gz                                                                             | 153 kB  00:00:00
(2/4): centos/group_gz                                                                           | 153 kB  00:00:00
(3/4): centos/primary_db                                                                         | 3.3 MB  00:00:00
(4/4): iaas/primary_db                                                                           | 3.3 MB  00:00:00
repo id                                                  repo name                                                status
centos                                                   centos                                                   4,070
iaas                                                     iaas                                                     4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.82
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas

你可能感兴趣的:(Linux,linux)