Shell應用(7):nginx升級與恢復
阿新 • • 發佈:2018-12-22
指令碼概述
nginx是一款著名的開源web伺服器,為方便升級與恢復,編寫了一個簡單的指令碼,因為升級備份了可執行檔案和配置檔案(字尾名為old),所以可用於恢復。當升級時,若nginx正在執行,則不中斷服務進行平滑升級,否則直接拷貝覆蓋;當恢復時,若nginx正在執行,則不中斷服務進行平滑恢復,否則直接拷貝覆蓋。是否正在執行根據pid來判斷,而pid從pid檔案讀取,pid檔案則從conf檔案提取(預設為/usr/local/nginx/logs/nginx.pid)。對於引數指定的conf檔案,會分析它是否存在http {和server {行來檢查有效性。該指令碼的用法如下:
● 第1引數必須為upgrade或restore,分別表示升級或恢復。
● 第2引數是可選的,為nginx可執行檔案,預設為/usr/local/nginx/sbin/nginx。
● 第3引數是可選的,為nginx配置檔案,預設為/usr/local/nginx/conf/nginx.conf。
指令碼實現
在迴圈讀取配置檔案每一行時,首先要忽略空白行和註釋行,對應正則式分別為^$、^[[:blank:]]*#;然後識別http {或server {行,對應正則式分別為^[[:blank:]]*http[[:blank:]]*{[[:blank:]]*、^[[:blank:]]*server[[:blank:]]*{[[:blank:]]*。不管恢復還是升級,當替換nginx可執行檔案後,如果nginx正在執行(一定要使用mv替換才能成功),先發送USR2訊號(通知nginx建立新的工作程序)並等待老的pid檔案出現,再發送QUIT使老的nginx工作程序退出。 1#!/bin/bash
2# nginx admin script
3
4. extfuncs
5
6usage()
7{
8 echo "Usage: $(basename "$0") upgrade|restore [executable file] [configure file]" 9 exit 1 10} 11
12if [ $# -lt 1 ]; then
13 usage
14elif [ "$1"!="upgrade"-a "$1"!="restore" ]; then
15 echo "The first parameter must be upgrade or restore " 16 exit 1 17fi
18
19do_restore=no
20[ "$1"="restore" ] && do_restore=yes
21
22bin_file=${2:-/usr/local/nginx/sbin/nginx} 23! check_file_exist "$bin_file"&& usage
24
25if [ !-x "$bin_file" ]; then
26 echo "$bin_file: Permission denied" 27 exit 1 28fi
29
30conf_file=${3:-/usr/local/nginx/conf/nginx.conf} 31! check_file_exist "$conf_file"&& usage
32
33re_0="[[:blank:]]" 34re_1="$re_0*" 35re_2="^$re_1" 36re_3="$re_0+.+" 37re_4="$re_2#" 38re_http="${re_2}http${re_1}{${re_1}" 39re_server="${re_2}server${re_1}{${re_1}" 40re_pid="${re_2}pid$re_3" 41
42has_http= 43has_server= 44pid_file= 45
46while read line
47do 48 if (echo $line | grep "^$">/dev/null) || (echo $line | grep "$re_4">/dev/null); then
49 continue 50 elif (echo $line | grep "$re_http">/dev/null); then
51 has_http=yes
52 elif (echo $line | grep "$re_server">/dev/null); then
53 has_server=yes
54 test -n "$pid_file"|| pid_file=`echo $line | awk '{if($0~/'"$re_pid"'/) print substr($2,1,index($2,";")-1)}'`
55 test -n "$pid_file"&&break 56done <"$conf_file" 57
58if [ "x$has_http"!="xyes"-o "x$has_server"!="xyes" ]; then
59 echo "$conf_file is not valid nginx configure file" 60 exit 1 61fi
62
63if [ -z "$pid_file" ]; then
64 pid_file=/usr/local/nginx/logs/nginx.pid
65elif [ "${pid_file,0,1}"!="/" ]; then
66 pid_file=/usr/local/nginx/$pid_file
67fi
68
69[ "x$do_restore"="xno" ] &&! check_file_exist nginx && exit 1 70[ "x$do_restore"="xyes" ] &&! check_file_exist "${bin_file}.old"&& exit 1 71[ "x$do_restore"="xyes" ] &&! check_file_exist "${conf_file}.old"&& exit 1 72
73pid=$(get_pid "$pid_file")
74check_pid $pid
75ret=$? 76
77if [ "$ret"-eq "0" ]; then
78 if [ "x$do_restore"="xno" ]; then
79 mv "$bin_file""${bin_file}.old" 80 cp "$conf_file""${conf_file}.old" 81 else 82 mv "$bin_file""${bin_file}.tmp" 83 fi
84 echo -n "nginx is running($pid)," 85else 86 echo -n "nginx is not run," 87fi
88
89if [ "x$do_restore"="xno" ]; then
90 echo "upgrading it" 91else 92 echo "restoring it" 93fi
94
95if [ "x$do_restore"="xno" ]; then
96 cp -f nginx $bin_file
97else 98 mv "${bin_file}.old""$bin_file" 99 mv "${conf_file}.old""$conf_file"100fi
101
102$bin_file -t -v -c "$conf_file"103
104if [ "$ret"-eq "0" ]; then
105 kill -USR2 $pid
106 wait_file "${pid_file}.oldbin"107 kill -QUIT `cat "${pid_file}.oldbin"`
108 [ "x$do_restore"="xyes" ] && rm -f "${bin_file}.tmp"109fi
110
111if [ "x$do_restore"="xno" ]; then
112 echo "upgrade nginx finished"113else114 echo "restore nginx finished"115fi
指令碼示例
升級前:nginx正在執行中,由於此時還沒升級,所以沒有old備份檔案,如下圖
升級後:執行./ngxadmin upgrade後,如下圖
從上可得,sbin和conf子目錄下分別多出了一個nginx.old和nginx.conf.old。
恢復後:執行./ngxadmin restore後,如下圖
從上可得,sbin子目錄下沒有了nginx.old,conf子目錄下沒有了nginx.conf.old,nginx可執行檔案和配置檔案均已恢復為升級前的版本。 posted on 2015-01-19 00:36 春秋十二月 閱讀(1512) 評論(0) 編輯 收藏 引用 所屬分類: System
nginx是一款著名的開源web伺服器,為方便升級與恢復,編寫了一個簡單的指令碼,因為升級備份了可執行檔案和配置檔案(字尾名為old),所以可用於恢復。當升級時,若nginx正在執行,則不中斷服務進行平滑升級,否則直接拷貝覆蓋;當恢復時,若nginx正在執行,則不中斷服務進行平滑恢復,否則直接拷貝覆蓋。是否正在執行根據pid來判斷,而pid從pid檔案讀取,pid檔案則從conf檔案提取(預設為/usr/local/nginx/logs/nginx.pid)。對於引數指定的conf檔案,會分析它是否存在http {和server {行來檢查有效性。該指令碼的用法如下:
● 第1引數必須為upgrade或restore,分別表示升級或恢復。
● 第2引數是可選的,為nginx可執行檔案,預設為/usr/local/nginx/sbin/nginx。
● 第3引數是可選的,為nginx配置檔案,預設為/usr/local/nginx/conf/nginx.conf。
指令碼實現
在迴圈讀取配置檔案每一行時,首先要忽略空白行和註釋行,對應正則式分別為^$、^[[:blank:]]*#;然後識別http {或server {行,對應正則式分別為^[[:blank:]]*http[[:blank:]]*{[[:blank:]]*、^[[:blank:]]*server[[:blank:]]*{[[:blank:]]*。不管恢復還是升級,當替換nginx可執行檔案後,如果nginx正在執行(一定要使用mv替換才能成功),先發送USR2訊號(通知nginx建立新的工作程序)並等待老的pid檔案出現,再發送QUIT使老的nginx工作程序退出。 1#!/bin/bash
3
4. extfuncs
5
6usage()
7{
8 echo "Usage: $(basename "$0") upgrade|restore [executable file] [configure file]" 9 exit 1 10} 11
12if [ $# -lt 1 ]; then
13 usage
14elif [ "$1"!="upgrade"-a "$1"!="restore" ]; then
15 echo "The first parameter must be upgrade or restore
18
19do_restore=no
20[ "$1"="restore" ] && do_restore=yes
21
22bin_file=${2:-/usr/local/nginx/sbin/nginx} 23! check_file_exist "$bin_file"&& usage
24
25if [ !-x "$bin_file" ]; then
26 echo "$bin_file: Permission denied" 27 exit 1 28fi
29
30conf_file=${3:-/usr/local/nginx/conf/nginx.conf} 31! check_file_exist "$conf_file"&& usage
32
33re_0="[[:blank:]]" 34re_1="$re_0*" 35re_2="^$re_1" 36re_3="$re_0+.+" 37re_4="$re_2#" 38re_http="${re_2}http${re_1}{${re_1}" 39re_server="${re_2}server${re_1}{${re_1}" 40re_pid="${re_2}pid$re_3" 41
42has_http= 43has_server= 44pid_file= 45
46while read line
47do 48 if (echo $line | grep "^$">/dev/null) || (echo $line | grep "$re_4">/dev/null); then
49 continue 50 elif (echo $line | grep "$re_http">/dev/null); then
51 has_http=yes
52 elif (echo $line | grep "$re_server">/dev/null); then
53 has_server=yes
54 test -n "$pid_file"|| pid_file=`echo $line | awk '{if($0~/'"$re_pid"'/) print substr($2,1,index($2,";")-1)}'`
55 test -n "$pid_file"&&break 56done <"$conf_file" 57
58if [ "x$has_http"!="xyes"-o "x$has_server"!="xyes" ]; then
59 echo "$conf_file is not valid nginx configure file" 60 exit 1 61fi
62
63if [ -z "$pid_file" ]; then
64 pid_file=/usr/local/nginx/logs/nginx.pid
65elif [ "${pid_file,0,1}"!="/" ]; then
66 pid_file=/usr/local/nginx/$pid_file
67fi
68
69[ "x$do_restore"="xno" ] &&! check_file_exist nginx && exit 1 70[ "x$do_restore"="xyes" ] &&! check_file_exist "${bin_file}.old"&& exit 1 71[ "x$do_restore"="xyes" ] &&! check_file_exist "${conf_file}.old"&& exit 1 72
73pid=$(get_pid "$pid_file")
74check_pid $pid
75ret=$? 76
77if [ "$ret"-eq "0" ]; then
78 if [ "x$do_restore"="xno" ]; then
79 mv "$bin_file""${bin_file}.old" 80 cp "$conf_file""${conf_file}.old" 81 else 82 mv "$bin_file""${bin_file}.tmp" 83 fi
84 echo -n "nginx is running($pid)," 85else 86 echo -n "nginx is not run," 87fi
88
89if [ "x$do_restore"="xno" ]; then
90 echo "upgrading it" 91else 92 echo "restoring it" 93fi
94
95if [ "x$do_restore"="xno" ]; then
96 cp -f nginx $bin_file
97else 98 mv "${bin_file}.old""$bin_file" 99 mv "${conf_file}.old""$conf_file"100fi
101
102$bin_file -t -v -c "$conf_file"103
104if [ "$ret"-eq "0" ]; then
105 kill -USR2 $pid
106 wait_file "${pid_file}.oldbin"107 kill -QUIT `cat "${pid_file}.oldbin"`
108 [ "x$do_restore"="xyes" ] && rm -f "${bin_file}.tmp"109fi
110
111if [ "x$do_restore"="xno" ]; then
112 echo "upgrade nginx finished"113else114 echo "restore nginx finished"115fi
指令碼示例
升級前:nginx正在執行中,由於此時還沒升級,所以沒有old備份檔案,如下圖
升級後:執行./ngxadmin upgrade後,如下圖
從上可得,sbin和conf子目錄下分別多出了一個nginx.old和nginx.conf.old。
恢復後:執行./ngxadmin restore後,如下圖
從上可得,sbin子目錄下沒有了nginx.old,conf子目錄下沒有了nginx.conf.old,nginx可執行檔案和配置檔案均已恢復為升級前的版本。 posted on 2015-01-19 00:36 春秋十二月 閱讀(1512) 評論(0) 編輯 收藏 引用 所屬分類: System