且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux下简单的备份的脚本 2 【转】

更新时间:2022-06-30 22:35:38

转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4577034

 

 之前写过 linux下简单的备份的脚本 , 最开始一直用着, 后来觉得有必要改进下它了, 不管是从操作方式上还是工作方式上。有这样的想法是因为一次备份引起的。 我经历过磁盘损坏的痛苦, 花了1500元才勉强将数据拯救回来, 于是导致我对备份要求更加高了, 我期望尽量每周备份, 期望备份的目的地是当前系统的完整镜像,也就是说我能够从备份盘启动,且启动后
系统的操作方法以及文件和当前的一模一样,每周我只需要增量备份当前的修改的备份盘,这样就安全了很多。于是就有了下面的备份脚本(我不喜欢造***,但是在linux下没有现成的适合我的)

NOTE: 当前还没有加入自动镜像系统,所以如果想镜像系统,那么可以手动这样操作, 准备一块大于当前系统盘的移动盘,
分区,注意分区的结构尽量和系统的一模一样, 然后格式化,文件系统也尽量相同, 然后就可以备份了 备份的时候主要有些
目录需要跳过,比如sys dev proc等等,需要跳过的目录可以在backup程序了面设置!
这是脚本程序:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # This program is free software; you can redistribute it and/or
  3. # modify it under the terms of the GNU General Public License as
  4. # published by the Free Software Foundation; either version 2 of
  5. # the License, or (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # Author: rongp
  11. # email: rongpmcu#gmail.com
  12. # Date: 2014/10/26
  13. # 备份程序
  14. # 特点:
  15. # 主要支持unix类系统下, 支持符号链接
  16. # 支持增量备份
  17. # 支持网络备份(由于基于rsync, 很容易加入该功能,但暂时没加入)
  18. # 文件名支持空格 但是不能出现含有@#@的文件名
  19. # 支持备份每次的更新,方便用于人工操作失误后的修复
  20. # 支持添加规则用于剔除某些文件 格式参考rsync的PATTERN部分
  21. SHELL=/bin/bash
  22. backup_cfg_path=/etc
  23. backup_cfg=$backup_cfg_path/backup.cfg
  24. db_path=
  25. db_pathname=
  26. inc_path=
  27. XECHO=1
  28. _help() 
  29. {
  30.     echo -e "$0 [option]\n"\
  31.         "\tOption:\n"\
  32.         "\t-h show this help.\n"\
  33.         "\t-i perform the installation, and you should use this option\n"\
  34.         "\t before using the backup to do something else.\n"\
  35.         "\t-u perform the un-installation.\n"
  36. }
  37. help() 
  38. {
  39.     echo -e "Command action\n"\
  40.         "\th show this help.\n"\
  41.         "\ta add any file that you want to backup to the database.\n"\
  42.         "\td delete any file that you no longer want from the database.\n"\
  43.         "\tb start backup.\n"\
  44.         "\tbf assume \"yes\" as answer to all prompts and run non-interactively.\n"\
  45.         "\tn perform a trial backup with no changes made.\n"\
  46.         "\tp print the file record.\n"\
  47.         "\tc do some configurations, such as, modifying the path to the\n"\
  48.         "\t database or to the incremental backup directory.\n"\
  49.         "\ts show the current configuration.\n"\
  50.         "\ti perform the installation, and you should use this option\n"\
  51.         "\t before using the backup to do something else.\n"\
  52.         "\tu perform the un-installation.\n"\
  53.         "\tq quit"
  54. }
  55. color_echo()
  56. {
  57.     case "$1" in 
  58.         g)
  59.             shift
  60.             echo -e "\033[32m"$@"\033[0m"
  61.         ;;
  62.         gn)
  63.             shift
  64.             echo -e -n "\033[32m"$@"\033[0m"
  65.         ;;
  66.         r)
  67.             shift
  68.             echo -e "\033[31m"$@"\033[0m"
  69.         ;;
  70.         y)
  71.             shift
  72.             echo -e "\033[33m"$@"\033[0m"
  73.         ;;
  74.         yn)
  75.             shift
  76.             echo -e -n "\033[33m"$@"\033[0m"
  77.         ;;
  78.         *)
  79.             shift
  80.             echo $@
  81.         ;;
  82.     esac
  83. }
  84. XECHO()
  85. {
  86.     if [ "$XECHO" = 1 ]; then
  87.         echo $@
  88.     fi
  89. }
  90. check_src_dst()
  91. {
  92.     if ! test -e "$1" || ! test -e "$2"; then
  93.         color_echo r "$1" or "$2" does not ignore 
  94.         return 2
  95.     fi
  96.     local src_part1=`df "$1" | cut -d ' ' -f 1`
  97.     local src_part2=`df "$2" | cut -d ' ' -f 1`
  98.     local nsrc_inode=`ls -lid "$1" | cut -d ' ' -f 1`
  99.     local ndst_inode=`ls -lid "$2" | cut -d ' ' -f 1`
  100.     XECHO nsrc_inode:$nsrc_inode ndst_inode:$ndst_inode
  101.     if [ "$src_part1" != "$src_part2" ]; then
  102.         return 1
  103.     fi
  104.     if [ "$nsrc_inode" = "$ndst_inode" ]; then
  105.             color_echo r "$src is equivalent to $dst. ignore it!"
  106.             return 2
  107.     fi
  108.     if [ ! -e $db_pathname ]; then
  109.         return 1
  110.     fi
  111.     while read -r tsrc tdst tex_src;
  112.     do
  113.         tsrc="${tsrc//@#@/ }"
  114.         tdst="${tdst//@#@/ }"
  115.         tex_src="${tex_src//@#@/ }"
  116.         XECHO tsrc:"$tsrc" tdst:"$tdst"
  117.         osrc_inode=`ls -lid "$tsrc" | cut -d ' ' -f 1`
  118.         odst_inode=`ls -lid "$tdst" | cut -d ' ' -f 1`
  119.         XECHO osrc_inode:$osrc_inode odst_inode:$odst_inode
  120.         if [ "$nsrc_inode" = "$osrc_inode" -a "$ndst_inode" = "$odst_inode" ]; then
  121.             if [ ${1:((${#1}-1))} = '/' -a ${tsrc:((${#tsrc}-1))} != '/' ] \
  122.                 || [ ${1:((${#1}-1))} != '/' -a ${tsrc:((${#tsrc}-1))} = '/' ]; then #/home and /home/ is very 
  123.                 echo -n "";
  124.             else
  125.                 return 0
  126.             fi
  127.         fi
  128.     done < $db_pathname
  129.     return 1 
  130. }
  131. extract_src_dst()
  132. {
  133.     XECHO "extract src dst from $1"
  134.     src="${1%#*}" 
  135.     dst="${1#$src}" 
  136.     dst="${dst#\#}" 
  137.     XECHO "src: $src"
  138.     XECHO "dst: $dst"
  139.     if [ "$src" = "" -o "$dst" = "" ]; then
  140.         return 1
  141.     else
  142.         return 0
  143.     fi
  144. }
  145. fix_path()
  146. {
  147.     local srcpath="$1"
  148.     if [ "${srcpath:0:1}" = '/' ]; then
  149.         echo $srcpath 
  150.     elif [ "${srcpath:0:2}" = './' ]; then
  151.         echo `pwd`/${srcpath:2}
  152.     else 
  153.         echo `pwd`/$srcpath
  154.     fi
  155. }
  156. insert_new_item()
  157. {
  158.     XECHO add item src:"$1" dst:"$2" exclude:"$3"
  159.     tmp1="${1// /@#@}"
  160.     tmp2="${2// /@#@}"
  161.     tmp3="${3// /@#@}"
  162.     echo "$tmp1" "$tmp2" "$tmp3" >> $db_pathname
  163.     return $?
  164. }
  165. parse_item()
  166. {
  167.     if ! extract_src_dst "$1"; then
  168.         color_echo r "src:$src or dst:$dst is illegal!"
  169.         return 1
  170.     fi
  171.     src=`fix_path "$src"`
  172.     dst=`fix_path "$dst"`
  173.     XECHO after fixed, src:"$src"
  174.     XECHO after fixed, src:"$dst"
  175.     return 0
  176. }
  177. do_add() 
  178. {
  179.     local item
  180.     
  181.     color_echo g "Enter the mode of adding files! Some patterns are available, as follows:"
  182.     color_echo g "eg: /home/#/tmp/ means we want to backup the whole things which "
  183.     color_echo g "are under home directory to /tmp directory."
  184.     color_echo g "eg: /home/#/tmp/:/etc/#/tmp/ means we want to backup the whole "
  185.     color_echo g "things which are under the home directory and the /etc/ directory "
  186.     color_echo g "to the /tmp directory, you can append any item with ':'."
  187.     color_echo r "Note: /home and /home/ are quite different, because /home just means "
  188.     color_echo r "/home itself while /home/ means the whole contents of /home."
  189.     read -p "Please type in file items: " items
  190.     items="`echo "$items" | sed "s/'//g"`"
  191.     flag=0
  192.     while [ $flag = 0 ]; 
  193.     do 
  194.         item=${items%%:*}
  195.         items=${items#$item:}
  196.         ex_src=""
  197.         if [ "$items" = "$item" ]; then 
  198.             flag=1
  199.         fi 
  200.         if parse_item "$item"; then
  201.             check_src_dst "$src" "$dst"
  202.             ret=$?
  203.             if [ "$ret" = 0 ]; then
  204.                 color_echo y "Warning! ""$src#$dst"" is already existed! do not re-submit!" 
  205.                 continue
  206.             elif [ "$ret" = 2 ]; then
  207.                 continue
  208.             fi
  209.             read -p "Would you like to add some excluding conditions to $src: (y/n)[n] " yn
  210.             if [ "$yn" = y ]; then
  211.                 color_echo r "Note: this is an expert mode, and we don't check your pattern"
  212.                 color_echo r "is valid or not. Some patterns are available, as follows:"
  213.                 color_echo r "eg: if your src directory is /home, and your want to exclude"
  214.                 color_echo r "the directory /home/rongp, then you should type in \"rongp\"." 
  215.                 color_echo r "eg: if your src directory is /home, and your want to exclude"
  216.                 color_echo r "the directory /home/rongp and /home/test, then you should" 
  217.                 color_echo r "type in \"rongp:test\", and you can append any item with ':' ."
  218.                 
  219.                 read -p "Please type in paths to the excluding files: " exitem 
  220.                 ex_src="$exitem"
  221.             fi
  222.             if insert_new_item "$src" "$dst" "$ex_src"; then
  223.                 echo ""$src"#"$dst" add successed!"
  224.             else 
  225.                 echo ""$src"#"$dst" add failed!"
  226.             fi
  227.         else 
  228.             read -p "skip it? Yy/Nn:[n] " yn
  229.             if [ "$yn" = "y" -o "$yn" = "Y" ]; then
  230.                 continue
  231.             fi
  232.             return 1
  233.         fi
  234.     done
  235.     return 0
  236. }
  237. get_choices()
  238. {
  239.     local total_line=`wc -l $db_pathname | cut -d ' ' -f 1`
  240.     
  241.     select_tab=
  242.     color_echo g "Enter the mode of "$1"! some patterns are available, as follows:"
  243.     color_echo g "eg: 1-3 means select no 1 2 3 item"
  244.     color_echo g "eg: 1:3:5 means select no 1 3 5 item"
  245.     color_echo g "you can append any no with ':' or '-', but don't mix use it."
  246.     color_echo g "no 0 means select all."
  247.     do_print
  248.     read -p "Please type in the number: " NO
  249.     if [ "${NO/-/ }" != "$NO" ]; then 
  250.         num_tab=(${NO//-/ })
  251.         [ ${#num_tab[@]} -gt 2 ] && \
  252.             echo "Select failed, argument $NO is illegal!" && return 1
  253.         
  254.         num0=${num_tab[0]}
  255.         num1=${num_tab[1]}
  256.         XECHO num0:$num0 num1:$num1
  257.         if [ -z "${num0//[0-9]/}" -a "$num0" -le "$total_line" -a "$num0" -gt "0" ]\
  258.            && [ -z "${num1//[0-9]/}" -a "$num1" -le "$total_line" -a "$num1" -gt "0" ]\
  259.            && [ "$num0" -lt "$num1" ]; 
  260.         then
  261.             select_tab=(`seq $num0 $num1`)
  262.         else 
  263.             echo "Select failed, argument $NO is illegal!" && return 1
  264.         fi
  265.     elif [ "${NO/:/ }" != "$NO" ]; then
  266.         for num in ${NO//:/ }
  267.         do
  268.             if [ -z "${num//[0-9]/}" ]&&[ "$num" -le "$total_line" ]\
  269.                 &&[ "$num" -gt "0" ]; then
  270.                 continue
  271.             else 
  272.                 echo "Select failed, argument $num is illegal!" && return 1
  273.             fi
  274.         done
  275.         j=0
  276.         for i in ${NO//:/ }
  277.         do
  278.             select_tab[j]=$i
  279.             ((j++))
  280.         done
  281.     else 
  282.         if [ "$NO" = 0 ]; then
  283.             select_tab=(`seq 1 $total_line`)
  284.         elif [ -z "${NO//[0-9]/}" ]&&[ "$NO" -le "$total_line" ]\
  285.                 &&[ "$NO" -gt "0" ]; then
  286.             select_tab[0]=${NO}
  287.         else 
  288.             echo "Select failed, argument $NO is illegal!" && return 1
  289.         fi
  290.     fi
  291.     return 0
  292. }
  293. do_del() 
  294. {
  295.     if ! get_choices "deleting files"; then
  296.         return 1
  297.     fi
  298.     local total_num=${#select_tab[@]}
  299.     if [ "$total_num" = 1 ]; then
  300.         nums=${select_tab[0]}d
  301.     elif [ "$total_num" = 2 ]; then
  302.         nums=${select_tab[0]},${select_tab[1]}d
  303.     else 
  304.         for ((i=0; i<$total_num; ++i))
  305.         do
  306.             nums+="${select_tab[i]}d;"
  307.         done
  308.     fi
  309.     sed -i "$nums" $db_pathname >/dev/null 2>&1
  310.     [ "$?" = 0 ] && echo "$NO delete successed!" || echo "$NO delete failed, delete failed!"
  311. }
  312. do_print()
  313. {
  314.     [ ! -s $db_pathname ] && color_echo y "Warning, no record found!" && return 1
  315.     echo " no source destination action"
  316.     cat -n $db_pathname | sed 's/@#@/ /g'
  317. }
  318. check_in_select_tab()
  319. {
  320.     local i
  321.     for ((i=0; i<${#select_tab[@]}; ++i))
  322.     do
  323.         XECHO $1:select_tab[$i]:${select_tab[i]}
  324.         if [ "${select_tab[i]}" = "$1" ]; then
  325.             return 0
  326.         fi
  327.     done
  328.     return 1
  329. }
  330. do_backup()
  331. {
  332.     local ex_file=`mktemp`
  333.     local fake="${1/fake/-n}"
  334.     local yes="${1/yes/y}"
  335.     [ ! -f "$db_pathname" ] && color_echo r "$db_pathname does not exist!" && return 1
  336.     if ! get_choices "backup"; then
  337.         return 1
  338.     fi
  339.     local i=0
  340.     local k=0
  341.     while read -r src dst ex_src;
  342.     do
  343.         if check_in_select_tab $((i+1)); then
  344.             XECHO "$i in select table"
  345.             src="${src//@#@/ }"
  346.             dst="${dst//@#@/ }"
  347.             XECHO src:$src dst:$dst ex_src:$ex_src ex_file:$ex_file
  348.             src_tab[k]="$src"
  349.             dst_tab[k]="$dst"
  350.             ex_src_tab[k]="$ex_src"
  351.             ((k++))
  352.         fi
  353.         ((i++))
  354.     done < $db_pathname
  355.     for ((j=0; j<$k; ++j))
  356.     do
  357.         echo src:${src_tab[j]} dst:${dst_tab[j]} ex_src:${ex_src_tab[j]}
  358.         src="${src_tab[j]}"
  359.         dst="${dst_tab[j]}"
  360.         ex_src="${ex_src_tab[j]}"
  361.         echo "$ex_src" | awk -F ':' '{for (i=1;i<=NF;++i)print $i}' | sed 's/@#@/ /g' > $ex_file
  362.         if [ "$src" = "/" ]; then
  363.             tmpsrc=$(blkid `mount | grep "/ " | cut -d ' ' -f 1` | awk -F "\"" '{print $2}')
  364.         else 
  365.             tmpsrc="$src"
  366.         fi
  367.         
  368.         if [ "$dst" = "/" ]; then
  369.             tmpdst=$(blkid `mount | grep "/ " | cut -d ' ' -f 1` | awk -F "\"" '{print $2}')
  370.         else 
  371.             tmpdst="$dst"
  372.         fi
  373.         color_echo g "We will start backup from "
  374.         color_echo r "$src"
  375.         color_echo g to 
  376.         color_echo r "$dst" 
  377.         color_echo g "with excluding file or directory" 
  378.         color_echo r "${ex_src//@#@/ }"
  379.         color_echo gn "continue or not? y/n: "
  380.         if [ "$yes" = y ]; then 
  381.             yn=y
  382.         else 
  383.             read yn
  384.         fi
  385.         if [ "$yn" = y ]; then
  386.             echo Start backup "$src" to "$dst" with excluding file or directory "$ex_src"
  387.             (
  388.             flock -x 200
  389.             rsync -avzrtopg $fake --progress --delete --exclude-from=$ex_file \
  390.                 "$src" "$dst" --backup --backup-dir=$inc_path/$(date +%Y-%m-%d_%H:%M:%S_$(basename ${tmpsrc})_$(basename $tmpdst))
  391.             ) 200>/var/lock/abc
  392.             echo Backup $src to $dst with excluding file or directory $ex_src 
  393.         else 
  394.             echo Skip backup $src to $dst with excluding file or directory $
  395.         fi
  396.     done
  397.     
  398. }
  399. get_answer()
  400. {
  401.     local ret
  402.     if [ "$4" != "" ]; then
  403.         tmpbackup="$4"
  404.     else
  405.         tmpbackup=backup.cfg.bak
  406.     fi
  407.     while :
  408.     do
  409.         read -p "Type in $1 path of the backup(default is $2, q for exit): " ans_tmp 
  410.         if [ "$ans_tmp" = q ]; then
  411.             ret=1
  412.             break
  413.         elif [ "$ans_tmp" != "" ]; then 
  414.             if [ ! -d "$ans_tmp" ]; then
  415.                 echo "$1: $ans_tmp is invalid!" 
  416.                 read -p "Would you like to create it now? y/n [y]: " yn
  417.                 if [ "$yn" = y ]; then
  418.                     mkdir -p $ans_tmp
  419.                 else
  420.                     continue
  421.                 fi
  422.             fi
  423.             sed -i "s,$3.*,$3$ans_tmp,g" $tmpbackup
  424.             ret=$?
  425.             break
  426.         else
  427.             ans_tmp="$2"
  428.             ret=0 
  429.             break
  430.         fi
  431.     done
  432.     return $ret
  433. }
  434. already_install()
  435. {
  436.     if load_cfg $backup_cfg s; then
  437.         XECHO "already install"
  438.         return 0 #has install
  439.     fi
  440.     return 1
  441. }
  442. do_install()
  443.     color_echo g start install
  444.     if already_install; then 
  445.         color_echo y "We check that you have already installed, you should"
  446.         color_echo yn "uninstall first, would you want to uninstall it first?y/n[n] "
  447.         read yn
  448.         if [ "$yn" != y ]; then
  449.             color_echo g install 
  450.             color_echo r install 
  451.             return 1
  452.         else 
  453.             do_uninstall
  454.         fi
  455.     fi
  456.     cp -f backup.cfg backup.cfg.bak
  457.     load_cfg backup.cfg.bak s
  458.     if [ "$?" = 1 ]; then
  459.         exit
  460.     fi
  461.     if ! get_answer "executable file backup" "$bin_path" "INSTALL_PATH=";then
  462.         color_echo g install 
  463.         color_echo r install 
  464.         return 1
  465.     fi
  466.     install_path=$ans_tmp
  467.     color_echo g install path is $install_path
  468.     if ! get_answer "database" "$db_path" "DB_PATH=";then
  469.         color_echo g install 
  470.         color_echo r install 
  471.         return 1
  472.     fi
  473.     db_path=$ans_tmp
  474.     color_echo g database path is $db_path
  475.     if ! get_answer "incremental backup" "$inc_path" "INCREMENTAL_BACKUP_PATH=";then
  476.         color_echo g install 
  477.         color_echo r install 
  478.         return 1
  479.     fi
  480.     inc_path=$ans_tmp
  481.     color_echo g incremental backup path is $inc_path
  482.     echo 
  483.     who=`whoami`
  484.     cp backup $install_path
  485.     color_echo g install backup to $install_path
  486.     ret=$?
  487.     mv backup.cfg.bak $backup_cfg
  488.     color_echo g install $backup_cfg
  489.     ret=$((ret+$?))
  490.     mkdir -p $db_path
  491.     color_echo g install $db_path
  492.     ret=$((ret+$?))
  493.     mkdir -p $inc_path
  494.     color_echo g install $inc_path
  495.     ret=$((ret+$?))
  496.     ln -s $db_path $inc_path/db
  497.     color_echo g install $inc_path/db
  498.     ret=$((ret+$?))
  499.     color_echo g install 
  500.     if [ $ret -gt 0 ]; then 
  501.         color_echo r install 
  502.         [ -e $bin_path/backup ] && rm_print $bin_path/backup
  503.         [ -e $backup_cfg ] && rm_print $backup_cfg
  504.         [ -e $inc_path/db ] && rm_print $inc_path/db && rm_print -rf $inc_path
  505.         [ -e $db_pathname ] && rm_print $db_pathname
  506.         rm_print -d $db_path
  507.         return 1
  508.     fi
  509.     echo 
  510.     echo 
  511.     color_echo y "The installation work is done, and you can remove this package now!"
  512.     color_echo y "Note: you should put the executable file \"backup\""
  513.     color_echo y "into \$PATH and you need to get \"root\" privileges to execute it."
  514.     color_echo y "for example, you can execute it like this in ubuntu: sudo backup"
  515.     return 0
  516. }
  517. rm_print()
  518. {
  519.     color_echo g remove $@
  520.     eval rm $@
  521. }
  522. do_uninstall()
  523. {
  524.     XECHO "Perform the un-installation."
  525.     color_echo g perform the un-installation...
  526.     if ! load_cfg $backup_cfg; then 
  527.         color_echo g uninstall 
  528.     fi
  529.     [ -e $bin_path/backup ] && rm_print $bin_path/backup
  530.     [ -e $backup_cfg ] && rm_print $backup_cfg
  531.     [ -e $inc_path/db ] && rm_print $inc_path/db && rm_print -rf $inc_path
  532.     [ -e $db_pathname ] && rm_print $db_pathname
  533.     rm_print -d $db_path
  534.     color_echo g uninstall 
  535.     color_echo g uninstall 
  536. }
  537. load_cfg()
  538. {
  539.     if [ ! -e "$1" ]; then
  540.         [ "$2" != "s" ] && color_echo r "Error, we can't find the configure file $1, exit now!"
  541.         return 1
  542.     fi
  543.     bin_path=`sed -n 's/INSTALL_PATH=\(.*\)/\1/p' $1`
  544.     db_path=`sed -n 's/DB_PATH=\(.*\)/\1/p' $1`
  545.     db_pathname=$db_path/backup.db
  546.     inc_path=`sed -n 's/INCREMENTAL_BACKUP_PATH=\(.*\)/\1/p' $1`
  547.     if [ ! -d "$inc_path" ]; then 
  548.         [ "$2" != "s" ] && color_echo r Load configuration file your should
  549.         [ "$2" != "s" ] && color_echo r check the directory $db_pathname is valid or 
  550.         return 2
  551.     fi
  552.     XECHO database path is $db_path
  553.     XECHO database file path is $db_pathname
  554.     XECHO incremental backup path is $inc_path
  555.     return 0
  556. }
  557. show_configure()
  558. {
  559.     color_echo g executable backup is in $bin_path
  560.     color_echo g database directory is in $db_path
  561.     color_echo g incremental backup directory is in $inc_path
  562. }
  563. do_modify_inc_backup_path()
  564. {
  565.     if ! get_answer "incremental backup" "$inc_path" \
  566.         "INCREMENTAL_BACKUP_PATH=" $backup_cfg;then
  567.         return 1
  568.     fi
  569.     inc_path=$ans_tmp
  570.     XECHO incremental backup is $inc_path
  571.     return 0
  572. }
  573. do_configure()
  574. {
  575.     color_echo g [1] modify incremental backup path
  576.     color_echo g [2] ...
  577.     read -p "Please type in the no which you are expecting to: " no
  578.     if [ "$no" = 1 ]; then
  579.         do_modify_inc_backup_path
  580.     else
  581.         color_echo r Unsupported 
  582.     fi
  583. }
  584. backup_start()
  585. {
  586.     if ! load_cfg $backup_cfg; then 
  587.         exit
  588.     fi
  589.     while :
  590.     do
  591.         read -p "Command (h for help): " cmd
  592.         case "$cmd" in
  593.             a)
  594.                 do_add
  595.                 ;;
  596.             d)
  597.                 do_del 
  598.                 ;;
  599.             p)
  600.                 do_print
  601.                 ;;
  602.             c)
  603.                 do_configure 
  604.                 ;;
  605.             b)
  606.                 do_backup 
  607.                 ;;
  608.             bf)
  609.                 do_backup yes 
  610.                 ;;
  611.             n)
  612.                 do_backup fake
  613.                 ;;
  614.             s)
  615.                 show_configure
  616.                 ;;
  617.             i)
  618.                 do_install
  619.                 ;;
  620.             u)
  621.                 do_uninstall
  622.                 exit
  623.                 ;;
  624.             q)
  625.                 break
  626.                 ;;
  627.             h | *)
  628.                 help 
  629.                 ;;
  630.         esac
  631.     done
  632. }
  633. username=`echo $USER`
  634. if [ "$username" != root ]; then
  635.     color_echo r "Error, you need to have \"root\" privileges to execute this program."
  636.     exit
  637. fi
  638. if [ "$1" = "-i" ]; then
  639.     if ! do_install; then
  640.         color_echo y "Sorry, We can't continue any more. Exit now!" 
  641.     fi
  642.     exit
  643. elif [ "$1" = "-u" ]; then
  644.     do_uninstall 
  645.     exit
  646. elif [ "$1" = "-h" ]; then
  647.     _help
  648.     exit
  649. fi
  650. if [ ! -e $backup_cfg ]; then 
  651.     color_echo r "$backup_cfg does not exist! "
  652.     read -p "You need to install the backup first. perform the installation? y/n?[y]: " yn 
  653.     if [ "$yn" != n ]; then
  654.         do_install
  655.     else
  656.         echo Sorry, we can\'t continue any more. Exit 
  657.     fi
  658.     exit
  659. fi
  660. backup_start


这是配置文件

点击(此处)折叠或打开

  1. #############################
  2. ######
  3. #############################
  4. AUTHOR=rongp
  5. EMAIL=rongpmcu@gmail.com
  6. VERSION=1.0
  7. INSTALL_PATH=/usr/bin/
  8. DB_PATH=/var/lib/backup/
  9. INCREMENTAL_BACKUP_PATH=/var/lib/backup/incr_backup


git路径:git@bitbucket.org:rongpmcu/backup-script-shell.git

【作者】张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.