歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> bash 腳本編程二十一 MongoDB自動部署

bash 腳本編程二十一 MongoDB自動部署

日期:2017/3/1 10:09:15   编辑:Linux編程

這是單機版本的MongoDB自動部署,手動部署可以參考我的另一篇文章:http://www.linuxidc.com/Linux/2012-09/71132.htm

首先下載mongodb-linux-x86_64-2.2.0.tgz, 解壓後放到工程目錄mongodb下。

然後准備啟動腳本mongodb:

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

最後看一下install.sh

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

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

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