Configuration file Intelligence Backup and restore _php tutorial

Source: Internet
Author: User
Tags bulk insert

Configure file intelligence for backup and restore


Application Scenarios:
Whenever we install the same machine upgrade or update service for many, we want to back up one or more lines in the configuration file, or a data block or multiple data blocks, and the service upgrade, update, and then the corresponding backup data back to the new configuration file, in the high-volume server operation, Will waste a lot of time and energy, the following script is used to accomplish similar things.
The main functions are:
1. Back up a specified row or lines, one block or multiple blocks
2. Back up the specified individual files
3. Restore all partial backups and all backups
4. configuration file precise positioning insert "regular match"
5. configuration file Precise location Delete
6. configuration file similar to line after bulk insert
Sample configuration file for the service:
 
  
  
  1. Server.config:
  2. # # # #配置文件样例:
  3. ser_max_connection=6000 #行
  4. ser_min_connection=10

  5. Ser_time_out=60
  6. ser_time_spent=120

  7. Server tcp_nodes{ # #块

  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, which mainly uses the SED tool to complete the configuration editing function. This script is changed to a generic version according to the real application environment, and has not been rigorously tested, if you want to use this script, please do serious testing according to your own environment. If you have a better idea, please join QQ Group: 63565867.

 
 
  1. #!/bin/bash
  2. #DianDian
  3. Proconf=/usr/local/server/etc #要备份的主目录, the name of the parameter passed in and the path merged together
  4. Confpath= $proconf
  5. Bkpath=/usr/local/src/bkconfig/part #备份文件中部分内容的保存目录
  6. Bkpath_whole=/usr/local/src/bkconfig/whole #备份整个文件的保存目录
  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 # #shell里面的一个递归函数 to get all the files in a directory "error may occur if the file name includes a space"

  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 [@]};d iri++) {
  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= ($)
  35. ifs= "$OLD _ifs"
  36. Else
  37. Echo-e "\033[31m Error \033[0m"
  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[31m Error $B _conf exist and no empty. \033[0m "
  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[31m error:skip ${field[0]} \033[0m"
  77. Fi
  78. elif ["x$1" = = "Xrestore"];then
  79. If ["x$2"! = "X"];then
  80. List= (' ls-f $bkpath/$2 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[31m restore:no valid file. \033[0m "
  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[31m restore error @ $B _conf \033[0m"
  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[31m restore error @ $F _conf \033[0m"
  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 "$;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
  112. tt= "+"
  113. elif [[$bconf =~ $FD]];then
  114. str=$ (Sed-n-E "${num}p" $B _conf | sed ' 1s/^=//' | awk ' {s=s ' \\n ' $;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
  115. tt= "="
  116. elif [[$bconf =~ ^-]];then
  117. str=$ (Sed-n-E "${num}p" $B _conf | sed ' 1s/^-//' | awk ' {s=s ' \\n ' $;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
  118. Tt= "-"
  119. Else
  120. Continue
  121. Fi
  122. first=$ (echo-e $Str | sed-n 1p)
  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[33merror: $Err \033[0m"
  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= ($)
  172. Else
  173. Echo-e "\033[31m Insert Error \033[0m"
  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[33m${insert[$nn]} exits.\033[0m"
  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[31m Insert error:none ${insert[$nn]} \033[0m $[$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[31m Insert error:file not exist $confpath/${insert[0]} \033[0m"
  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= ($)
  218. Else
  219. Echo-e "\033[31m Delete Error \033[0m"
  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[0m"
  243. Exit 1
  244. Fi
  245. Sed-i "${num}d" $confpath/${delete[0]}
  246. Else
  247. Echo-e "\033[31m Delete error:file not exist $confpath/${delete[0]} \033[0m"
  248. Fi
  249. elif ["x$1" = = "Xinsall"];then
  250. Old_ifs= "$IFS"
  251. ifs= "#"
  252. If ["x$2"! = "X"];then
  253. Insert= ($)
  254. Else
  255. Echo-e "\033[31m insall Error \033[0m"
  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= ($)
  267. ifs= "$OLD _ifs"
  268. Else
  269. Echo-e "\033[31m Copy Error \033[0m"
  270. Exit 1
  271. Fi
  272. For ((i=0;i<${#field [@]};i++)) {
  273. deep=$ (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[31m copy Error @ ${field[$i]}\033[0m"
  280. Exit 1
  281. Fi
  282. If [-e] $bk _file "-a-s" $BK _file "];then
  283. Echo-e "\033[31m Error $bk _file exist and no empty. \033[0m "
  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= ($)
  299. ifs= "$OLD _ifs"
  300. Else
  301. Echo-e "\033[31m rcopy Error \033[0m"
  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[31m rcopy error $bk _dir not dir or $bk _file not file.\033[0m"
  315. Exit 1
  316. Fi
  317. If [!-S "$BK _file"];then
  318. ECHO-E "\033[31m rcopy error $BK _file exist but empty. \033[0m "
  319. Exit 1
  320. Fi
  321. if [!-d "$rcopied _dir"];then
  322. ECHO-E "\033[31m rcopy error: $rcopied _dir not dir. \033[0m"
  323. Exit 1
  324. Fi
  325. /bin/cp-f $rcopied _file $rcopied _file $handle _date | | (echo-e "\033[31mrcopy:backup $rcopied _file failed.\033[0m" && exit 1)
  326. /bin/cp-f $bk _file $rcopied _file | | (echo-e "\033[31mrcopy:rcopy:restore $rcopied _file failed.\033[0m" && rm-rf $rcopied _file. $handle _date && Amp 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 "Bulk Backup restores a configuration file in a given directory, you can back up one or more lines, one block, or multiple blocks in a file. When recovering, you can directly find the corresponding row or block restore. "
  341. Echo-e "When inserting a field, it may be accurate to a specific line"
  342. Echo-e "There are two types of file formats to be backed up:"
  343. Echo-e "such as:"
  344. Echo-e "Pattern_hot_switch=0 #行"
  345. Echo-e "Define server_proxy_host1{#块, block with} as 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: $ [backup|restore|insert|delete|insall|copy|rcopy|version]"
  356. echo "Backup: Back up restore: Restore insert: Insert Delete: Delete insall: Bulk Insert Copy: Copy file rcopy: Restore copied Files"
  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

http://www.bkjia.com/PHPjc/1108028.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108028.html techarticle configuration file Intelligent Backup and restore application scenario: Whenever we install the same machine upgrade or update service for many, we want 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.