查看: 624|回复: 0

[一键脚本] Centos7一键挂载硬盘脚本

[复制链接]

124

主题

0

回帖

508

积分

管理员

积分
508
发表于 2026-8-11 12:17:10 | 显示全部楼层 |阅读模式
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. LANG=en_US.UTF-8
  5. setup_path=/www
  6. #if [ $1 != "" ];then
  7.         #setup_path=$1;
  8. #fi

  9. #检测磁盘数量
  10. 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'`
  11. if [ "${sysDisk}" == "" ]; then
  12.         echo -e "ERROR!This server has only one hard drive,exit"
  13.         echo -e "此服务器只有一块磁盘,无法挂载"
  14.         echo -e "Bye-bye"
  15.         exit;
  16. fi
  17. #检测/www目录是否已挂载磁盘
  18. mountDisk=`df -h | awk '{print $6}' |grep www`
  19. if [ "${mountDisk}" != "" ]; then
  20.         echo -e "www directory has been mounted,exit"
  21.         echo -e "www目录已被挂载,不执行任何操作"
  22.         echo -e "Bye-bye"
  23.         exit;
  24. fi
  25. #检测是否有windows分区
  26. winDisk=`fdisk -l |grep "NTFS\|FAT32"`
  27. if [ "${winDisk}" != "" ];then
  28.         echo 'Warning: The Windows partition was detected. For your data security, Mount manually.';
  29.         echo "危险 数据盘为windwos分区,为了你的数据安全,请手动挂载,本脚本不执行任何操作。"
  30.         exit;
  31. fi
  32. echo "
  33. +----------------------------------------------------------------------
  34. | Bt-WebPanel Automatic disk partitioning tool
  35. +----------------------------------------------------------------------
  36. | Copyright © 2015-2017 BT-SOFT(http://www.bt.cn) All rights reserved.
  37. +----------------------------------------------------------------------
  38. | Auto mount partition disk to $setup_path
  39. +----------------------------------------------------------------------
  40. "


  41. #数据盘自动分区
  42. fdiskP(){
  43.         
  44.         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'`;
  45.         do
  46.                 #判断指定目录是否被挂载
  47.                 isR=`df -P|grep $setup_path`
  48.                 if [ "$isR" != "" ];then
  49.                         echo "Error: The $setup_path directory has been mounted."
  50.                         return;
  51.                 fi
  52.                
  53.                 isM=`df -P|grep '/dev/${i}1'`
  54.                 if [ "$isM" != "" ];then
  55.                         echo "/dev/${i}1 has been mounted."
  56.                         continue;
  57.                 fi
  58.                         
  59.                 #判断是否存在未分区磁盘
  60.                 isP=`fdisk -l /dev/$i |grep -v 'bytes'|grep "$i[1-9]*"`
  61.                 if [ "$isP" = "" ];then
  62.                                 #开始分区
  63.                                 fdisk -S 56 /dev/$i << EOF
  64. n
  65. p
  66. 1


  67. wq
  68. EOF

  69.                         sleep 5
  70.                         #检查是否分区成功
  71.                         checkP=`fdisk -l /dev/$i|grep "/dev/${i}1"`
  72.                         if [ "$checkP" != "" ];then
  73.                                 #格式化分区
  74.                                 mkfs.ext4 /dev/${i}1
  75.                                 mkdir $setup_path
  76.                                 #挂载分区
  77.                                 sed -i "/\/dev\/${i}1/d" /etc/fstab
  78.                                 echo "/dev/${i}1    $setup_path    ext4    defaults    0 0" >> /etc/fstab
  79.                                 mount -a
  80.                                 df -h
  81.                         fi
  82.                 else
  83.                         #判断是否存在Windows磁盘分区
  84.                         isN=`fdisk -l /dev/$i|grep -v 'bytes'|grep -v "NTFS"|grep -v "FAT32"`
  85.                         if [ "$isN" = "" ];then
  86.                                 echo 'Warning: The Windows partition was detected. For your data security, Mount manually.';
  87.                                 return;
  88.                         fi
  89.                         
  90.                         #挂载已有分区
  91.                         checkR=`df -P|grep "/dev/$i"`
  92.                         if [ "$checkR" = "" ];then
  93.                                         mkdir $setup_path
  94.                                         sed -i "/\/dev\/${i}1/d" /etc/fstab
  95.                                         echo "/dev/${i}1    $setup_path    ext4    defaults    0 0" >> /etc/fstab
  96.                                         mount -a
  97.                                         df -h
  98.                         fi
  99.                         
  100.                         #清理不可写分区
  101.                         echo 'True' > $setup_path/checkD.pl
  102.                         if [ ! -f $setup_path/checkD.pl ];then
  103.                                         sed -i "/\/dev\/${i}1/d" /etc/fstab
  104.                                         mount -a
  105.                                         df -h
  106.                         else
  107.                                         rm -f $setup_path/checkD.pl
  108.                         fi
  109.                 fi
  110.         done
  111. }
  112. stop_service(){

  113.         /etc/init.d/bt stop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  160. start_service()
  161. {
  162.         /etc/init.d/bt start

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  205.         if [ -f "/etc/init.d/php-fpm-72" ]; then
  206.                 /etc/init.d/php-fpm-71 start > /dev/null 2>&1
  207.         fi
  208.         
  209.         if [ -f "/etc/init.d/php-fpm-73" ]; then
  210.                 /etc/init.d/php-fpm-71 start > /dev/null 2>&1
  211.         fi

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

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

  218. if [ "$go" = 'n' ];then
  219.         echo -e "Bye-bye"
  220.         exit;
  221. fi

  222. if [ -f "/etc/init.d/bt" ] && [ -f "/www/server/panel/data/port.pl" ]; then
  223.         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'`
  224.         diskFree=`cat /proc/partitions |grep ${disk}|awk '{print $3}'`
  225.         wwwUse=`du -sh -k /www|awk '{print $1}'`

  226.         if [ "${diskFree}" -lt "${wwwUse}" ]; then
  227.                 echo -e "Sorry,your data disk is too small,can't coxpy to the www."
  228.                 echo -e "对不起,你的数据盘太小,无法迁移www目录数据到此数据盘"
  229.                 exit;
  230.         else
  231.                 echo -e ""
  232.                 echo -e "stop bt-service"
  233.                 echo -e "停止宝塔服务"
  234.                 echo -e ""
  235.                 sleep 3
  236.                 stop_service
  237.                 echo -e ""
  238.                 mv /www /bt-backup
  239.                 echo -e "disk partition..."
  240.                 echo -e "磁盘分区..."
  241.                 sleep 2
  242.                 echo -e ""
  243.                 fdiskP
  244.                 echo -e ""
  245.                 echo -e "move disk..."
  246.                 echo -e "迁移数据中..."
  247.                 \cp -r -p -a /bt-backup/* /www
  248.                 echo -e ""
  249.                 echo -e "Done"
  250.                 echo -e "迁移完成"
  251.                 echo -e ""
  252.                 echo -e "start bt-service"
  253.                 echo -e "启动宝塔服务"
  254.                 echo -e ""
  255.                 start_service
  256.         fi
  257. else
  258.         fdiskP
  259.         echo -e ""
  260.         echo -e "Done"
  261.         echo -e "挂载成功"
  262. fi

复制代码
运行一键脚本挂载硬盘命令
  1. chmod +x auto_disk.sh
  2. ./auto_disk.sh
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|蜀ICP备17024538号-6

在本版发帖
关注公众号
QQ客服返回顶部