宝塔面板数据从系统盘迁移到数据盘

作者: print("") 分类: linux 发布时间: 2018-06-11 17:14

宝塔面板数据从系统盘迁移到数据盘

很多人问我,怎么把宝塔的/www 数据迁移到数据盘中,那么下面就来给大家讲解如何迁移到数据盘emmmm,现在是这样的。我有一台安装有宝塔的机器。里面有两盘硬盘。那么首先把数据盘先格式化把

格式化

格式化了,那么就挂载把


那么,需要停止掉所有的服务。这里有一个脚本脚本如下:

#!/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

#检测磁盘数量
startAction=$1

service_Action(){

	if [ -f "/www/server/panel/data/502Task.pl" ]; then
		rm -f /www/server/panel/data/502Task.pl
	fi

	if [ -f "/www/swap" ] && [ "${action}" == "stop" ];then
		swapoff /www/swap
	fi

	/etc/init.d/bt ${action}

	for service in nginx httpd mysqld pure-ftpd tomcat redis memcached mongodb
	do
		if [ -f "/etc/init.d/${service}" ];then
			/etc/init.d/${service} ${action}
		fi
	done

	for phpV in 52 53 54 55 56 70 71 72
	do
		if [ -f "/etc/init.d/php-fpm-${phpV}" ];then
			/etc/init.d/php-fpm-${phpV} ${action}
		fi
	done

	if [ -f "/www/swap" ] && [ "${action}" == "start" ];then
		swapon /www/swap
	fi
}
if [ "${startAction}" == "stop" ]; then
	action="stop"
	service_Action
	exit;
elif [ "${startAction}" == "start" ]; then
	action="start"
	service_Action
	echo "True" > /www/server/panel/data/502Task.pl
	exit;
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 '/\/www/ {print $6 "Disk"}' | awk '/\/wwwDisk/ {print $1}'`
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
}

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/main.pyc" ]; 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 ""
		action="stop"
		service_Action
		echo -e ""
		mv /www /bt-backup
		echo -e "disk partition..."
		echo -e "磁盘分区..."
		sleep 2
		echo -e ""
		fdiskP
		mountDisk=`df -h | awk '/\/www/ {print $6 "Disk"}' | awk '/\/wwwDisk/ {print $1}'`
		if [ "${mountDisk}" = "" ]; then
			echo -e "挂载失败!"
			echo -e "正在还原数据"
			mv /www /bt-backup
			echo -e "还原成功,启动宝塔服务"
			action="start"
			service_Action
			echo "True" > /www/server/panel/data/502Task.pl
			exit;
		fi
		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 ""
		action="start"
		service_Action
		echo "True" > /www/server/panel/data/502Task.pl
	fi
else
	fdiskP
	mountDisk=`df -h | awk '/\/www/ {print $6 "Disk"}' | awk '/\/wwwDisk/ {print $1}'`
	if [ "${mountDisk}" = "" ]; then
		echo -e "挂载失败!"
		exit;
	fi
	echo -e ""
	echo -e "Done"
	echo -e "挂载成功"
fi

后面把。文件放到数据盘中

[root@localhost ~]# mv /www  /www2/


等把。看你的数据量。唔。慢慢等,mv 弄完之后呢。就可以卸载硬盘。后面挂载到www目录

后面启动一下。还是那个脚本

[root@localhost ~]# sh auto_disk.sh start
Starting Bt-Panel... done
Starting Bt-Tasks... done
Starting nginx...  done
Starting MySQL.. SUCCESS! 
Starting Pure-FTPd... /etc/init.d/pure-ftpd: line 26: /www/server/pure-ftpd/sbin/pure-config.pl: No such file or directory
failed
Starting php-fpm /etc/init.d/php-fpm-54: line 57: /www/server/php/54/sbin/php-fpm: No such file or directory
failed
Starting php-fpm  done
[root@localhost ~]# 


访问一下把

完成了!!!!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

一条评论
  • pandalin

    2020年7月9日 下午10:22

    系统盘40g,已经用了36g,无法备份。整个服务器硬盘有快照。是不是可以不用bt-back. 备份www的数据,是不是跳过备份这个步骤。进行操作即可

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注