Linux服務器分區一鍵自動掛載硬盤工具


整理目前可用一鍵腳本可以直接掛載磁盤,比如我們常見的可以用到阿里雲、騰訊雲等服務器。如果我們數據不是太多的話,默認的50GB磁盤放系統和數據是足夠的,個人覺得無需掛載盤單獨購買。當然,如果我們項目以后數據確實較大,開始就要做好掛載磁盤。

寶塔官方腳本

#Centos系統請使用以下命令
  yum install wget -y && wget -O auto_disk.sh http://download.bt.cn/tools/auto_disk.sh && bash auto_disk.sh #Ubuntu系統請使用以下命令
  wget -O auto_disk.sh http://download.bt.cn/tools/auto_disk.sh && sudo bash auto_disk.sh #Debian系統請使用以下命令
  wget -O auto_disk.sh http://download.bt.cn/tools/auto_disk.sh && bash auto_disk.sh

wn789備份版(寶塔)

#Centos/Fedora:
  yum install wget -y && wget -O auto_disk.sh https://raw.githubusercontent.com/wn789/www/master/auto_disk.sh && bash auto_disk.sh
#Ubuntu:
  wget -O auto_disk.sh https://raw.githubusercontent.com/wn789/www/master/auto_disk.sh && sudo bash auto_disk.sh
#Debian:
  wget -O auto_disk.sh https://raw.githubusercontent.com/wn789/www/master/auto_disk.sh && bash auto_disk.sh

zhuji999備份版(寶塔)

#Centos系統請使用以下命令:
yum install wget -y && wget -O auto_disk.sh https://raw.githubusercontent.com/zhuji9999/BT-Panel-Linux/master/auto_disk.sh && bash auto_disk.sh
#Ubuntu系統請使用以下命令:
wget -O auto_disk.sh https://raw.githubusercontent.com/zhuji9999/BT-Panel-Linux/master/auto_disk.sh && sudo bash auto_disk.sh
#Debian系統請使用以下命令:
wget -O auto_disk.sh https://raw.githubusercontent.com/zhuji9999/BT-Panel-Linux/master/auto_disk.sh && bash auto_disk.sh

Linux運維筆記腳本

wget http://mirrors.linuxeye.com/scripts/auto_fdisk.sh
chmod +x ./auto_fdisk.sh
./auto_fdisk.sh

下面的腳本是  自建  的,請熟悉代碼流程再使用避免出錯,不同環境不敢保證通用。

#/bin/bash
item=(`lsblk |awk -F " " '$1!~/sda/{print $1}'|grep "sd"|grep -v sdb|xargs`)
num=01
dir=/srv/BigData/data
for var in ${item[@]}
    do
        parted -s /dev/${var} mklabel gpt
        parted -s /dev/${var} mkpart logic 100M 100%
        echo "y\r"|mkfs.ext4 /dev/${var}
    done
Blkid(`blkid /dev/sd* |awk -F [:" "] '$1!~/sda/{print $1}'|xargs`)
for var in ${Blkid[@]}
    do
        if [ ! -d "$dir$num" ];then
            mkdir "${dir}${num}"
        else
            echo "the dir is exist"
        fi
        chmod 000 "${dir}${num}"
        `partprobe`
        uuid = `blkid $var`
        echo UUID=${uuid:16:36} "${dir}${num}" ext4 default,noatime,nodiratime 1 0 >>/etc/fstab
        let "num++"
    done
mount -a
chown 2000:wheel "${dir}"*

 寶塔腳本備份,  備份日期2021/02/08 AM:00.32  

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8
setup_path=/www
#if [ $1 != "" ];then
	#setup_path=$1;
#fi

#檢測磁盤數量
sysDisk=`cat /proc/partitions|grep -v name|grep -v ram|awk '{print $4}'|grep -v '^$'|grep -v '[0-9]$'|grep -v 'vda'|grep -v 'xvda'|grep -v 'sda'|grep -e 'vd' -e 'sd' -e 'xvd'`
if [ "${sysDisk}" == "" ]; then
	echo -e "ERROR!This server has only one hard drive,exit"
	echo -e "此服務器只有一塊磁盤,無法掛載"
	echo -e "Bye-bye"
	exit;
