自己寫的初始化指令碼,其實也包含了一些功能,以後如果有什麼想法,會繼續在選項中追加

凨筝發表於2024-03-08
#!/bin/bash

base_ori(){
	#1.關閉防火牆
	stop_firewalld() {
		fw_stat=$(systemctl status firewalld | awk '/Active/{print $3}')
		if [ $fw_stat == "(running)" ]; then
			systemctl stop firewalld && echo "關閉防火牆"
		fi

		fw_en=$(systemctl is-enabled firewalld)
		if [ $fw_en == "enabled" ]; then
			systemctl disable firewalld && echo "關閉防火牆自啟動"
		fi
		echo "防火牆關閉完成"
	} 

	#2.關閉SELinux
	stop_selinux() {
		sed -i.bak '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config && echo "關閉SELinux完成"
	}

	#3.掛載sr0
	mount_sr0() {
		echo "開始掛載sr0"
		cd /mnt/
		if [ ! -d cdrom ]; then
			mkdir cdrom
		fi
		echo "/dev/sr0	/mnt/cdrom	iso9660	defaults	0 0" >>/etc/fstab && mount -a && echo "掛載sr0完成" || echo "掛載sr0失敗" exit 1 
	}

	#4.配置yum倉庫
	yum_repo() {
		echo "開始配置yum倉庫"
		yum_addr=/etc/yum.repos.d
		cd $yum_addr
		mkdir repos
		mv *.repo repos
		cat <<EOF >$yum_addr/development.repo
[development]
name=development
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
EOF
		yum repolist && echo "配置yum倉庫完成"
	}
	#5.安裝軟體
	ins_app() {
		ins_package="vim gcc* apr apr-devel apr-util apr-util-devel pcre pcre-devel net-tools tree wget lrzsz cyrus-sasl-devel expat-devel libdb-devel openldap-devel openssl* chrony"
		yum -y install $ins_package && echo "軟體包安裝完成"
	}

	#6.校檢
	check_log() {
		clear
		echo "1.校檢防火牆自啟動狀態"
		systemctl is-enabled firewalld
		echo "2.校檢防火牆當前狀態"
		systemctl status firewalld | grep Active
		echo "3.校檢SELinux狀態"
		cat /etc/selinux/config | grep -v "^#" | grep -v "^$" | head -1
		echo "4.校檢CD-ROM是否成功掛載"
		lsblk | grep rom
		echo "5.檢視repo倉庫是否配置成功"
		yum repolist
		echo "6.校檢ip"
		ifconfig ens33 | awk 'NR==2{print $2}'
	}
	#7.呼叫程式
	echo "1.開始關閉防火牆"
	stop_firewalld
	echo "2.開始關閉SELinux"
	stop_selinux
	echo "3.開始掛載sr0"
	mount_sr0
	echo "4.開始配置yum倉庫"
	yum_repo
	echo "5.開始安裝軟體"
	ins_app
	echo "6.校檢流程"
	check_log
}

network_config(){
	read -p "請輸入網路卡名稱: " interface
	read -p "請輸入IP地址: " ip_addr
	read -p "請輸入子網掩碼: " prefix_code
	read -p "請輸入閘道器: " gate_code
	interface_addr=/etc/sysconfig/network-scripts/ifcfg-$interface
	echo "開始配置網路卡"
	# sed -i '/^BOOTPROTO=/cBOOTPROTO=none' $interface_addr
	# sed -i '/^ONBOOT=/cONBOOT=yes' $interface_addr
	cat > $interface_addr <<EOF
TYPE=Ethernet
BOOTPROTO=none
DEVICE=$interface
NAME=$interface
ONBOOT=yes
IPADDR=$ip_addr
PREFIX=$prefix_code
GATEWAY=$gate_code
DNS1=114.114.114.114
EOF
	systemctl restart network && echo "網路卡配置完成" || echo "網路卡配置失敗"
}

