配置文件智能的备份和还原
配置文件智能的备份和还原
运用场景:每当我们为很多安装同样的机器升级或者更新服务时,要备份配置文件中的某一行或者多行,或者一个数据块或者多个数据块,和服务升级、更新完后,再把对应的备份数据重新写回新的配置文件中;在大批量的服务器中操作,会浪费很多的时间和精力,以下脚本就是用来完成类似的事情。
主要功能有:
1.备份指定的一行或者多行,一个块或者多个块
2.备份指定的单个文件
3.还原所有部分备份和所有备份
4.配置文件精确定位插入【正则匹配】
5.配置文件精确定位删除
6.配置文件类似行后批量插入
服务的配置文件样例:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>server.config:<br /> </li><li>####配置文件样例:<br /></li><li>ser_max_connection=6000 #行<br /></li><li>ser_min_connection=10<br /></li><li><br /></li><li>ser_time_out=60<br /></li><li>ser_time_spent=120<br /></li><li><br /></li><li>server tcp_nodes{ ##块<br /></li><li><br /></li><li>ser_client_ip=ipv4<br /></li><li>ser_client_len=20*N<br /></li><li>ser_client_syn=yes<br /></li><li>ser_client_ack=yes<br /></li><li>}</li></ol>
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#!/bin/bash<br /> </li><li>#DianDian<br /></li><li>proconf=/usr/local/server/etc #要备份的主目录,参数传入的文件名和会这个路径合并起来<br /></li><li>confpath=$proconf<br /></li><li>bkpath=/usr/local/src/bkconfig/part #备份文件中部分内容的保存目录<br /></li><li>bkpath_whole=/usr/local/src/bkconfig/whole #备份整个文件的保存目录<br /></li><li>handle_date=$(date "+%y%m%d%H%M")<br /></li><li>mkdir -p $bkpath $bkpath_whole<br /></li><li>FJ='^\+'<br /></li><li>FD='^\='<br /></li><li>ALLFILE=""<br /></li><li><br /></li><li>function multidir(){ #dirname ##shell里面的一个递归函数,用来获得一个目录下的所有文件【如果文件名中包括空格,可能会出错】<br /></li><li><br /></li><li>local dirs=$1<br /></li><li>local diri=0<br /></li><li>if [ ! -d $dirs ];then<br /></li><li>echo "$dirs is not a directory."<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>local lsfile=$(ls -d $dirs/* 2>/dev/null)<br /></li><li>local dir_list=($lsfile)<br /></li><li>for((diri=0;diri<${#dir_list[@]};diri++)){<br /></li><li>if [ -d ${dir_list[$diri]} ];then<br /></li><li>multidir ${dir_list[$diri]}<br /></li><li>else<br /></li><li>ALLFILE="$ALLFILE ${dir_list[$diri]}"<br /></li><li>fi<br /></li><li>}<br /></li><li>}<br /></li><li><br /></li><li>if [ "X$1" == "Xbackup" ];then<br /></li><li>num=0<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>field=($2)<br /></li><li>IFS="$OLD_IFS"<br /></li><li>else<br /></li><li>echo -e "\033[31m Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>SFS="+"<br /></li><li>F_CONF="$confpath/${field[0]}"<br /></li><li>B_CONF="$bkpath/${field[0]}"<br /></li><li>B_CDIR=$(dirname $B_CONF)<br /></li><li>if [ -e "$B_CONF" -a -s "$B_CONF" ];then<br /></li><li>echo -e "\033[31m Error $B_CONF exist and no empty. \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>if [ ! -d $B_CDIR ];then<br /></li><li>mkdir -p $B_CDIR<br /></li><li>fi<br /></li><li>if [ -f "$F_CONF" ];then<br /></li><li>echo "backup @${field[0]}"<br /></li><li>while read line<br /></li><li>do<br /></li><li>((num++))<br /></li><li>for((i=1;i<${#field[@]};i++)){<br /></li><li>if [[ ${field[$i]} =~ $FJ ]];then<br /></li><li>ST=${field[$i]#+}<br /></li><li>if [[ $line =~ ^$ST ]];then<br /></li><li>sed -n "$num,/}/p" $F_CONF | sed '1s/^/&+/' >> $B_CONF<br /></li><li>fi<br /></li><li>elif [[ ${field[$i]} =~ $FD ]];then<br /></li><li>ST=${field[$i]#=}<br /></li><li>STLine=${line%%=*}<br /></li><li>if [[ "$STLine" == "$ST" ]];then<br /></li><li>sed -n "${num}p" $F_CONF | sed '1s/^/&=/' >> $B_CONF<br /></li><li>fi<br /></li><li>else<br /></li><li>if [[ $line =~ ^${field[$i]} ]];then<br /></li><li>sed -n "${num}p" $F_CONF | sed '1s/^/&-/' >> $B_CONF<br /></li><li>fi<br /></li><li>fi<br /></li><li>}<br /></li><li>done < $F_CONF<br /></li><li>else<br /></li><li>echo -e "\033[31m Error:Skip ${field[0]} \033[0m"<br /></li><li>fi<br /></li><li>elif [ "X$1" == "Xrestore" ];then<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>List=(`ls -f $bkpath/$2 2>/dev/null`)<br /></li><li>else<br /></li><li>#List=(`ls -d $bkpath/* 2>/dev/null`)<br /></li><li>multidir $bkpath<br /></li><li>List=($ALLFILE)<br /></li><li>fi<br /></li><li>if [ "$List" == "" ];then<br /></li><li>echo -e "\033[31m restore:no valid file. \033[0m"<br /></li><li>fi<br /></li><li>for i in ${List[*]}<br /></li><li>do<br /></li><li>num=0<br /></li><li>B_CONF="$i"<br /></li><li>base=$(basename $i)<br /></li><li>if [ "$base" == "" -a -s $B_CONF ];then<br /></li><li>echo -e "\033[31m restore error @ $B_CONF \033[0m"<br /></li><li>continue<br /></li><li>fi<br /></li><li>F_CONF=$(echo $i | sed -n "s#$bkpath#$confpath#p")<br /></li><li>#F_CONF="$confpath/$base"<br /></li><li>echo -n "@ $F_CONF "<br /></li><li>if [ ! -f $F_CONF ];then<br /></li><li>echo -e "\033[31m restore error @ $F_CONF \033[0m"<br /></li><li>continue<br /></li><li>fi<br /></li><li>while read bconf<br /></li><li>do<br /></li><li>((num++))<br /></li><li>TT=""<br /></li><li>Str=""<br /></li><li>if [[ $bconf =~ $FJ ]];then<br /></li><li>Str=$(sed -n -e "${num},/}/p" $B_CONF | sed '1s/^+//' | awk '{S=S"\\n"$0;}END{sub(/^../,"",S);printf("%s",S);}')<br /></li><li>TT="+"<br /></li><li>elif [[ $bconf =~ $FD ]];then<br /></li><li>Str=$(sed -n -e "${num}p" $B_CONF | sed '1s/^=//' | awk '{S=S"\\n"$0;}END{sub(/^../,"",S);printf("%s",S);}')<br /></li><li>TT="="<br /></li><li>elif [[ $bconf =~ ^- ]];then<br /></li><li>Str=$(sed -n -e "${num}p" $B_CONF | sed '1s/^-//' | awk '{S=S"\\n"$0;}END{sub(/^../,"",S);printf("%s",S);}')<br /></li><li>TT="-"<br /></li><li>else<br /></li><li>continue<br /></li><li>fi<br /></li><li>First=$(echo -e $Str | sed -n 1p)<br /></li><li>seek=0<br /></li><li>while read fconf<br /></li><li>do<br /></li><li>((seek++))<br /></li><li>tmp_fconf=${fconf%%=*}<br /></li><li>tmp_first=${First%%=*}<br /></li><li>if [ "$tmp_fconf" == "$tmp_first" ];then<br /></li><li>if [ "$TT" == "+" ];then<br /></li><li>sed -i "${seek},/}/d" $F_CONF<br /></li><li>if [ $seek -ne 1 ];then<br /></li><li>sed -i "$[ ${seek} - 1 ]a$Str" $F_CONF<br /></li><li>else<br /></li><li>sed -i "${seek}a$Str" $F_CONF<br /></li><li>fi<br /></li><li>echo -n "$TT"<br /></li><li>TT=""<br /></li><li>break<br /></li><li>elif [ "$TT" == "=" ];then<br /></li><li>sed -i "${seek}s/.*/$Str/" $F_CONF<br /></li><li>echo -n "$TT"<br /></li><li>TT=""<br /></li><li>break<br /></li><li>elif [ "$TT" == "-" ];then<br /></li><li>sed -i "${seek}s/.*/$Str/" $F_CONF<br /></li><li>echo -n "$TT"<br /></li><li>TT=""<br /></li><li>break<br /></li><li>fi<br /></li><li>fi<br /></li><li>done < $F_CONF<br /></li><li>if [ "$TT" != "" ];then<br /></li><li>Err=$Err" $First\n"<br /></li><li>fi<br /></li><li>done < $B_CONF<br /></li><li>echo<br /></li><li>done<br /></li><li>if [ "$Err" != "" ];then<br /></li><li>echo -e "\n\033[33mError: $Err\033[0m"<br /></li><li>Err=""<br /></li><li>fi<br /></li><li>elif [ "X$1" == "Xinsert" ];then<br /></li><li>num=0<br /></li><li>snum=0<br /></li><li>n=1<br /></li><li>nn=0<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>insert=($2)<br /></li><li>else<br /></li><li>echo -e "\033[31m Insert Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>IFS="$OLD_IFS"<br /></li><li>if [ -f "$confpath/${insert[0]}" ];then<br /></li><li>while read olc<br /></li><li>do<br /></li><li>((num++))<br /></li><li>tmp_olc=$(echo "$olc" | sed 's/ //g')<br /></li><li>tmp_olc=${tmp_olc%%=*}<br /></li><li>tmp_insert=$(echo "${insert[$n]}" | sed 's/ //g')<br /></li><li>tmp_insert=${tmp_insert%%=*}<br /></li><li>if [[ "$tmp_olc" == $tmp_insert && $n -le ${#insert[@]} ]];then<br /></li><li>((n++))<br /></li><li>if [ $n -eq $[ ${#insert[@]} - 1 ] ];then<br /></li><li>echo "Insert: ${insert[$n]} @ ${insert[0]}[$num]"<br /></li><li>snum=$num<br /></li><li>nn=$n<br /></li><li>elif [ $n -eq ${#insert[@]} ];then<br /></li><li>echo -e "\033[33m${insert[$nn]} exits.\033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>else<br /></li><li>if [[ $num -eq $[ $snum + 2 ] && $nn -eq $n ]];then<br /></li><li>break<br /></li><li>fi<br /></li><li>fi<br /></li><li>done < $confpath/${insert[0]}<br /></li><li>if [ $nn -ne $[ ${#insert[@]} - 1 ] ];then<br /></li><li>echo -e "\033[31m Insert Error: None ${insert[$nn]} \033[0m $[ $nn + 1 ]"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>Str="${insert[$nn]}"<br /></li><li>sed -i "${snum}a\\$Str" $confpath/${insert[0]}<br /></li><li>else<br /></li><li>echo -e "\033[31m Insert Error:File not exist $confpath/${insert[0]} \033[0m"<br /></li><li>fi<br /></li><li>elif [ "X$1" == "Xdelete" ];then<br /></li><li>num=0<br /></li><li>n=1<br /></li><li>del=0<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>delete=($2)<br /></li><li>else<br /></li><li>echo -e "\033[31m Delete Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>IFS="$OLD_IFS"<br /></li><li>if [ -f "$confpath/${delete[0]}" ];then<br /></li><li>while read olc<br /></li><li>do<br /></li><li>((num++))<br /></li><li>tmp_olc=$(echo "$olc" | sed 's/ //g')<br /></li><li>tmp_olc=${tmp_olc%%=*}<br /></li><li>tmp_delete=$(echo "${delete[$n]}" | sed 's/ //g')<br /></li><li>tmp_delete=${tmp_delete%%=*}<br /></li><li>#echo "$tmp_olc"<br /></li><li>if [[ $tmp_olc == $tmp_delete ]];then<br /></li><li>((n++))<br /></li><li>if [ $n -eq $[ ${#delete[@]} ] ];then<br /></li><li>echo "Delete: ${delete[$n-1]} @ ${delete[0]}[$num]"<br /></li><li>del=1<br /></li><li>break<br /></li><li>fi<br /></li><li>fi<br /></li><li>done < $confpath/${delete[0]}<br /></li><li>if [ $n -ne $[ ${#delete[@]} ] ];then<br /></li><li>echo -e "\033[33mCan't find:\"${delete[$n]}\"@ $[ $n + 1 ] \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>sed -i "${num}d" $confpath/${delete[0]}<br /></li><li>else<br /></li><li>echo -e "\033[31m Delete Error:File not exist $confpath/${delete[0]} \033[0m"<br /></li><li>fi<br /></li><li>elif [ "X$1" == "Xinsall" ];then<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>insert=($2)<br /></li><li>else<br /></li><li>echo -e "\033[31m insall Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>IFS="$OLD_IFS"<br /></li><li>if [ -f "$confpath/${insert[0]}" ];then<br /></li><li>sed -i "/${insert[1]}/a\\${insert[2]}" $confpath/${insert[0]}<br /></li><li>fi<br /></li><li>elif [ "X$1" == "Xcopy" ];then<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>field=($2)<br /></li><li>IFS="$OLD_IFS"<br /></li><li>else<br /></li><li>echo -e "\033[31m Copy Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>for((i=0;i<${#field[@]};i++)){<br /></li><li>Deep=$(dirname ${field[$i]} 2>/dev/null)<br /></li><li>copied_dir=$confpath/$Deep<br /></li><li>copied_file=$confpath/${field[$i]}<br /></li><li>bk_dir=$bkpath_whole/$Deep<br /></li><li>bk_file=$bkpath_whole/${field[$i]}<br /></li><li>if [ ! -d "$copied_dir" -o ! -f "$copied_file" ];then<br /></li><li>echo -e "\033[31m copy Error @ ${field[$i]}\033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>if [ -e "$bk_file" -a -s "$bk_file" ];then<br /></li><li>echo -e "\033[31m Error $bk_file exist and no empty. \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>mkdir -p $bkpath_whole/$Deep && /bin/cp -f $copied_file $bk_file <br /></li><li>check=$(diff $bk_file $copied_file)<br /></li><li>if [ "$check" == "" ];then<br /></li><li>echo "copy $copied_file => $bk_file"<br /></li><li>else<br /></li><li>echo "error copy @ ${field[$i]}"<br /></li><li>fi<br /></li><li>}<br /></li><li>elif [ "X$1" == "Xrcopy" ];then<br /></li><li>if [ "X$2" != "X" ];then<br /></li><li>OLD_IFS="$IFS"<br /></li><li>IFS="#"<br /></li><li>field=($2)<br /></li><li>IFS="$OLD_IFS"<br /></li><li>else<br /></li><li>echo -e "\033[31m rcopy Error \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>for((i=0;i<${#field[@]};i++)){<br /></li><li>if [ "${field[$i]}" == "" ];then<br /></li><li>continue<br /></li><li>fi<br /></li><li>Deep=$(dirname ${field[$i]} >/dev/null)<br /></li><li>rcopied_dir=$confpath/$Deep<br /></li><li>rcopied_file=$confpath/${field[$i]}<br /></li><li>bk_dir=$bkpath_whole/$Deep<br /></li><li>bk_file=$bkpath_whole/${field[$i]}<br /></li><li>if [ ! -d "$bk_dir" -o ! -f "$bk_file" ];then<br /></li><li>echo -e "\033[31m rcopy error $bk_dir not dir or $bk_file not file.\033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>if [ ! -s "$bk_file" ];then<br /></li><li>echo -e "\033[31m rcopy error $bk_file exist but empty. \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>if [ ! -d "$rcopied_dir" ];then<br /></li><li>echo -e "\033[31m rcopy error:$rcopied_dir not dir. \033[0m"<br /></li><li>exit 1<br /></li><li>fi<br /></li><li>/bin/cp -f $rcopied_file $rcopied_file.$handle_date || ( echo -e "\033[31mrcopy: backup $rcopied_file failed.\033[0m" && exit 1)<br /></li><li>/bin/cp -f $bk_file $rcopied_file || ( echo -e "\033[31mrcopy: rcopy: restore $rcopied_file failed.\033[0m" && rm -rf $rcopied_file.$handle_date && exit 1)<br /></li><li>check=$(diff $bk_file $rcopied_file)<br /></li><li>if [ "$check" == "" ];then<br /></li><li>echo "restore $bk_file => $rcopied_file"<br /></li><li>rm -rf $rcopied_file.$handle_date<br /></li><li>else<br /></li><li>echo "error rcopy @ ${field[$i]}"<br /></li><li>/bin/cp -f $rcopied_file.$handle_date $rcopied_file<br /></li><li>rm -rf $rcopied_file.$handle_date<br /></li><li>fi<br /></li><li>}<br /></li><li>elif [ "X$1" == "Xversion" ];then<br /></li><li>echo "Version:1.0.7"<br /></li><li>else <br /></li><li>echo -e "批量备份还原给定目录下的配置文件,可以备份某个文件中的一行或者多行、一个块或者多个块。恢复时,可以直接找到对应的行或者块还原。"<br /></li><li>echo -e "插入字段时,可能精确到具体的某一行"<br /></li><li>echo -e "要被备份的文件格式有两种:"<br /></li><li>echo -e "如:"<br /></li><li>echo -e "pattern_hot_switch=0 #行"<br /></li><li>echo -e "define server_proxy_host1{ #块,块以}作为结束符号"<br /></li><li>echo -e " part1=no1"<br /></li><li>echo -e " part2=no2"<br /></li><li>echo -e " part3=no3"<br /></li><li>echo -e "}"<br /></li><li>echo -e "Help:"<br /></li><li>echo -e "Backup Dir:"<br /></li><li>echo -e "\tServer Conf Dir: $confpath"<br /></li><li>echo -e "\tPartBKP Conf Dir: $bkpath"<br /></li><li>echo -e "\tWholeBKP Conf Dir: $bkpath_whole"<br /></li><li>echo "Usage: $0 [backup|restore|insert|delete|insall|copy|rcopy|version]"<br /></li><li>echo "backup:备份 restore:还原 insert:插入 delete:删除 insall:批量插入 copy:拷贝文件 rcopy:还原拷贝的文件"<br /></li><li>echo -e "\tbackup 'server.config#+srcpattern#=request_src_type#...#+src src_acl#'"<br /></li><li>echo -e "\tbackup 'main.config#p_src_switch#url_log_switch#=url_log_switch#'"<br /></li><li>echo -e "\trestore"<br /></li><li>echo -e "\trestore main.config"<br /></li><li>echo -e "\tinsert 'server.config#def p_r t_default#...#r_ww_switch# xxx_xxx_xxx=1-2-3-'"<br /></li><li>echo -e "\tdelete 'server.config#def p_r t_default#...#r_ww_switch#xxx_xxx_xxx'"<br /></li><li>echo -e "\tinsall 'server.config#def p_r t_default#xxx_xxx_xxx=1-2-3-'"<br /></li><li>echo -e "\tcopy 'main.config#r.config#....'"<br /></li><li>echo -e "\trcopy 'main.config#r.config#....'"<br /></li><li>fi</li><li><br /></li></ol>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of
