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:
- Server.config:
- # # # #配置文件样例:
- ser_max_connection=6000 #行
- ser_min_connection=10
- Ser_time_out=60
- ser_time_spent=120
- Server tcp_nodes{ # #块
- Ser_client_ip=ipv4
- Ser_client_len=20*n
- Ser_client_syn=yes
- Ser_client_ack=yes
- }
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.
- #!/bin/bash
- #DianDian
- Proconf=/usr/local/server/etc #要备份的主目录, the name of the parameter passed in and the path merged together
- Confpath= $proconf
- Bkpath=/usr/local/src/bkconfig/part #备份文件中部分内容的保存目录
- Bkpath_whole=/usr/local/src/bkconfig/whole #备份整个文件的保存目录
- handle_date=$ (date "+%y%m%d%h%m")
- Mkdir-p $bkpath $bkpath _whole
- fj= ' ^\+ '
- Fd= ' ^\= '
- Allfile= ""
- function Multidir () {#dirname # #shell里面的一个递归函数 to get all the files in a directory "error may occur if the file name includes a space"
- Local dirs=$1
- Local diri=0
- if [!-D $dirs];then
- echo "$dirs is not a directory."
- Exit 1
- Fi
- Local lsfile=$ (ls-d $dirs/* 2>/dev/null)
- Local dir_list= ($lsfile)
- For ((diri=0;diri<${#dir_list [@]};d iri++) {
- If [-D ${dir_list[$diri]}];then
- Multidir ${dir_list[$diri]}
- Else
- allfile= "$ALLFILE ${dir_list[$diri]}"
- Fi
- }
- }
- If ["x$1" = = "Xbackup"];then
- Num=0
- If ["x$2"! = "X"];then
- Old_ifs= "$IFS"
- ifs= "#"
- Field= ($)
- ifs= "$OLD _ifs"
- Else
- Echo-e "\033[31m Error \033[0m"
- Exit 1
- Fi
- Sfs= "+"
- F_conf= "$confpath/${field[0]}"
- B_conf= "$bkpath/${field[0]}"
- b_cdir=$ (dirname $B _conf)
- If [-e] $B _conf "-a-s" $B _conf "];then
- Echo-e "\033[31m Error $B _conf exist and no empty. \033[0m "
- Exit 1
- Fi
- if [!-D $B _cdir];then
- Mkdir-p $B _cdir
- Fi
- If [-F ' $F _conf "];then
- echo "Backup @${field[0]}"
- While Read line
- Do
- ((num++))
- For ((i=1;i<${#field [@]};i++)) {
- if [[${field[$i]} =~ $FJ]];then
- st=${field[$i]#+}
- if [[$line =~ ^ $ST]];then
- Sed-n "$num,/}/p" $F _conf | Sed ' 1s/^/&+/' >> $B _conf
- Fi
- elif [[${field[$i]} =~ $FD]];then
- st=${field[$i]#=}
- stline=${line%%=*}
- if [["$STLine" = = "$ST"]];then
- Sed-n "${num}p" $F _conf | Sed ' 1s/^/&=/' >> $B _conf
- Fi
- Else
- if [[$line =~ ^${field[$i]}]];then
- Sed-n "${num}p" $F _conf | Sed ' 1s/^/&-/' >> $B _conf
- Fi
- Fi
- }
- Done < $F _conf
- Else
- Echo-e "\033[31m error:skip ${field[0]} \033[0m"
- Fi
- elif ["x$1" = = "Xrestore"];then
- If ["x$2"! = "X"];then
- List= (' ls-f $bkpath/$2 2>/dev/null ')
- Else
- #List = (' ls-d $bkpath/* 2>/dev/null ')
- Multidir $bkpath
- List= ($ALLFILE)
- Fi
- If ["$List" = = ""];then
- ECHO-E "\033[31m restore:no valid file. \033[0m "
- Fi
- For i in ${list[*]}
- Do
- Num=0
- b_conf= "$i"
- base=$ (basename $i)
- If ["$base" = = ""-a-s $B _conf];then
- ECHO-E "\033[31m restore error @ $B _conf \033[0m"
- Continue
- Fi
- f_conf=$ (echo $i | sed-n "s# $bkpath # $confpath #p")
- #F_CONF = "$confpath/$base"
- Echo-n "@ $F _conf"
- if [!-F $F _conf];then
- ECHO-E "\033[31m restore error @ $F _conf \033[0m"
- Continue
- Fi
- While Read bconf
- Do
- ((num++))
- Tt= ""
- Str= ""
- if [[$bconf =~ $FJ]];then
- str=$ (Sed-n-E "${num},/}/p" $B _conf | Sed ' 1s/^+//' | awk ' {s=s ' \\n "$;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
- tt= "+"
- elif [[$bconf =~ $FD]];then
- str=$ (Sed-n-E "${num}p" $B _conf | sed ' 1s/^=//' | awk ' {s=s ' \\n ' $;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
- tt= "="
- elif [[$bconf =~ ^-]];then
- str=$ (Sed-n-E "${num}p" $B _conf | sed ' 1s/^-//' | awk ' {s=s ' \\n ' $;} End{sub (/^. /, "", s);p rintf ("%s", s);} ')
- Tt= "-"
- Else
- Continue
- Fi
- first=$ (echo-e $Str | sed-n 1p)
- Seek=0
- While Read fconf
- Do
- ((seek++))
- tmp_fconf=${fconf%%=*}
- tmp_first=${first%%=*}
- If ["$tmp _fconf" = = "$tmp _first"];then
- If ["$TT" = = "+"];then
- Sed-i "${seek},/}/d" $F _conf
- If [$seek-ne 1];then
- Sed-i "$[${seek}-1]a$str" $F _conf
- Else
- Sed-i "${seek}a$str" $F _conf
- Fi
- Echo-n "$TT"
- Tt= ""
- Break
- elif ["$TT" = = "="];then
- Sed-i "${seek}s/.*/$Str/" $F _conf
- Echo-n "$TT"
- Tt= ""
- Break
- elif ["$TT" = = "-"];then
- Sed-i "${seek}s/.*/$Str/" $F _conf
- Echo-n "$TT"
- Tt= ""
- Break
- Fi
- Fi
- Done < $F _conf
- If ["$TT"! = ""];then
- err= $Err "$First \ n"
- Fi
- Done < $B _conf
- Echo
- Done
- If ["$ERR"! = ""];then
- Echo-e "\n\033[33merror: $Err \033[0m"
- Err= ""
- Fi
- elif ["x$1" = = "Xinsert"];then
- Num=0
- Snum=0
- N=1
- Nn=0
- Old_ifs= "$IFS"
- ifs= "#"
- If ["x$2"! = "X"];then
- Insert= ($)
- Else
- Echo-e "\033[31m Insert Error \033[0m"
- Exit 1
- Fi
- ifs= "$OLD _ifs"
- If [-F ' $confpath/${insert[0]} "];then
- While read OLC
- Do
- ((num++))
- Tmp_olc=$ (echo "$olc" | sed ' s///g ')
- tmp_olc=${tmp_olc%%=*}
- Tmp_insert=$ (echo "${insert[$n]}" | sed ' s///g ')
- tmp_insert=${tmp_insert%%=*}
- if [["$tmp _olc" = = $tmp _insert && $n-le ${#insert [@]}]];then
- ((n++))
- If [$n-eq $[${#insert [@]}-1]];then
- echo "Insert: ${insert[$n]} @ ${insert[0]}[$num]"
- snum= $num
- nn= $n
- elif [$n-eq ${#insert [@]}];then
- Echo-e "\033[33m${insert[$nn]} exits.\033[0m"
- Exit 1
- Fi
- Else
- if [[$num-eq $[$snum + 2] && $nn-eq $n]];then
- Break
- Fi
- Fi
- Done < $confpath/${insert[0]}
- If [$nn-ne $[${#insert [@]}-1]];then
- Echo-e "\033[31m Insert error:none ${insert[$nn]} \033[0m $[$nn + 1]"
- Exit 1
- Fi
- Str= "${insert[$nn]}"
- Sed-i "${snum}a\\ $Str" $confpath/${insert[0]}
- Else
- Echo-e "\033[31m Insert error:file not exist $confpath/${insert[0]} \033[0m"
- Fi
- elif ["x$1" = = "Xdelete"];then
- Num=0
- N=1
- Del=0
- Old_ifs= "$IFS"
- ifs= "#"
- If ["x$2"! = "X"];then
- Delete= ($)
- Else
- Echo-e "\033[31m Delete Error \033[0m"
- Exit 1
- Fi
- ifs= "$OLD _ifs"
- If [-F ' $confpath/${delete[0]} "];then
- While read OLC
- Do
- ((num++))
- Tmp_olc=$ (echo "$olc" | sed ' s///g ')
- tmp_olc=${tmp_olc%%=*}
- Tmp_delete=$ (echo "${delete[$n]}" | sed ' s///g ')
- tmp_delete=${tmp_delete%%=*}
- #echo "$tmp _olc"
- if [[$tmp _olc = = $tmp _delete]];then
- ((n++))
- If [$n-eq $[${#delete [@]}]];then
- echo "Delete: ${delete[$n-1]} @ ${delete[0]}[$num]"
- Del=1
- Break
- Fi
- Fi
- Done < $confpath/${delete[0]}
- If [$n-ne $[${#delete [@]}]];then
- Echo-e "\033[33mcan ' t find:\" ${delete[$n]}\ "@ $[$n + 1] \033[0m"
- Exit 1
- Fi
- Sed-i "${num}d" $confpath/${delete[0]}
- Else
- Echo-e "\033[31m Delete error:file not exist $confpath/${delete[0]} \033[0m"
- Fi
- elif ["x$1" = = "Xinsall"];then
- Old_ifs= "$IFS"
- ifs= "#"
- If ["x$2"! = "X"];then
- Insert= ($)
- Else
- Echo-e "\033[31m insall Error \033[0m"
- Exit 1
- Fi
- ifs= "$OLD _ifs"
- If [-F ' $confpath/${insert[0]} "];then
- Sed-i "/${insert[1]}/a\\${insert[2]}" $confpath/${insert[0]}
- Fi
- elif ["x$1" = = "Xcopy"];then
- If ["x$2"! = "X"];then
- Old_ifs= "$IFS"
- ifs= "#"
- Field= ($)
- ifs= "$OLD _ifs"
- Else
- Echo-e "\033[31m Copy Error \033[0m"
- Exit 1
- Fi
- For ((i=0;i<${#field [@]};i++)) {
- deep=$ (dirname ${field[$i]} 2>/dev/null)
- copied_dir= $confpath/$Deep
- copied_file= $confpath/${field[$i]}
- bk_dir= $bkpath _whole/$Deep
- bk_file= $bkpath _whole/${field[$i]}
- if [!-d "$copied _dir"-O!-F "$copied _file"];then
- Echo-e "\033[31m copy Error @ ${field[$i]}\033[0m"
- Exit 1
- Fi
- If [-e] $bk _file "-a-s" $BK _file "];then
- Echo-e "\033[31m Error $bk _file exist and no empty. \033[0m "
- Exit 1
- Fi
- Mkdir-p $bkpath _whole/$Deep &&/bin/cp-f $copied _file $bk _file
- check=$ (diff $bk _file $copied _file)
- If ["$check" = = ""];then
- echo "Copy $copied _file = $BK _file"
- Else
- echo "Error copy @ ${field[$i]}"
- Fi
- }
- elif ["x$1" = = "Xrcopy"];then
- If ["x$2"! = "X"];then
- Old_ifs= "$IFS"
- ifs= "#"
- Field= ($)
- ifs= "$OLD _ifs"
- Else
- Echo-e "\033[31m rcopy Error \033[0m"
- Exit 1
- Fi
- For ((i=0;i<${#field [@]};i++)) {
- If ["${field[$i]}" = = ""];then
- Continue
- Fi
- deep=$ (dirname ${field[$i]} >/dev/null)
- rcopied_dir= $confpath/$Deep
- rcopied_file= $confpath/${field[$i]}
- bk_dir= $bkpath _whole/$Deep
- bk_file= $bkpath _whole/${field[$i]}
- if [!-d "$BK _dir"-O!-F "$BK _file"];then
- ECHO-E "\033[31m rcopy error $bk _dir not dir or $bk _file not file.\033[0m"
- Exit 1
- Fi
- If [!-S "$BK _file"];then
- ECHO-E "\033[31m rcopy error $BK _file exist but empty. \033[0m "
- Exit 1
- Fi
- if [!-d "$rcopied _dir"];then
- ECHO-E "\033[31m rcopy error: $rcopied _dir not dir. \033[0m"
- Exit 1
- Fi
- /bin/cp-f $rcopied _file $rcopied _file $handle _date | | (echo-e "\033[31mrcopy:backup $rcopied _file failed.\033[0m" && exit 1)
- /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)
- check=$ (diff $bk _file $rcopied _file)
- If ["$check" = = ""];then
- echo "restore $BK _file = $rcopied _file"
- RM-RF $rcopied _file. $handle _date
- Else
- echo "Error rcopy @ ${field[$i]}"
- /bin/cp-f $rcopied _file $handle _date $rcopied _file
- RM-RF $rcopied _file. $handle _date
- Fi
- }
- elif ["x$1" = = "Xversion"];then
- echo "version:1.0.7"
- Else
- 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. "
- Echo-e "When inserting a field, it may be accurate to a specific line"
- Echo-e "There are two types of file formats to be backed up:"
- Echo-e "such as:"
- Echo-e "Pattern_hot_switch=0 #行"
- Echo-e "Define server_proxy_host1{#块, block with} as end symbol"
- Echo-e "Part1=no1"
- Echo-e "Part2=no2"
- Echo-e "Part3=no3"
- Echo-e "}"
- Echo-e "Help:"
- Echo-e "Backup Dir:"
- Echo-e "\tserver Conf Dir: $confpath"
- Echo-e "\tpartbkp Conf Dir: $bkpath"
- Echo-e "\twholebkp Conf Dir: $bkpath _whole"
- echo "Usage: $ [backup|restore|insert|delete|insall|copy|rcopy|version]"
- echo "Backup: Back up restore: Restore insert: Insert Delete: Delete insall: Bulk Insert Copy: Copy file rcopy: Restore copied Files"
- Echo-e "\tbackup ' server.config#+srcpattern#=request_src_type#...#+src src_acl# '"
- Echo-e "\tbackup ' main.config#p_src_switch#url_log_switch#=url_log_switch# '"
- Echo-e "\trestore"
- Echo-e "\trestore main.config"
- Echo-e "\tinsert ' server.config#def p_r t_default# ... #r_ww_switch # xxx_xxx_xxx=1-2-3-'"
- Echo-e "\tdelete ' server.config#def p_r t_default# ... #r_ww_switch #xxx_xxx_xxx '"
- Echo-e "\tinsall ' server.config#def p_r t_default#xxx_xxx_xxx=1-2-3-'"
- Echo-e "\tcopy ' main.config#r.config# .... '"
- Echo-e "\trcopy ' main.config#r.config# .... '"
- 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 ...