Smart backup and restoration of configuration files-PHP Tutorial

Source: Internet
Author: User
Smart backup and restoration of configuration files. Intelligent backup and restoration of configuration files: each time we upgrade or update services for many of the same machines, we need to back up one or more lines in the configuration file, or smart backup and restoration of configuration files
Use cases:
Every time we upgrade or update services for many machines, we need to back up one or more rows in the configuration file, or one or more data blocks, after upgrading and updating services, you can re-write the corresponding backup data back to the new configuration file. operations on a large number of servers will waste a lot of time and effort, the following script is used to accomplish similar tasks.
Main functions:
1. backup a specified row or multiple rows, one block or multiple blocks
2. back up a specified file
3. restore all partial backups and all backups
4. Insert a regular expression matching in the configuration file]
5. precisely locate and delete the configuration file
6. insert configuration files in batches after similar rows
Service configuration file example:
 
 
  1. Server. config:
  2. #### Configuration file example:
  3. Ser_max_connection = 6000 # row
  4. Ser_min_connection = 10

  5. Ser_time_out = 60
  6. Ser_time_spent = 120

  7. Server tcp_nodes {# Block

  8. Ser_client_ip = ipv4
  9. Ser_client_len = 20 * N
  10. Ser_client_syn = yes
  11. Ser_client_ack = yes
  12. }