fi
#檢測/www目錄是否已掛載磁盤
mountDisk=`df -h | awk '{print $6}' |grep www`
if [ "${mountDisk}" != "" ]; then
	echo -e "www directory has been mounted,exit"
	echo -e "www目錄已被掛載,不執行任何操作"
	echo -e "Bye-bye"
	exit;
fi
#檢測是否有windows分區
winDisk=`fdisk -l |grep "NTFS\|FAT32"`
if [ "${winDisk}" != "" ];then
	echo 'Warning: The Windows partition was detected. For your data security, Mount manually.';
	echo "危險 數據盤為windwos分區,為了你的數據安全,請手動掛載,本腳本不執行任何操作。"
	exit;
fi
echo "
+----------------------------------------------------------------------
| Bt-WebPanel Automatic disk partitioning tool
+----------------------------------------------------------------------
| Copyright © 2015-2017 BT-SOFT(http://www.bt.cn) All rights reserved.
+----------------------------------------------------------------------
| Auto mount partition disk to $setup_path
+----------------------------------------------------------------------
"


#數據盤自動分區
fdiskP(){
	
	for i in `cat /proc/partitions|grep -v name|grep -v ram|awk '{print $4}'|grep -v '^$'|grep -v '[0-9]$'|grep -v 'vda'|grep -v 'xvda'|grep -v 'sda'|grep -e 'vd' -e 'sd' -e 'xvd'`;
	do
		#判斷指定目錄是否被掛載
		isR=`df -P|grep $setup_path`
		if [ "$isR" != "" ];then
			echo "Error: The $setup_path directory has been mounted."
			return;
		fi
		
		isM=`df -P|grep '/dev/${i}1'`
		if [ "$isM" != "" ];then
			echo "/dev/${i}1 has been mounted."
			continue;
		fi
			
		#判斷是否存在未分區磁盤
		isP=`fdisk -l /dev/$i |grep -v 'bytes'|grep "$i[1-9]*"`
		if [ "$isP" = "" ];then
				#開始分區
				fdisk -S 56 /dev/$i << EOF
n
p
1


wq
EOF

			sleep 5
			#檢查是否分區成功
			checkP=`fdisk -l /dev/$i|grep "/dev/${i}1"`
			if [ "$checkP" != "" ];then
				#格式化分區
				mkfs.ext4 /dev/${i}1
				mkdir $setup_path
				#掛載分區
				sed -i "/\/dev\/${i}1/d" /etc/fstab
				echo "/dev/${i}1    $setup_path    ext4    defaults    0 0" >> /etc/fstab
				mount -a
				df -h
			fi
		else
			#判斷是否存在Windows磁盤分區
			isN=`fdisk -l /dev/$i|grep -v 'bytes'|grep -v "NTFS"|grep -v "FAT32"`
			if [ "$isN" = "" ];then
				echo 'Warning: The Windows partition was detected. For your data security, Mount manually.';
				return;
			fi
			
			#掛載已有分區
			checkR=`df -P|grep "/dev/$i"`
			if [ "$checkR" = "" ];then
					mkdir $setup_path
					sed -i "/\/dev\/${i}1/d" /etc/fstab
					echo "/dev/${i}1    $setup_path    ext4    defaults    0 0" >> /etc/fstab
					mount -a
					df -h
			fi
			
			#清理不可寫分區
			echo 'True' > $setup_path/checkD.pl
			if [ ! -f $setup_path/checkD.pl ];then
					sed -i "/\/dev\/${i}1/d" /etc/fstab
					mount -a
					df -h
			else
					rm -f $setup_path/checkD.pl
			fi
		fi
	done
}
stop_service(){

	/etc/init.d/bt stop

	if [ -f "/etc/init.d/nginx" ]; then
		/etc/init.d/nginx stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/httpd" ]; then
		/etc/init.d/httpd stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/mysqld" ]; then
		/etc/init.d/mysqld stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/pure-ftpd" ]; then
		/etc/init.d/pure-ftpd stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/tomcat" ]; then
		/etc/init.d/tomcat stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/redis" ]; then
		/etc/init.d/redis stop > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/memcached" ]; then
		/etc/init.d/memcached stop > /dev/null 2>&1
	fi

	if [ -f "/www/server/panel/data/502Task.pl" ]; then
		rm -f /www/server/panel/data/502Task.pl
		if [ -f "/etc/init.d/php-fpm-52" ]; then
			/etc/init.d/php-fpm-52 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-53" ]; then
			/etc/init.d/php-fpm-53 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-54" ]; then
			/etc/init.d/php-fpm-54 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-55" ]; then
			/etc/init.d/php-fpm-55 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-56" ]; then
			/etc/init.d/php-fpm-56 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-70" ]; then
			/etc/init.d/php-fpm-70 stop > /dev/null 2>&1
		fi

		if [ -f "/etc/init.d/php-fpm-71" ]; then
			/etc/init.d/php-fpm-71 stop > /dev/null 2>&1
		fi
	fi
}

start_service()
{
	/etc/init.d/bt start

	if [ -f "/etc/init.d/nginx" ]; then
		/etc/init.d/nginx start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/httpd" ]; then
		/etc/init.d/httpd start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/mysqld" ]; then
		/etc/init.d/mysqld start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/pure-ftpd" ]; then
		/etc/init.d/pure-ftpd start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/tomcat" ]; then
		/etc/init.d/tomcat start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/redis" ]; then
		/etc/init.d/redis start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/memcached" ]; then
		/etc/init.d/memcached start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-52" ]; then
		/etc/init.d/php-fpm-52 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-53" ]; then
		/etc/init.d/php-fpm-53 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-54" ]; then
		/etc/init.d/php-fpm-54 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-55" ]; then
		/etc/init.d/php-fpm-55 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-56" ]; then
		/etc/init.d/php-fpm-56 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-70" ]; then
		/etc/init.d/php-fpm-70 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-71" ]; then
		/etc/init.d/php-fpm-71 start > /dev/null 2>&1
	fi

	if [ -f "/etc/init.d/php-fpm-72" ]; then
		/etc/init.d/php-fpm-71 start > /dev/null 2>&1
	fi
	
	if [ -f "/etc/init.d/php-fpm-73" ]; then
		/etc/init.d/php-fpm-71 start > /dev/null 2>&1
	fi

	echo "True" > /www/server/panel/data/502Task.pl
}

while [ "$go" != 'y' ] && [ "$go" != 'n' ]
do
	read -p "Do you want to try to mount the data disk to the $setup_path directory?(y/n): " go;
done

if [ "$go" = 'n' ];then
	echo -e "Bye-bye"
	exit;
fi

if [ -f "/etc/init.d/bt" ] && [ -f "/www/server/panel/data/port.pl" ]; then
	disk=`cat /proc/partitions|grep -v name|grep -v ram|awk '{print $4}'|grep -v '^$'|grep -v '[0-9]$'|grep -v 'vda'|grep -v 'xvda'|grep -v 'sda'|grep -e 'vd' -e 'sd' -e 'xvd'`
	diskFree=`cat /proc/partitions |grep ${disk}|awk '{print $3}'`
	wwwUse=`du -sh -k /www|awk '{print $1}'`

	if [ "${diskFree}" -lt "${wwwUse}" ]; then
		echo -e "Sorry,your data disk is too small,can't coxpy to the www."
		echo -e "對不起,你的數據盤太小,無法遷移www目錄數據到此數據盤"
		exit;
	else
		echo -e ""
		echo -e "stop bt-service"
		echo -e "停止寶塔服務"
		echo -e ""
		sleep 3
		stop_service
		echo -e ""
		mv /www /bt-backup
		echo -e "disk partition..."
		echo -e "磁盤分區..."
		sleep 2
		echo -e ""
		fdiskP
		echo -e ""
		echo -e "move disk..."
		echo -e "遷移數據中..."
		\cp -r -p -a /bt-backup/* /www
		echo -e ""
		echo -e "Done"
		echo -e "遷移完成"
		echo -e ""
		echo -e "start bt-service"
		echo -e "啟動寶塔服務"
		echo -e ""
		start_service
	fi
else
	fdiskP
	echo -e ""
	echo -e "Done"
	echo -e "掛載成功"
fi

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM