bash 指令碼編程二十一 MongoDB自動部署

來源:互聯網
上載者:User

這是單機版本的自動部署,手動部署可以參考我的另一篇文章:

http://blog.csdn.net/sheismylife/article/details/6737127

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

然後準備啟動指令碼mongodb:

#!/bin/sh ### BEGIN INIT INFO# Provides:     mongodb# Required-Start:# Required-Stop:# Default-Start:        2 3 4 5# Default-Stop:         0 1 6# Short-Description: mongodb# Description: mongo db server### END INIT INFO . /lib/lsb/init-functions PROGRAM=/usr/mongodb/bin/mongodMONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'` test -x $PROGRAM || exit 0 case "$1" in  start)     ulimit -n 3000     log_begin_msg "Starting MongoDB server"      $PROGRAM --fork --quiet -journal -maxConns=100 -rest --logpath /data/db/journal/mongdb.log     log_end_msg 0     ;;  stop)     log_begin_msg "Stopping MongoDB server"      if [ ! -z "$MONGOPID" ]; then         kill -15 $MONGOPID     fi     log_end_msg 0     ;;  status)     ;;  *)     log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"      exit 1esac exit 0

最後看一下install.sh

#!/bin/bash source ../common/tool.shcopyFolder ./mongodb-linux-x86_64-2.2.0 /usr/mongodb-linux-x86_64-2.2.0createLink "./mongodb-linux-x86_64-2.2.0" "/usr/mongodb"createFolder "/data/db/journal"chmod -R 777 /data/db/copyFile mongodb $PWD /etc/init.dchmod +x /etc/init.d/mongodbupdate-rc.d mongodb defaultsservice mongodb start

看一下目前經常用到的common/tool.sh指令碼的全貌:

#!/bin/bashfunction removeFolder {    if [ -d "$1" ]    thenecho "$2 folder exists already, remove it..."rm -rf $1    elseecho "$2 folder doesn't exists"    fi}#$1 src folder#$2 dst folderfunction copyFolder {    if [ -d "$2" ]    thenecho "$2 folder exists already, remove it..."rm -rf $2    elseecho "$2 folder doesn't exists, start copying..."    fi    cp -r $1 $2}#remove the folder if exists already#$1 folder pathfunction createFolder {    if [ -d "$1" ]    thenecho "$1 folder exists already, remove it..."rm -rf $1    elseecho "$1 folder doesn't exists, create it..."    fi    mkdir -p $1}#remove the link if exists already#create a link($2) to file($1)function createLink {    if [ -L "$2" ]    thenecho "$2 link exists already, removing it..."rm $2    elseecho "$2 link doesn't exists, creating it..."    fi    echo "creating link: $2 to $1"    ln -s $1 $2}#$1 source file name#$2 source folder#$3 destion folder#remove the file if exists already#create a filefunction copyFile {    if [ -f "$3/$1" ]    thenecho "$3/$1 exists already, removing it..."rm $3/$1    elseecho "$3/$1 doesn't exists, copying it..."    fi    cp $2/$1 $3}# $1 variable name# $2 expected value# put this into /etc/environment if not foundfunction setEnv {    source /etc/environment    if [ "${!1}" = "$2" ]    thenecho "$1 is correct: $2"    elseecho "$1 is wrong: ${!1} != $2"h=`grep "$1=\"$2\"" /etc/environment`if [ -n "$h" ]then    echo "/etc/environment has $1 already"else    echo "Adding $1 into /etc/environment..."    echo "$1=\"$2\"" >> /etc/environmentfisource /etc/environment    fi}#$1 means the full name of dpkg#return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)#otherwise return 0function hasDpkg {    r=`dpkg -l | grep "$1"`    if [ -n "$r" ]    thenh=`dpkg -l | grep "ii  $1"`if [ -n "$h" ]then    return 1else    return 0fi    elsereturn 0    fi}#$1 search string#$2 file path#return 1 if found#return 0 if not foundfunction findStringInFile {    h=`grep "$1" $2`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#$1 dpkg namefunction installDpkg {    hasDpkg $1    r=$?        if [ $r -eq 1 ]    thenecho "$1 was installed"    elseecho "$1 was not installed, installing..."apt-get install $1    fi}#$1 user name#return 1 if exists#return 0 if doesn't existfunction hasUser {    h=`grep "$1" /etc/passwd`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#$1 user group name#return 1 if exists#return 0 if doesn't existfunction hasUserGroup {    h=`grep "$1" /etc/group`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#remove user and home folder#then create then againfunction recreateSystemUserAndFolder {    hasUser $1    r=$?        if [ $r -eq 1 ]    thenecho "$1 exits already,remove it..."userdel -r $1    elseecho "$1 doesn't exist,create it..."    fi    adduser --home /home/$1 --system --shell /bin/bash $1  }#remove user group #then create it againfunction recreateUserGroup {    hasUserGroup $1    r=$?    if [ $r -eq 1 ]    thenecho "$1 exists already, remove it..."delgroup $1    elseecho "$1 doesn't exist, create it..."    fi    groupadd $1}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.