kickstart(){
	#declare var
	yum_ins="yum -y install"
	sys_res="systemctl restart"

	#1.配置dhcp
	dhcp_cfg() {
		#declare dhcp_var
		dhcp_path="/etc/dhcp"
		dhcp_conf=$dhcp_path/dhcpd.conf
		# 網段
		net_seg="192.168.152.0"
		# 子網掩碼
		net_mask="255.255.255.0"
		#地址池
		ran="192.168.152.201 192.168.152.220"
		# 閘道器
		op_router="192.168.152.2"
		# tftp主機
		next_ser="192.168.152.200"

		$yum_ins dhcp
		#解決cp覆蓋時強制詢問
		rm -rf $dhcp_conf
		cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example $dhcp_conf
		#去除註釋和空行
		sed -i "/^$/d;/^#/d" $dhcp_conf
		#將內容存入中轉檔案
		cat $dhcp_conf | awk 'NR<6{print $0}' >$dhcp_path/transfer
		#將中轉內容轉回並刪除
		cat $dhcp_path/transfer >$dhcp_conf
		rm -rf $dhcp_path/transfer
		#配置dhcpd.conf檔案
		cat >>$dhcp_conf <<EOF
subnet $net_seg netmask $net_mask {
range $ran;
option routers $op_router;
next-server $next_ser;
filename "pxelinux.0";
}
EOF
		#重啟dhcp
		if $sys_res dhcpd &>/dev/null; then
			echo "dhcp啟動完成"
		else
			echo "dhcp啟動失敗"
			exit 1
		fi

	}

	#2.配置tftp
	tftp_cfg() {
		$yum_ins tftp-server xinetd lsof
		sed -i.bak '14s/yes/no/' /etc/xinetd.d/tftp
		$sys_res xinetd
		echo "配置tftp完成"
	}

	#3.配置syslinux
	sys_kick() {
		#路徑
		pxe_path="/var/lib/tftpboot"
		pxe_cfg="$pxe_path/pxelinux.cfg"
		pxe_def="$pxe_cfg/default"

		$yum_ins syslinux system-config-kickstart
		mkdir $pxe_cfg
		cp /usr/share/syslinux/pxelinux.0 $pxe_path
		cp /mnt/cdrom/images/pxeboot/* $pxe_path
		cp /mnt/cdrom/isolinux/isolinux.cfg $pxe_def
		sed -i '1s/vesamenu.c32/linux/' $pxe_def
		sed -i '64a\  append initrd=initrd.img inst.repo=ftp://192.168.152.200/pub inst.ks=ftp://192.168.152.200/ks.cfg' $pxe_def
		sed -i '64d' $pxe_def

		echo "配置syslinux完成"
	}

	#4.安裝vsftpd
	ins_ftp() {
		if $yum_ins vsftpd &>/dev/null; then
			if $sys_res vsftpd &>/dev/null; then
				echo "安裝vsftpd完成"
			else
				echo "重啟vsftpd失敗"
				exit 1
			fi
		else
			echo "安裝vsftp失敗"
			exit 1
		fi
	}

	#5.準備執行Kickstart
	use_kick() {
		mount /dev/sr0 /var/ftp/pub/
		cat <<EOF >/etc/yum.repos.d/development.repo
[development]
name=origin
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
EOF
		yum clean all
		yum repolist
		echo "配置development yum倉庫完成"
		#安裝圖形化
		$yum_ins xorg-x11-xauth
		#安裝語言包
		yum -y groupinstall "fonts"
		echo "kickstart最後準備完成"
	}

	#執行Kickstart
	echo "1.開始配置dhcp"
	dhcp_cfg
	echo "2.開始配置tftp"
	tftp_cfg
	echo "3.開始配置syslinux"
	sys_kick
	echo "4.安裝vsftpd"
	ins_ftp
	echo "5.執行kickstart"
	use_kick
	echo "final: 開始kickstart"

	echo "接下來需要重連xshell"
	echo "透過system-config-kickstart命令進入配置圖形化介面"
	    
}

apache_install(){
	if tar zxf httpd-2.4.54.tar.gz && cd httpd-2.4.54/ 1>/dev/null; then
		./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-ssl --enable-mpms-shared=all --enable-mpm-with=event && make && make install 
		if [ $? -eq 0 ] ; then
			if ln -s /usr/local/apache/bin/* /usr/local/bin/ 1>/dev/null; then
				# 修改PATH環境變數
               if echo "PATH=$PATH:/usr/local/apache/bin" >>/etc/profile && source /etc/profile 1>/dev/null; then
                  echo "httpd ok!"
               else
                  echo "httpd no!"
               fi
            else
               echo "ln!"
               exit 1
            fi
		else
			ehco "apache安裝失敗"
		fi
	else
		echo "沒有Apache_tar包"
	fi
	# 編輯.service指令碼
	cat >/usr/lib/systemd/system/httpd.service <<EOF
[Unit]
# 服務名稱
Description=httpd 
# 在network服務後啟動
After=network.target 

[Service]  
 # 啟動型別
Type=forking   
# 配置變數
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl restart
ExecStop=/usr/local/apache/bin/apachectl stop
PrivateTmp=true                 

[Install]
# 表示"WantedBy"服務開機自啟 
WantedBy=multi-user.target 
EOF

	if [ $? == "0" ]; then

	clear
	echo

	systemctl restart httpd
	echo -e "\napache 啟動成功!\n"

	systemctl enable httpd
	echo -e "\napache 開機自啟動設定成功!\n"

	echo -e "apache 執行狀態如下:\n"
	systemctl status httpd

	echo -e "\napache 埠執行狀態如下:\n"
	netstat -antup | grep httpd

	fi
}

mysql_install(){
	# 全域性變數
	# MySQL原始碼包
	mysql_rpm="mysql-community-5.7.38-1.el7.src.rpm"
	# 原始碼路徑
	src_path="/usr/local/src"
	# MySQL安裝路徑
	mysql_path="/usr/local/mysql"
	# 資料儲存目錄
	data_path="/data/mysql"

	if [ ! -e $mysql_rpm ] ; then
		echo -e "當前路徑沒有${mysql_rpm}包"
		exit 1
	fi

	# 安裝軟體,解壓tar包
	app(){
		# tar包
		boost_tar="boost_1_59_0.tar.bz2"
		mysql_tar="mysql-5.7.38.tar.gz"
		# 需要用到的軟體
		app_ins="cmake make gcc gcc-c++ bison ncurses ncurses-devel openssl openssl-devel bzip2"

		# 1.解除安裝系統自帶boost
		yum -y remove boost-*
		echo "刪除系統自帶的boost軟體"

		# 2.安裝必須得軟體
		yum -y install $app_ins
		echo "安裝必須得依賴軟體結束"

		# 3.安裝MySQL原始碼包
		rpm -ivh $mysql_rpm
		echo "安裝mysql_rpm完成"

		# 4.將tar包解壓到/usr/local/src/
		cd rpmbuild/SOURCES/
		tar jxvf $boost_tar -C $src_path
		tar zxvf $mysql_tar -C $src_path
		echo "tar包解壓完成"
	}

	# 安裝MySQL
	install(){
		# MySQL賬戶名稱
		user_name="mysql"
		# MySQL許可權
		user_judge=$user_name.$user_name
		# 預編譯選項
		mysql_opt=" 
			-DCMAKE_INSTALL_PREFIX=$mysql_path \
			-DMYSQL_DATADIR=$data_path/data \
			-DSYSCONFDIR=/etc \
			-DWITH_MYISAM_STORAGE_ENGINE=1 \
			-DWITH_INNOBASE_STORAGE_ENGINE=1 \
			-DWITH_MEMORY_STORAGE_ENGINE=1 \
			-DWITH_READLINE=1 \
			-DMYSQL_UNIX_ADDR=$mysql_path/mysql.sock \
			-DMYSQL_TCP_PORT=3306 \
			-DWITH_PARTITION_STORAGE_ENGINE=1 \
			-DEXTRA_CHARSETS=all \
			-DDEFAULT_CHARSET=utf8 \
			-DDEFAULT_COLLATION=utf8_general_ci \
			-DDOWNLOAD_BOOST=1 \
			-DWITH_BOOST=$src_path/boost_1_59_0
		"

		# 1.建立MySQL賬號
		useradd -M -s /sbin/nologin $user_name
		echo "建立MySQL賬號結束"

		# 2.建立MySQL資料儲存目錄
		mkdir -p $data_path/data
		mkdir -p $data_path/log
		chown -R $user_judge $data_path
		echo "建立資料庫儲存目錄結束"

		# 3.編譯安裝MySQL
		cd $src_path/mysql-5.7.38/
		cmake $mysql_opt && make -j $(grep processor /proc/cpuinfo | wc -l) && make install

		# 4.修改目錄許可權
		chown -R $user_judge $mysql_path
		echo "安裝mysql完成"
	}

	# 配置啟動MySQL
	config(){
		# MySQL配置檔案
		my_cnf="/etc/my.cnf"
		# 指令碼路徑
		mysqld_path="/etc/init.d/mysqld"
		# 初始化資料庫選項
		mysqld_opt="
			--initialize-insecure \
			--user=mysql \
			--basedir=$mysql_path \
			--datadir=$data_path/data
		"
		# MySQL預設使用者名稱密碼
		default_name="root"
		default_passwd="root"

		# 1.修改MySQL主配置檔案
		mv $my_cnf{,.bak}
		cat > $my_cnf <<EOF
[mysqld]
basedir=$mysql_path         #mysql安裝目錄
datadir=$data_path/data     #mysql資料存放目錄
port=3306                   #mysql監聽埠
socket=$mysql_path/mysql.sock #mysql.sock檔案存放目錄
symbolic-links=0               #關閉mysql的符號連結
character-set-server=utf8        #指定mysql的字符集為utf8
log-error=$data_path/log/mysqld.log     #指定mysql的錯誤日誌存放路徑
pid-file=$mysql_path/mysqld.pid #mysql的pid檔案存放目錄
datadir=$data_path/data #xtrabackup存放備份檔案路徑
log-bin=$data_path/log/mysql_bin # 二進位制日誌binlog檔案位置和名稱
server-id=1 #MySQL伺服器的唯一識別符號,用於主從同步
EOF
		echo "配置MySQL主配置檔案完成"
		# 2.配置指令碼
		cp $mysql_path/support-files/mysql.server $mysqld_path
		chmod +x $mysqld_path

		# 3. 配置指令碼開機自啟
		chkconfig --add mysqld
		chkconfig mysqld on
		chkconfig --list mysqld
		echo "配置指令碼完成"

		# 4.初始化資料庫
		${mysql_path}/bin/mysqld $mysqld_opt
		echo "初始化資料庫結束"

		# 5.啟動資料路
		$mysqld_path start
		netstat -antup | grep 3306

		# 6.將MySQL命令軟連線到/usr/local/bin
		ln -s $mysql_path/bin/* /usr/local/bin/
		echo "將MySQL命令軟連結到/usr/local/bin"

		# 7.設定MySQL密碼為"root
		mysqladmin -u root password "root"
		echo "使用者名稱root的密碼為\"root\""

		# 8.登入測試驗證
		mysql -u $default_name -p $default_passwd
	}

	# 執行指令碼
	app
	install
	config
}


start(){
	options=("基礎初始化" "配置網路卡" "配置kickstart" "原始碼Apache安裝" "原始碼MySQL安裝" "結束配置")
	opt_length=${#options[@]}
	while true; do
		select i in ${options[*]} ; do
			case $i in 
				${options[0]})
					base_ori
				;;
				${options[1]})
					network_config
				;;
				${options[2]})
					kickstart
				;;
				${options[3]})
					apache_install
				;;
				${options[4]})
					mysql_install
				;;
				${options[$((opt_length - 1))]})
					echo "退出"
					break 2
				;;
				*)
					echo "\e[31m沒有那個選項!\e[0m"
					break 2
				;;
			esac
		done
	done
}

start

相關文章