bash 脚本编程二十一 MongoDB自动部署


这是单机版本的MongoDB自动部署,手动部署可以参考我的另一篇文章:

首先下载mongodb-linux-x86_64-2.2.0.tgz, 解压后放到工程目录mongodb下。

然后准备启动脚本mongodb:

  1. #!/bin/sh  
  2.    
  3. ### BEGIN INIT INFO  
  4. # Provides:     mongodb  
  5. # Required-Start:  
  6. # Required-Stop:  
  7. # Default-Start:        2 3 4 5  
  8. # Default-Stop:         0 1 6  
  9. # Short-Description: mongodb  
  10. # Description: mongo db server  
  11. ### END INIT INFO  
  12.    
  13. . /lib/lsb/init-functions  
  14.    
  15. PROGRAM=/usr/mongodb/bin/mongod  
  16. MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`  
  17.    
  18. test -x $PROGRAM || exit 0  
  19.    
  20. case "$1" in  
  21.   start)  
  22.      ulimit -n 3000  
  23.      log_begin_msg "Starting MongoDB server"   
  24.      $PROGRAM --fork --quiet -journal -maxConns=100 -rest --logpath /data/db/journal/mongdb.log  
  25.      log_end_msg 0  
  26.      ;;  
  27.   stop)  
  28.      log_begin_msg "Stopping MongoDB server"   
  29.      if [ ! -z "$MONGOPID" ]; then   
  30.         kill -15 $MONGOPID  
  31.      fi  
  32.      log_end_msg 0  
  33.      ;;  
  34.   status)  
  35.      ;;  
  36.   *)  
  37.      log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"   
  38.      exit 1  
  39. esac  
  40.    
  41. exit 0  

最后看一下install.sh

  1. #!/bin/bash   
  2.   
  3. source ../common/tool.sh  
  4.   
  5. copyFolder ./mongodb-linux-x86_64-2.2.0 /usr/mongodb-linux-x86_64-2.2.0  
  6. createLink "./mongodb-linux-x86_64-2.2.0" "/usr/mongodb"  
  7. createFolder "/data/db/journal"  
  8. chmod -R 777 /data/db/  
  9. copyFile mongodb $PWD /etc/init.d  
  10. chmod +x /etc/init.d/mongodb  
  11. update-rc.d mongodb defaults  
  12. service mongodb start  

看一下目前经常用到的common/tool.sh脚本的全貌:

  1. #!/bin/bash  
  2.   
  3. function removeFolder {  
  4.   
  5.     if [ -d "$1" ]  
  6.     then  
  7.     echo "$2 folder exists already, remove it..."  
  8.     rm -rf $1  
  9.     else  
  10.     echo "$2 folder doesn't exists"  
  11.     fi  
  12. }  
  13.   
  14. #$1 src folder  
  15. #$2 dst folder  
  16. function copyFolder {  
  17.   
  18.     if [ -d "$2" ]  
  19.     then  
  20.     echo "$2 folder exists already, remove it..."  
  21.     rm -rf $2  
  22.     else  
  23.     echo "$2 folder doesn't exists, start copying..."  
  24.     fi  
  25.     cp -r $1 $2  
  26. }  
  27.   
  28.   
  29. #remove the folder if exists already  
  30. #$1 folder path  
  31. function createFolder {  
  32.   
  33.     if [ -d "$1" ]  
  34.     then  
  35.     echo "$1 folder exists already, remove it..."  
  36.     rm -rf $1  
  37.     else  
  38.     echo "$1 folder doesn't exists, create it..."  
  39.     fi  
  40.     mkdir -p $1  
  41. }  
  42.   
  43. #remove the link if exists already  
  44. #create a link($2) to file($1)  
  45. function createLink {  
  46.   
  47.     if [ -L "$2" ]  
  48.     then  
  49.     echo "$2 link exists already, removing it..."  
  50.     rm $2  
  51.     else  
  52.     echo "$2 link doesn't exists, creating it..."  
  53.     fi  
  54.     echo "creating link: $2 to $1"  
  55.     ln -s $1 $2  
  56. }  
  57.   
  58. #$1 source file name  
  59. #$2 source folder  
  60. #$3 destion folder  
  61. #remove the file if exists already  
  62. #create a file  
  63. function copyFile {  
  64.     if [ -f "$3/$1" ]  
  65.     then  
  66.     echo "$3/$1 exists already, removing it..."  
  67.     rm $3/$1  
  68.     else  
  69.     echo "$3/$1 doesn't exists, copying it..."  
  70.     fi  
  71.     cp $2/$1 $3  
  72. }  
  73.   
  74.   
  75.   
  76.   
  77. # $1 variable name  
  78. # $2 expected value  
  79. # put this into /etc/environment if not found  
  80. function setEnv {  
  81.     source /etc/environment  
  82.     if [ "${!1}" = "$2" ]  
  83.     then  
  84.     echo "$1 is correct: $2"  
  85.     else  
  86.     echo "$1 is wrong: ${!1} != $2"  
  87.   
  88.     h=`grep "$1=\"$2\"" /etc/environment`  
  89.     if [ -n "$h" ]  
  90.     then  
  91.         echo "/etc/environment has $1 already"  
  92.     else  
  93.         echo "Adding $1 into /etc/environment..."  
  94.         echo "$1=\"$2\"" >> /etc/environment  
  95.     fi  
  96.     source /etc/environment  
  97.     fi  
  98. }  
  99.   
  100. #$1 means the full name of dpkg  
  101. #return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)  
  102. #otherwise return 0  
  103. function hasDpkg {  
  104.     r=`dpkg -l | grep "$1"`  
  105.     if [ -n "$r" ]  
  106.     then  
  107.     h=`dpkg -l | grep "ii  $1"`  
  108.     if [ -n "$h" ]  
  109.     then  
  110.         return 1  
  111.     else  
  112.         return 0  
  113.     fi  
  114.     else  
  115.     return 0  
  116.     fi  
  117. }  
  118.   
  119. #$1 search string  
  120. #$2 file path  
  121. #return 1 if found  
  122. #return 0 if not found  
  123. function findStringInFile {  
  124.     h=`grep "$1" $2`  
  125.     echo "h: $h"  
  126.     if [ -n "$h" ]  
  127.     then  
  128.     return 1  
  129.     else  
  130.     return 0  
  131.     fi  
  132. }  
  133.   
  134.   
  135. #$1 dpkg name  
  136. function installDpkg {  
  137.     hasDpkg $1  
  138.     r=$?  
  139.       
  140.     if [ $r -eq 1 ]  
  141.     then  
  142.     echo "$1 was installed"  
  143.     else  
  144.     echo "$1 was not installed, installing..."  
  145.     apt-get install $1  
  146.     fi  
  147. }  
  148.   
  149. #$1 user name  
  150. #return 1 if exists  
  151. #return 0 if doesn't exist  
  152. function haSUSEr {  
  153.     h=`grep "$1" /etc/passwd`  
  154.     echo "h: $h"  
  155.     if [ -n "$h" ]  
  156.     then  
  157.     return 1  
  158.     else  
  159.     return 0  
  160.     fi  
  161. }  
  162.   
  163. #$1 user group name  
  164. #return 1 if exists  
  165. #return 0 if doesn't exist  
  166. function hasUserGroup {  
  167.     h=`grep "$1" /etc/group`  
  168.     echo "h: $h"  
  169.     if [ -n "$h" ]  
  170.     then  
  171.     return 1  
  172.     else  
  173.     return 0  
  174.     fi  
  175. }  
  176.   
  177. #remove user and home folder  
  178. #then create then again  
  179. function recreateSystemUserAndFolder {  
  180.     hasUser $1  
  181.     r=$?  
  182.       
  183.     if [ $r -eq 1 ]  
  184.     then  
  185.     echo "$1 exits already,remove it..."  
  186.     userdel -r $1  
  187.     else  
  188.     echo "$1 doesn't exist,create it..."  
  189.     fi  
  190.     adduser --home /home/$1 --system --shell /bin/bash $1    
  191. }  
  192.   
  193. #remove user group   
  194. #then create it again  
  195. function recreateUserGroup {  
  196.     hasUserGroup $1  
  197.     r=$?  
  198.     if [ $r -eq 1 ]  
  199.     then  
  200.     echo "$1 exists already, remove it..."  
  201.     delgroup $1  
  202.     else  
  203.     echo "$1 doesn't exist, create it..."  
  204.     fi  
  205.     groupadd $1  
  206. }  

相关内容

    暂无相关文章