宝塔51特惠活动,企业版1099元/年,送SSL证书,最高立减2万元!查看活动
当前位置:论坛首页 > Linux面板 > 讨论

Linux 自动硬盘挂载一键脚本

发表在 Linux面板2024-3-13 03:03 [复制链接] 0 2921

Linux 自动硬盘挂载一键脚本
更具官方的稍微修改了2行代码,自己折腾了好久。
因为官方的会默认自动迁移数据到数据盘。
我自己删除了就成功了。
自己新建一个.sh脚本就可以。
以下为代码,我自己设置的是挂载到web目录,这个可以自己根据个人喜好来修改。
  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=/web
  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 '^

  11. |grep -v '[0-9]

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


  43. #数据盘自动分区
  44. fdiskP(){
  45.        
  46.         for i in `cat /proc/partitions|grep -v name|grep -v ram|awk '{print $4}'|grep -v '^

  47. |grep -v '[0-9]

  48. |grep -v 'vda'|grep -v 'xvda'|grep -v 'sda'|grep -e 'vd' -e 'sd' -e 'xvd'`;
  49.         do
  50.                 #判断指定目录是否被挂载
  51.                 isR=`df -P|grep $setup_path`
  52.                 if [ "$isR" != "" ];then
  53.                         echo "Error: The $setup_path directory has been mounted."
  54.                         return;
  55.                 fi
  56.                
  57.                 isM=`df -P|grep '/dev/${i}1'`
  58.                 if [ "$isM" != "" ];then
  59.                         echo "/dev/${i}1 has been mounted."
  60.                         continue;
  61.                 fi
  62.                        
  63.                 #判断是否存在未分区磁盘
  64.                 isP=`fdisk -l /dev/$i |grep -v 'bytes'|grep "$i[1-9]*"`
  65.                 if [ "$isP" = "" ];then
  66.                                 #开始分区
  67.                                 fdisk -S 56 /dev/$i << EOF
  68. n
  69. p
  70. 1


  71. wq
  72. EOF

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

  117.         /etc/init.d/bt stop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  164. start_service()
  165. {
  166.         /etc/init.d/bt start

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  209.         if [ -f "/etc/init.d/php-fpm-72" ]; then
  210.                 /etc/init.d/php-fpm-71 start > /dev/null 2>&1
  211.         fi
  212.        
  213.         if [ -f "/etc/init.d/php-fpm-73" ]; then
  214.                 /etc/init.d/php-fpm-71 start > /dev/null 2>&1
  215.         fi

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

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

  222. if [ "$go" = 'n' ];then
  223.         echo -e "Bye-bye"
  224.         exit;
  225. fi

  226. if [ -f "/etc/init.d/bt" ] && [ -f "/www/server/panel/data/port.pl" ]; then
  227.         disk=`cat /proc/partitions|grep -v name|grep -v ram|awk '{print $4}'|grep -v '^

  228. |grep -v '[0-9]

  229. |grep -v 'vda'|grep -v 'xvda'|grep -v 'sda'|grep -e 'vd' -e 'sd' -e 'xvd'`
  230.         diskFree=`cat /proc/partitions |grep ${disk}|awk '{print $3}'`
  231.         wwwUse=`du -sh -k /web|awk '{print $1}'`

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

复制代码


使用道具 举报 只看该作者 回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

普通问题处理

论坛响应时间:72小时

问题处理方式:排队(仅解答)

工作时间:白班:9:00 - 18:00

紧急运维服务

响应时间:3分钟

问题处理方式:宝塔专家1对1服务

工作时间:工作日:9:00 - 18:30

宝塔专业团队为您解决服务器疑难问题

点击联系技术免费分析
快速回复 返回顶部 返回列表