The following is the code implementation of the shell script. the script mainly uses the sed tool to complete the configuration editing function. This script is changed to a general version based on the actual application environment, and has not been strictly tested. if you want to use this script, perform a serious test based on your environment. If you have a better idea, please join the QQ group: 63565867.

 
 
  1. #! /Bin/bash
  2. # DianDian
  3. Proconf =/usr/local/server/etc # main directory to be backed up. the file name passed in the parameter and the path will be merged.
  4. Confpath = $ proconf
  5. Bkpath =/usr/local/src/bkconfig/part # directory for saving part of the backup file
  6. Bkpath_whole =/usr/local/src/bkconfig/whole # back up the Directory of the entire file
  7. Handle_date = $ (date "+ % y % m % d % H % M ")
  8. Mkdir-p $ bkpath $ bkpath_whole
  9. FJ = '^ \ +'
  10. FD = '^ \ ='
  11. ALLFILE = ""

  12. Function multidir () {# dirname # a recursive function in shell is used to obtain all files in a directory. [if the file name contains spaces, errors may occur]

  13. Local dirs = $1
  14. Local diri = 0
  15. If [! -D $ dirs]; then
  16. Echo "$ dirs is not a directory ."
  17. Exit 1
  18. Fi
  19. Local lsfile =$ (ls-d $ dirs/* 2>/dev/null)
  20. Local dir_list = ($ lsfile)
  21. For (diri = 0; diri <$ {# dir_list [@]}; diri ++ )){
  22. If [-d $ {dir_list [$ diri]}]; then
  23. Multidir $ {dir_list [$ diri]}
  24. Else
  25. ALLFILE = "$ ALLFILE $ {dir_list [$ diri]}"
  26. Fi
  27. }
  28. }

  29. If ["X $1" = "Xbackup"]; then
  30. Num = 0
  31. If ["X $2 "! = "X"]; then
  32. OLD_IFS = "$ IFS"
  33. IFS = "#"
  34. Field = ($2)
  35. IFS = "$ OLD_IFS"
  36. Else
  37. Echo-e "\ 033 [31 m Error \ 033 [0 m"
  38. Exit 1
  39. Fi
  40. SFS = "+"
  41. F_CONF = "$ confpath/$ {field [0]}"
  42. B _CONF = "$ bkpath/$ {field [0]}"
  43. B _CDIR = $ (dirname $ B _CONF)
  44. If [-e "$ B _CONF"-a-s "$ B _CONF"]; then
  45. Echo-e "\ 033 [31 m Error $ B _CONF exist and no empty. \ 033 [0 m"
  46. Exit 1
  47. Fi
  48. If [! -D $ B _CDIR]; then
  49. Mkdir-p $ B _CDIR
  50. Fi
  51. If [-f "$ F_CONF"]; then
  52. Echo "backup @ $ {field [0]}"
  53. While read line
  54. Do
  55. (Num ++ ))
  56. For (I = 1; I <$ {# field [@]}; I ++ )){
  57. If [[$ {field [$ I]} = ~ $ FJ]; then
  58. ST =$ {field [$ I] # +}
  59. If [[$ line = ~ ^ $ ST]; then
  60. Sed-n "$ num, //}/p" $ F_CONF | sed '1s/^/& +/'> $ B _CONF
  61. Fi
  62. Elif [[$ {field [$ I]} = ~ $ FD]; then
  63. ST =$ {field [$ I] #=}
  64. STLine =$ {line % = *}
  65. If [["$ STLine" = "$ ST"]; then
  66. Sed-n "$ {num} p" $ F_CONF | sed '1s/^/& =/'> $ B _CONF
  67. Fi
  68. Else
  69. If [[$ line = ~ ^ $ {Field [$ I]}]; then
  70. Sed-n "$ {num} p" $ F_CONF | sed '1s/^/&-/'> $ B _CONF
  71. Fi
  72. Fi
  73. }
  74. Done <$ F_CONF
  75. Else
  76. Echo-e "\ 033 [31 m Error: Skip $ {field [0]} \ 033 [0 m"
  77. Fi
  78. Elif ["X $1" = "Xrestore"]; then
  79. If ["X $2 "! = "X"]; then
  80. List = ('ls-f $ bkpath/$2>/dev/Null ')
  81. Else
  82. # List = ('ls-d $ bkpath/* 2>/dev/Null ')
  83. Multidir $ bkpath
  84. List = ($ ALLFILE)
  85. Fi
  86. If ["$ List" = ""]; then
  87. Echo-e "\ 033 [31 m restore: no valid file. \ 033 [0 m"
  88. Fi
  89. For I in $ {List [*]}
  90. Do
  91. Num = 0
  92. B _CONF = "$ I"
  93. Base = $ (basename $ I)
  94. If ["$ base" = ""-a-s $ B _CONF]; then
  95. Echo-e "\ 033 [31 m restore error @ $ B _CONF \ 033 [0 m"
  96. Continue
  97. Fi
  98. F_CONF = $ (echo $ I | sed-n "s # $ bkpath # $ confpath # p ")
  99. # F_CONF = "$ confpath/$ base"
  100. Echo-n "@ $ F_CONF"
  101. If [! -F $ F_CONF]; then
  102. Echo-e "\ 033 [31 m restore error @ $ F_CONF \ 033 [0 m"
  103. Continue
  104. Fi
  105. While read bconf
  106. Do
  107. (Num ++ ))
  108. TT = ""
  109. Str = ""
  110. If [[$ bconf = ~ $ FJ]; then
  111. Str = $ (sed-n-e "$ {num }, //}/p "$ B _CONF | sed '1s/^ + // '| awk' {S = S" \ n "$0 ;} END {sub (/^ .. /, "", S); printf ("% s", S );}')
  112. TT = "+"
  113. Elif [[$ bconf = ~ $ FD]; then
  114. Str = $ (sed-n-e "$ {num} p" $ B _CONF | sed '1s/^ = // '| awk' {S = S "\ n" $0 ;} END {sub (/^ .. /, "", S); printf ("% s", S );}')
  115. TT = "="
  116. Elif [[$ bconf = ~ ^-]; Then
  117. Str = $ (sed-n-e "$ {num} p" $ B _CONF | sed '1s/^-// '| awk' {S = S "\ n" $0 ;} END {sub (/^ .. /, "", S); printf ("% s", S );}')
  118. TT = "-"
  119. Else
  120. Continue
  121. Fi
  122. First = $ (echo-e $ Str | sed-n 1 p)
  123. Seek = 0
  124. While read fconf
  125. Do
  126. (Seek ++ ))
  127. Tmp_fconf =$ {fconf % = *}
  128. Tmp_first =$ {First % = *}
  129. If ["$ tmp_fconf" = "$ tmp_first"]; then
  130. If ["$ TT" = "+"]; then
  131. Sed-I "$ {seek}, //}/d" $ F_CONF
  132. If [$ seek-ne 1]; then
  133. Sed-I "$ [$ {seek}-1] a $ Str" $ F_CONF
  134. Else
  135. Sed-I "$ {seek} a $ Str" $ F_CONF
  136. Fi
  137. Echo-n "$ TT"
  138. TT = ""
  139. Break
  140. Elif ["$ TT" = "="]; then
  141. Sed-I "$ {seek} s/. */$ Str/" $ F_CONF
  142. Echo-n "$ TT"
  143. TT = ""
  144. Break
  145. Elif ["$ TT" = "-"]; then
  146. Sed-I "$ {seek} s/. */$ Str/" $ F_CONF
  147. Echo-n "$ TT"
  148. TT = ""
  149. Break
  150. Fi
  151. Fi
  152. Done <$ F_CONF
  153. If ["$ TT "! = ""]; Then
  154. Err = $ Err "$ First \ n"
  155. Fi
  156. Done <$ B _CONF
  157. Echo
  158. Done
  159. If ["$ Err "! = ""]; Then
  160. Echo-e "\ n \ 033 [33 mError: $ Err \ 033 [0 m"
  161. Err = ""
  162. Fi
  163. Elif ["X $1" = "Xinsert"]; then
  164. Num = 0
  165. Snum = 0
  166. N = 1
  167. Nn = 0
  168. OLD_IFS = "$ IFS"
  169. IFS = "#"
  170. If ["X $2 "! = "X"]; then
  171. Insert = ($2)
  172. Else
  173. Echo-e "\ 033 [31 m Insert Error \ 033 [0 m"
  174. Exit 1
  175. Fi
  176. IFS = "$ OLD_IFS"
  177. If [-f "$ confpath/$ {insert [0]}"]; then
  178. While read olc
  179. Do
  180. (Num ++ ))
  181. Tmp_olc = $ (echo "$ olc" | sed's // g ')
  182. Tmp_olc =$ {tmp_olc % = *}
  183. Tmp_insert = $ (echo "$ {insert [$ n]}" | sed's // g ')
  184. Tmp_insert =$ {tmp_insert % = *}
  185. If [["$ tmp_olc" ==$ tmp_insert & $ n-le $ {# insert [@]}]; then
  186. (N ++ ))
  187. If [$ n-eq $ [$ {# insert [@]}-1]; then
  188. Echo "Insert :$ {insert [$ n] }@$ {insert [0]} [$ num]"
  189. Snum = $ num
  190. Nn = $ n
  191. Elif [$ n-eq $ {# insert [@]}]; then
  192. Echo-e "\ 033 [33 m $ {insert [$ nn]} exits. \ 033 [0 m"
  193. Exit 1
  194. Fi
  195. Else
  196. If [[$ num-eq $ [$ snum + 2] & $ nn-eq $ n]; then
  197. Break
  198. Fi
  199. Fi
  200. Done <$ confpath/$ {insert [0]}
  201. If [$ nn-ne $ [$ {# insert [@]}-1]; then
  202. Echo-e "\ 033 [31 m Insert Error: None $ {insert [$ nn]} \ 033 [0 m $ [$ nn + 1]"
  203. Exit 1
  204. Fi
  205. Str = "$ {insert [$ nn]}"
  206. Sed-I "$ {snum} a \ $ Str" $ confpath/$ {insert [0]}
  207. Else
  208. Echo-e "\ 033 [31 m Insert Error: File not exist $ confpath/$ {insert [0]} \ 033 [0 m"
  209. Fi
  210. Elif ["X $1" = "Xdelete"]; then
  211. Num = 0
  212. N = 1
  213. Del = 0
  214. OLD_IFS = "$ IFS"
  215. IFS = "#"
  216. If ["X $2 "! = "X"]; then
  217. Delete = ($2)
  218. Else
  219. Echo-e "\ 033 [31 m Delete Error \ 033 [0 m"
  220. Exit 1
  221. Fi
  222. IFS = "$ OLD_IFS"
  223. If [-f "$ confpath/$ {delete [0]}"]; then
  224. While read olc
  225. Do
  226. (Num ++ ))
  227. Tmp_olc = $ (echo "$ olc" | sed's // g ')
  228. Tmp_olc =$ {tmp_olc % = *}
  229. Tmp_delete = $ (echo "$ {delete [$ n]}" | sed's // g ')
  230. Tmp_delete =$ {tmp_delete % = *}
  231. # Echo "$ tmp_olc"
  232. If [[$ tmp_olc ==$ tmp_delete]; then
  233. (N ++ ))
  234. If [$ n-eq $ [$ {# delete [@]}]; then
  235. Echo "Delete :$ {delete [$ n-1] }@$ {delete [0]} [$ num]"
  236. Del = 1
  237. Break
  238. Fi
  239. Fi
  240. Done <$ confpath/$ {delete [0]}
  241. If [$ n-ne $ [$ {# delete [@]}]; then
  242. Echo-e "\ 033 [33mCan't find: \" $ {delete [$ n]} \ "@ $ [$ n + 1] \ 033 [0 m"
  243. Exit 1
  244. Fi
  245. Sed-I "$ {num} d" $ confpath/$ {delete [0]}
  246. Else
  247. Echo-e "\ 033 [31 m Delete Error: File not exist $ confpath/$ {delete [0]} \ 033 [0 m"
  248. Fi
  249. Elif ["X $1" = "Xinsall"]; then
  250. OLD_IFS = "$ IFS"
  251. IFS = "#"
  252. If ["X $2 "! = "X"]; then
  253. Insert = ($2)
  254. Else
  255. Echo-e "\ 033 [31 m insall Error \ 033 [0 m"
  256. Exit 1
  257. Fi
  258. IFS = "$ OLD_IFS"
  259. If [-f "$ confpath/$ {insert [0]}"]; then
  260. Sed-I "/$ {insert [1]}/a \\\ {insert [2]}" $ confpath/$ {insert [0]}
  261. Fi
  262. Elif ["X $1" = "Xcopy"]; then
  263. If ["X $2 "! = "X"]; then
  264. OLD_IFS = "$ IFS"
  265. IFS = "#"
  266. Field = ($2)
  267. IFS = "$ OLD_IFS"
  268. Else
  269. Echo-e "\ 033 [31 m Copy Error \ 033 [0 m"
  270. Exit 1
  271. Fi
  272. For (I = 0; I <$ {# field [@]}; I ++ )){
  273. Deep =2 (dirname $ {field [$ I]} 2>/dev/null)
  274. Copied_dir = $ confpath/$ Deep
  275. Copied_file = $ confpath/$ {field [$ I]}
  276. Bk_dir = $ bkpath_whole/$ Deep
  277. Bk_file = $ bkpath_whole/$ {field [$ I]}
  278. If [! -D "$ copied_dir"-o! -F "$ copied_file"]; then
  279. Echo-e "\ 033 [31 m copy Error @ $ {field [$ I]} \ 033 [0 m"
  280. Exit 1
  281. Fi
  282. If [-e "$ bk_file"-a-s "$ bk_file"]; then
  283. Echo-e "\ 033 [31 m Error $ bk_file exist and no empty. \ 033 [0 m"
  284. Exit 1
  285. Fi
  286. Mkdir-p $ bkpath_whole/$ Deep &/bin/cp-f $ copied_file $ bk_file
  287. Check = $ (diff $ bk_file $ copied_file)
  288. If ["$ check" = ""]; then
  289. Echo "copy $ copied_file => $ bk_file"
  290. Else
  291. Echo "error copy @ $ {field [$ I]}"
  292. Fi
  293. }
  294. Elif ["X $1" = "Xrcopy"]; then
  295. If ["X $2 "! = "X"]; then
  296. OLD_IFS = "$ IFS"
  297. IFS = "#"
  298. Field = ($2)
  299. IFS = "$ OLD_IFS"
  300. Else
  301. Echo-e "\ 033 [31 m rcopy Error \ 033 [0 m"
  302. Exit 1
  303. Fi
  304. For (I = 0; I <$ {# field [@]}; I ++ )){
  305. If ["$ {field [$ I]}" = ""]; then
  306. Continue
  307. Fi
  308. Deep =$ (dirname $ {field [$ I]}>/dev/null)
  309. Rcopied_dir = $ confpath/$ Deep
  310. Rcopied_file = $ confpath/$ {field [$ I]}
  311. Bk_dir = $ bkpath_whole/$ Deep
  312. Bk_file = $ bkpath_whole/$ {field [$ I]}
  313. If [! -D "$ bk_dir"-o! -F "$ bk_file"]; then
  314. Echo-e "\ 033 [31 m rcopy error $ bk_dir not dir or $ bk_file not file. \ 033 [0 m"
  315. Exit 1
  316. Fi
  317. If [! -S "$ bk_file"]; then
  318. Echo-e "\ 033 [31 m rcopy error $ bk_file exist but empty. \ 033 [0 m"
  319. Exit 1
  320. Fi
  321. If [! -D "$ rcopied_dir"]; then
  322. Echo-e "\ 033 [31 m rcopy error: $ rcopied_dir not dir. \ 033 [0 m"
  323. Exit 1
  324. Fi
  325. /Bin/cp-f $ rcopied_file. $ handle_date | (echo-e "\ 033 [31 mrcopy: backup $ rcopied_file failed. \ 033 [0 m "& exit 1)
  326. /Bin/cp-f $ bk_file $ rcopied_file | (echo-e "\ 033 [31 mrcopy: rcopy: restore $ rcopied_file failed. \ 033 [0 m "& rm-rf $ rcopied_file. $ handle_date & exit 1)
  327. Check = $ (diff $ bk_file $ rcopied_file)
  328. If ["$ check" = ""]; then
  329. Echo "restore $ bk_file => $ rcopied_file"
  330. Rm-rf $ rcopied_file. $ handle_date
  331. Else
  332. Echo "error rcopy @ $ {field [$ I]}"
  333. /Bin/cp-f $ rcopied_file. $ handle_date $ rcopied_file
  334. Rm-rf $ rcopied_file. $ handle_date
  335. Fi
  336. }
  337. Elif ["X $1" = "Xversion"]; then
  338. Echo "Version: 1.0.7"
  339. Else
  340. Echo-e "batch backup restores the configuration files under a given directory. you can back up one or more lines, one block, or multiple blocks in a file. During restoration, you can directly find the corresponding row or block restoration. "
  341. Echo-e "when inserting a field, it may be accurate to a specific line"
  342. Echo-e "there are two file formats to be backed up :"
  343. Echo-e "for example :"
  344. Echo-e "pattern_hot_switch = 0 # row"
  345. Echo-e "define server_proxy_host1 {# Block, block With} as the end symbol"
  346. Echo-e "part1 = no1"
  347. Echo-e "part2 = no2"
  348. Echo-e "part3 = no3"
  349. Echo-e "}"
  350. Echo-e "Help :"
  351. Echo-e "Backup Dir :"
  352. Echo-e "\ tServer Conf Dir: $ confpath"
  353. Echo-e "\ tPartBKP Conf Dir: $ bkpath"
  354. Echo-e "\ tWholeBKP Conf Dir: $ bkpath_whole"
  355. Echo "Usage: $0 [backup | restore | insert | delete | insall | copy | rcopy | version]"
  356. Echo "backup: backup restore: restore insert: insert delete: delete insall: Batch insert copy: copy file rcopy: restore copied file"
  357. Echo-e "\ tbackup 'server. config # + srcpattern # = request_src_type #... # + src src_acl #'"
  358. Echo-e "\ tbackup 'main. config # p_src_switch # url_log_switch # = url_log_switch #'"
  359. Echo-e "\ trestore"
  360. Echo-e "\ trestore main. config"
  361. Echo-e "\ tinsert 'server. config # def p_r t_default #... # r_ww_switch # xxx_xxx_xxx = 1-2-3 -'"
  362. Echo-e "\ tdelete 'server. config # def p_r t_default #... # r_ww_switch # xxx_xxx_xxx '"
  363. Echo-e "\ tinsall 'server. config # def p_r t_default # xxx_xxx_xxx = 1-2-3 -'"
  364. Echo-e "\ tcopy 'main. config # r. config #....'"
  365. Echo-e "\ trcopy 'main. config # r. config #....'"
  366. Fi

Application scenario: whenever we install the same machine or update services for many, we need to back up one or more lines in the configuration file, or...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.