一年十二月  誰主春秋
關(guān)注:基礎(chǔ)系統(tǒng)工程 密碼學(xué) 人工智能
C++博客
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-161 評論-223 文章-30 trackbacks-0
Shell應(yīng)用(7):nginx升級與恢復(fù)
腳本概述
nginx是一款著名的開源web服務(wù)器,為方便升級與恢復(fù),編寫了一個(gè)簡單的腳本,因?yàn)樯墏浞萘丝蓤?zhí)行文件和配置文件(后綴名為old),所以可用于恢復(fù)。當(dāng)升級時(shí),若nginx正在運(yùn)行,則不中斷服務(wù)進(jìn)行平滑升級,否則直接拷貝覆蓋;當(dāng)恢復(fù)時(shí),若nginx正在運(yùn)行,則不中斷服務(wù)進(jìn)行平滑恢復(fù),否則直接拷貝覆蓋。是否正在運(yùn)行根據(jù)pid來判斷,而pid從pid文件讀取,pid文件則從conf文件提取(默認(rèn)為/usr/local/nginx/logs/nginx.pid)。對于參數(shù)指定的conf文件,會分析它是否存在http {和server {行來檢查有效性。該腳本的用法如下:
● 第1參數(shù)必須為upgrade或restore,分別表示升級或恢復(fù)。
● 第2參數(shù)是可選的,為nginx可執(zhí)行文件,默認(rèn)為/usr/local/nginx/sbin/nginx。
● 第3參數(shù)是可選的,為nginx配置文件,默認(rèn)為/usr/local/nginx/conf/nginx.conf。
腳本實(shí)現(xiàn)
在循環(huán)讀取配置文件每一行時(shí),首先要忽略空白行和注釋行,對應(yīng)正則式分別為^$、^[[:blank:]]*#;然后識別http {或server {行,對應(yīng)正則式分別為^[[:blank:]]*http[[:blank:]]*{[[:blank:]]*、^[[:blank:]]*server[[:blank:]]*{[[:blank:]]*。不管恢復(fù)還是升級,當(dāng)替換nginx可執(zhí)行文件后,如果nginx正在運(yùn)行(一定要使用mv替換才能成功),先發(fā)送USR2信號(通知nginx創(chuàng)建新的工作進(jìn)程)并等待老的pid文件出現(xiàn),再發(fā)送QUIT使老的nginx工作進(jìn)程退出。
1
#
!
/
bin
/
bash
2
# nginx admin script
3
4
. extfuncs
5
6
usage()
7
{
8
echo
"
Usage: $(basename
"
$
0
"
) upgrade|restore [executable file] [configure file]
"
9
exit
1
10
}
11
12
if
[ $#
-
lt
1
]; then
13
usage
14
elif [
"
$1
"
!=
"
upgrade
"
-
a
"
$1
"
!=
"
restore
"
]; then
15
echo
"
The first parameter must be upgrade or restore
"
16
exit
1
17
fi
18
19
do_restore
=
no
20
[
"
$1
"
=
"
restore
"
]
&&
do_restore
=
yes
21
22
bin_file
=
$
{
2
:
-/
usr
/
local
/
nginx
/
sbin
/
nginx}
23
!
check_file_exist
"
$bin_file
"
&&
usage
24
25
if
[
!
-
x
"
$bin_file
"
]; then
26
echo
"
$bin_file: Permission denied
"
27
exit
1
28
fi
29
30
conf_file
=
$
{
3
:
-/
usr
/
local
/
nginx
/
conf
/
nginx.conf}
31
!
check_file_exist
"
$conf_file
"
&&
usage
32
33
re_0
=
"
[[:blank:]]
"
34
re_1
=
"
$re_0*
"
35
re_2
=
"
^$re_1
"
36
re_3
=
"
$re_0+.+
"
37
re_4
=
"
$re_2#
"
38
re_http
=
"
${re_2}http${re_1}{${re_1}
"
39
re_server
=
"
${re_2}server${re_1}{${re_1}
"
40
re_pid
=
"
${re_2}pid$re_3
"
41
42
has_http
=
43
has_server
=
44
pid_file
=
45
46
while
read line
47
do
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
56
done
<
"
$conf_file
"
57
58
if
[
"
x$has_http
"
!=
"
xyes
"
-
o
"
x$has_server
"
!=
"
xyes
"
]; then
59
echo
"
$conf_file is not valid nginx configure file
"
60
exit
1
61
fi
62
63
if
[
-
z
"
$pid_file
"
]; then
64
pid_file
=/
usr
/
local
/
nginx
/
logs
/
nginx.pid
65
elif
[
"
${pid_file,0,1}
"
!=
"
/
"
]; then
66
pid_file
=/
usr
/
local
/
nginx
/
$pid_file
67
fi
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
73
pid
=
$(get_pid
"
$pid_file
"
)
74
check_pid $pid
75
ret
=
$
?
76
77
if
[
"
$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),
"
85
else
86
echo
-
n
"
nginx is not run,
"
87
fi
88
89
if
[
"
x$do_restore
"
=
"
xno
"
]; then
90
echo
"
upgrading it
"
91
else
92
echo
"
restoring it
"
93
fi
94
95
if
[
"
x$do_restore
"
=
"
xno
"
]; then
96
cp
-
f nginx $bin_file
97
else
98
mv
"
${bin_file}.old
"
"
$bin_file
"
99
mv
"
${conf_file}.old
"
"
$conf_file
"
100
fi
101
102
$bin_file
-
t
-
v
-
c
"
$conf_file
"
103
104
if
[
"
$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
"
109
fi
110
111
if
[
"
x$do_restore
"
=
"
xno
"
]; then
112
echo
"
upgrade nginx finished
"
113
else
114
echo
"
restore nginx finished
"
115
fi
腳本示例
升級前
:nginx正在運(yùn)行中,由于此時(shí)還沒升級,所以沒有old備份文件,如下圖
升級后
:運(yùn)行./ngxadmin upgrade后,如下圖
從上可得,sbin和conf子目錄下分別多出了一個(gè)nginx.old和nginx.conf.old。
恢復(fù)后
:運(yùn)行./ngxadmin restore后,如下圖
從上可得,sbin子目錄下沒有了nginx.old,conf子目錄下沒有了nginx.conf.old,nginx可執(zhí)行文件和配置文件均已恢復(fù)為升級前的版本。
posted on 2015-01-19 00:36
春秋十二月
閱讀(2056)
評論(0)
編輯
收藏
引用
所屬分類:
System
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
Windows異常分發(fā)與子系統(tǒng)圖表集 -- 摘自Windows內(nèi)核原理與實(shí)現(xiàn)
淺談Linux共享庫庫函數(shù)掛鉤檢測
kretprobe探究思考
基于Rust構(gòu)建WebAssembly
基于VSS可傳輸卷影拷貝的備份架構(gòu)
Shell(11): 創(chuàng)建和刪除so庫軟鏈接
關(guān)于make依賴文件的自動(dòng)生成
Shell應(yīng)用(10):支持開源庫編譯的Makefile
Shell應(yīng)用(9):自動(dòng)化批量編譯
一種攔截Linux動(dòng)態(tài)庫API的方法及裝置
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
本博客所有隨筆均為原創(chuàng),因?yàn)椴欢ㄆ诰S護(hù)更新,所以轉(zhuǎn)載請注明出處,如有問題和建議,請留言或評論,發(fā)表您的寶貴意見,藉此平臺以分享交流、共同進(jìn)步。
聯(lián)系方式:微信math-engineer
<
2016年5月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(75)
給我留言
查看公開留言
查看私人留言
隨筆分類
(160)
Algorithm(48)
C/C++(24)
Compiler(25)
Compute Theory(5)
Database(4)
Network(17)
Opensrc(13)
System(24)
隨筆檔案
(161)
2025年6月 (2)
2025年4月 (2)
2024年12月 (1)
2024年11月 (1)
2024年9月 (1)
2024年8月 (2)
2024年6月 (1)
2024年5月 (1)
2024年4月 (1)
2024年3月 (2)
2024年2月 (2)
2023年12月 (1)
2023年11月 (2)
2023年10月 (2)
2023年9月 (37)
2021年12月 (1)
2021年10月 (1)
2021年9月 (1)
2021年2月 (1)
2020年5月 (3)
2020年4月 (1)
2019年11月 (4)
2019年7月 (1)
2018年11月 (1)
2017年12月 (1)
2016年12月 (1)
2016年11月 (2)
2016年10月 (1)
2016年9月 (1)
2016年8月 (3)
2016年7月 (4)
2016年5月 (1)
2015年10月 (2)
2015年9月 (1)
2015年6月 (2)
2015年5月 (3)
2015年2月 (1)
2015年1月 (1)
2014年12月 (2)
2014年4月 (2)
2014年3月 (1)
2014年1月 (1)
2013年10月 (1)
2013年9月 (1)
2013年8月 (3)
2013年5月 (1)
2013年3月 (1)
2012年11月 (1)
2012年9月 (3)
2012年8月 (1)
2012年7月 (1)
2012年6月 (5)
2012年5月 (3)
2011年12月 (5)
2011年11月 (1)
2011年10月 (5)
2011年8月 (7)
2011年7月 (6)
2011年6月 (6)
2010年6月 (1)
2009年12月 (1)
2009年8月 (1)
2009年7月 (1)
2009年6月 (1)
2009年4月 (3)
文章分類
(30)
詩詞作品集(30)
關(guān)注的開源項(xiàng)目
LLVM
編譯系統(tǒng)
nginx
高性能Web服務(wù)器
OpenSSL
密碼學(xué)庫
suricata
網(wǎng)絡(luò)IPS引擎
最新隨筆
1.?一個(gè)歐拉數(shù)整除問題的兩種證法
2.?有限域上的特征與指數(shù)和之?dāng)U展
3.?二元二次型的相似變換、正定性與正交分解
4.?關(guān)于群的一些結(jié)論及應(yīng)用
5.?不定方程的代數(shù)數(shù)論解法
6.?關(guān)于橢圓曲線的驗(yàn)證計(jì)算
7.?不可約多項(xiàng)式判別算法的改正
8.?論證有限域上平方根的求解
9.?求解離散對數(shù)問題的Terr算法
10.?簡單私鑰加密構(gòu)造的驗(yàn)證及安全性分析
積分與排名
積分 - 417363
排名 - 56
最新評論
1.?re: 一種攔截Linux原始套接字IO的方法[未登錄]
很有前途和很有錢途啊。
--chipset
2.?re: 一種攔截Linux原始套接字IO的方法[未登錄]
@chipset
是的
--春秋十二月
3.?re: 一種攔截Linux原始套接字IO的方法[未登錄]
工作是做網(wǎng)絡(luò)安全?
--chipset
4.?re: 一種使用函數(shù)指針實(shí)現(xiàn)狀態(tài)機(jī)的方法
函數(shù)指針實(shí)現(xiàn)狀態(tài)機(jī)
--linda
5.?re: 多標(biāo)簽視圖類CTabView的設(shè)計(jì)實(shí)現(xiàn)
為啥代碼缺少一些呢,給新手個(gè)完整點(diǎn)的啊
--pekingliu
6.?re: 工作線程與消息循環(huán)
從消息隊(duì)列取出消息 mark了
--mmocake
7.?re: 一種簡單的跨平臺套接字管道
評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
--IT搬運(yùn)工
8.?re: 一種簡單的跨平臺套接字管道
windows僅支持af_init和af_init6地址族有錯(cuò)別字么?
af_init和af_init6
--IT搬運(yùn)工
9.?re: Shell應(yīng)用(8):使用awk定位反匯編輸出[未登錄]
厲害
--Chipset
10.?re: TCP分組丟失時(shí)的狀態(tài)變遷
不錯(cuò)
--Binky
閱讀排行榜
1.?基于OpenSSL實(shí)現(xiàn)的安全連接(14007)
2.?字符串16進(jìn)制顯示(12887)
3.?基于boost asio實(shí)現(xiàn)的ssl socket框架(12345)
4.?Linux套接字與虛擬文件系統(tǒng)(1):初始化和創(chuàng)建(8680)
5.?關(guān)于數(shù)據(jù)庫的一些學(xué)習(xí)研究心得(8116)
6.?使用CString GetBuffer自適應(yīng)獲取計(jì)算機(jī)名稱(7985)
7.?使用正則表達(dá)式解析URL(7952)
8.?basic_string內(nèi)存泄露問題之分析解決(7756)
9.?Shell應(yīng)用(4): 使用sed刪除行尾的^M字符(7678)
10.?nginx iocp(1):tcp異步連接(7661)
評論排行榜
1.?basic_string內(nèi)存泄露問題之分析解決(19)
2.?求單向鏈表倒序第m個(gè)元素(11)
3.?基于順序存儲實(shí)現(xiàn)的多叉樹(1):深度優(yōu)先存儲(9)
4.?字符大小寫轉(zhuǎn)換(7)
5.?字符串16進(jìn)制顯示(6)
6.?面向?qū)ο箧i框架的設(shè)計(jì)與實(shí)現(xiàn)(6)
7.?Shell應(yīng)用(4): 使用sed刪除行尾的^M字符(5)
8.?使用正則表達(dá)式解析URL(5)
9.?工作線程與消息循環(huán)(5)
10.?十進(jìn)制整數(shù)千位分隔符(4)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 春秋十二月
青青国产成人久久91网
|
97超级碰碰碰久久久久
|
性做久久久久久免费观看
|
久久夜色精品国产亚洲av
|
日韩av无码久久精品免费
|
国产成人久久精品激情
|
久久久久国产一区二区
|
亚洲伊人久久综合影院
|
狠狠狠色丁香婷婷综合久久五月
|
亚洲精品99久久久久中文字幕
|
精品一二三区久久aaa片
|
久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡
|
久久丫精品国产亚洲av不卡
|
97久久精品人人做人人爽
|
人妻无码精品久久亚瑟影视
|
国产精品久久永久免费
|
久久99这里只有精品国产
|
99久久精品费精品国产
|
久久亚洲精品人成综合网
|
久久婷婷五月综合成人D啪
|
99久久无色码中文字幕
|
久久精品一区二区三区AV
|
久久亚洲综合色一区二区三区
|
久久久久久久91精品免费观看
|
精品久久人人妻人人做精品
|
99精品久久精品一区二区
|
色综合合久久天天给综看
|
国产激情久久久久影院
|
韩国免费A级毛片久久
|
午夜欧美精品久久久久久久
|
久久无码中文字幕东京热
|
综合网日日天干夜夜久久
|
久久精品无码专区免费
|
免费国产99久久久香蕉
|
.精品久久久麻豆国产精品
|
色欲综合久久中文字幕网
|
少妇人妻综合久久中文字幕
|
久久只这里是精品66
|
国产69精品久久久久久人妻精品
|
精品无码久久久久久国产
|
久久久久无码专区亚洲av
|