免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2281 | 回复: 6
打印 上一主题 下一主题

贴一个用sh备份的脚本[未包含恢复功能] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-03 15:40 |只看该作者 |倒序浏览
这个脚本既是练习也可以提供实际的工作。
此脚本在FreeBSD6.0、RedHat9.0上测试通过。

贴出来既服务大家,也欢迎各位拍砖。

本文最早发表于:半亩田

脚本目录结构:

  1. bin/  conf/
复制代码



  1. %cat bin/backup.sh
  2. #!/bin/sh

  3. #
  4. # Name:backup.sh
  5. ## Desinatio: Backup your file and tranlate to another space
  6. # Version: 0.01
  7. # Create_date: 2006/10/27
  8. # Last_update: 2006/11/02
  9. #

  10. # Load lib file
  11. libLOAD()
  12. {
  13.         if [ -z $1 ] || [ ! -f $1 ] ; then
  14.                 echo "No lib file"
  15.                 exit
  16.         else
  17.                 if ! . $1 ; then
  18.                         echo "Load file faild!"
  19.                         exit
  20.                 else
  21.                         return 0
  22.                 fi
  23.         fi
  24. }


  25. VERBOSE=false

  26. libLOAD ../conf/backup.lib.sh

  27. USAGE=`
  28. cat << EOF
  29. usage:
  30.         $(basename $0) -v -c -t -h
  31.                 -v show message
  32.                 -c assign the config file.
  33.                 -t assign backup type
  34.                    normal will be default.
  35.                    differential
  36.                    incremental
  37.                    restore
  38.                 -h show this message.
  39. \n`

  40. #---------------------------------
  41. #
  42. #       Main
  43. #       Get more to read ../conf/backup.lib.sh
  44. #
  45. #---------------------------------

  46. if [ $# -lt 1 ] ; then
  47.   printf "${USAGE}"
  48.   exit $?
  49. fi


  50. while getopts c:t:vh OPTION
  51. do
  52.   case "${OPTION}" in
  53.   v)
  54.   VERBOSE="true"
  55.   ;;
  56.   c)
  57.         libLOAD "${OPTARG}"
  58.   ;;
  59.   t)
  60.         case "${OPTARG}" in
  61.                 normal)
  62.                 backup_type="${OPTARG}"
  63.                 ;;
  64.                 differential)
  65.                 backup_type="${OPTARG}"
  66.                 ;;
  67.                 incremental)
  68.                 backup_type="${OPTARG}"
  69.                 ;;
  70.                 restore)
  71.                 backup_type="${OPTARG}"
  72.                 ;;
  73.                 *)
  74.                 messageREPORT "The backup type is not support!\n"
  75.                 exit $?
  76.                 ;;
  77.         esac
  78.   ;;
  79.   h)
  80.   printf "${USAGE}"
  81.   exit $?
  82.   ;;
  83.   *)
  84.   printf "${USAGE}"
  85.         exit $?
  86.   ;;
  87.   esac
  88. done

  89. # this test will let the program to load the default
  90. # configuration file.
  91. # the variable is only a test. it can be change to another one.

  92. if [ -z ${backup_file_list} ]
  93. then
  94.         libLOAD ../conf/backup_variable_configuration.default
  95. fi

  96. #-------------------------------------------
  97. #
  98. # check the backup type
  99. # if is not restore
  100. # will do normal incremental differential
  101. #
  102. #-------------------------------------------


  103. # The program should check the backup_type.
  104. # if it is not restore. it will help user
  105. # to do the three types backup actions.
  106. if [ "${backup_type}" != restore ] ; then

  107. messageREPORT "checking tmp dir ..."
  108. if backuptmpdirCHECK "${backup_tmp_dir}" ; then
  109.         messageREPORT "done.\n"
  110. else
  111.         messageREPORT "fail.\n"
  112.         exit
  113. fi

  114. messageREPORT "checking source file ..."
  115. if sourceCHECK ${backup_source_conf} ; then
  116.         messageREPORT "${backup_source_conf}.\n"
  117. else
  118.         messageREPORT "fail.\n"
  119. fi

  120. messageREPORT "checking backup type ..."
  121. messageREPORT "${backup_type}\n"

  122. flagfilecheck=true # this boolean flag is very importent!

  123. # in the function bellow backup_file_listCREATE
  124. # if the backup_type is incremental and if the user
  125. # never make a incremental type backup, the program will
  126. # use the normal backup flag. it is as the same to the
  127. # differential type.
  128. # but we need a real flag file.
  129. # so we use a temp varibal to get the backup type and create a
  130. # real flag file for the current type.
  131. # All the work is just for the incremental type.

  132. backup_file_listCREATE "${backup_type}"

  133. if [ ${flagfilecheck} = true ] && [ ${backup_type} != differential ] ; then
  134.         messageREPORT "checking flag file in ${backup_type} mode ...\n"
  135.         flagCREATE "${backup_flag_file}"
  136. else
  137.         if [ ${flagfilecheck} = false ] ; then
  138.                 tempflagfile="${backup_flag_file}"
  139.                 backup_flag_file="${backup_config_dir}.${backup_type}${backup_flag}"
  140.                 flagCREATE "${backup_flag_file}"
  141.                 backup_flag_file="${tempflagfile}"
  142.         else
  143.                 :
  144.         fi
  145. fi
  146. # if any questions see above.

  147. messageREPORT "checking spaces ... "
  148. if spaceCHECK "${backup_tmp_dir}"  ; then
  149.         messageREPORT "NOT enough space!\n"
  150.         exit $?
  151. else
  152.         messageREPORT "done.\n"
  153. fi

  154. backupCOMMAND "${backup_file_list}"


  155. #------------------------------------------
  156. #
  157. # if the backup type is restore
  158. #
  159. #------------------------------------------

  160. else
  161.         messageREPORT "restore\n"
  162.         messageREPORT "This function is not support\n"
  163. fi
复制代码

[ 本帖最后由 Knud 于 2006-11-3 17:07 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-11-03 15:42 |只看该作者

  1. ls conf/
  2. backup.lib.sh                          backup_variable_configuration.default
  3. backup_source.conf                     makeconfig.sh*
复制代码


  1. cat backup.lib.sh
  2. messageREPORT()
  3. {
  4.         if [ ${VERBOSE} = "true" ] ; then
  5.                 printf "${1}"
  6.                 printf "${1}" >> ${messagefile}
  7.         else
  8.                 printf "${1}" >> ${messagefile}
  9.         fi
  10. }

  11. #
  12. sourceCHECK()
  13. {
  14.         if [ -z $1 ] || [ ! -f $1 ] ; then
  15.                 messageREPORT "You have no source file. I do not know what to do.\n"
  16.                 exit
  17.         else
  18.         if [ $(du ${1}|sed -e 's/\..*//') -eq 0 ] ; then
  19.                 messageREPORT "${1} is empty.\n"
  20.                 exit $?
  21.         else
  22.                 while read LINE
  23.                         do
  24.                                 if [ -f ${LINE} ] || [ ! -x ${LINE} ] ; then
  25.                                         messageREPORT "Your souce file's format is not right!\n"
  26.                                         messageREPORT "Maybe ${LINE} is not a directory.\n"
  27.                                         exit
  28.                                 fi
  29.                         done < $1
  30.         fi
  31.         fi
  32. }

  33. backuptmpdirCHECK()
  34. {
  35. if [ ! -d "${1}" ] ; then
  36.         if /bin/mkdir -p "${1}"  > /dev/null ; then
  37.                 return 0
  38.         else
  39.                         return 1
  40.         fi
  41. else
  42.         touch  "$1"/a$$  > /dev/null 2>&1
  43.                 if [ $? = 0 ] ; then
  44.                         /bin/rm -f "$1"/a$$
  45.                         return 0
  46.                 else
  47.                         return 1
  48.                 fi
  49. fi
  50. }

  51. #To create flag file

  52. filecreate()
  53. {
  54. messageREPORT "creating a new one ... "
  55. if touch "${1}" ; then
  56. messageREPORT "${1}. "  
  57. messageREPORT "done.\n"
  58. else
  59. messageREPORT "\nI can not create the flag file ${1}."
  60. exit
  61. fi
  62. }

  63. flagCREATE()
  64. {
  65.         if [ -z "${1}" ] ; then
  66.                 messageREPORT "you must tell me where is your flag file.\n"
  67.         fi

  68.         if [ ! -f ${1} ] ; then
  69.                 messageREPORT "There is no flag file ${1}.\n"
  70.                 filecreate ${1}
  71.         else
  72.                 messageREPORT "removing the old flag file ${1} "
  73.                 /bin/rm -f ${1}
  74.                 messageREPORT "done\n"
  75.                 filecreate ${1}
  76.         fi
  77. }


  78. dirDEL()
  79. {
  80.         if [ $(du ${1}|sed -e 's/\..*//') -eq 0 ] ; then
  81.                 messageREPORT "${1} is empty, do not need backup.\n"
  82.                 messageREPORT "That means no file newer than ${backup_flag_file}\n"
  83.                 exit $?
  84.         else
  85.                 while read LINE
  86.                 do
  87.                         if [ -f "${LINE}" ] ; then
  88.                                 messageREPORT "\n${LINE}\n" >> ${listfiletemp}
  89.                         fi
  90.                 done < "${1}"

  91.                 messageREPORT "formating the file list temp file ...\t"
  92.                 if sed -i '/^$/d' ${listfiletemp} ; then
  93.                         messageREPORT "done.\n"
  94.                 else
  95.                         messageREPORT "fail.\n"
  96.                 fi
  97.                 messageREPORT "creating the final list file ...\t"
  98.                 if mv ${listfiletemp} ${backup_file_list} ; then
  99.                         messageREPORT "done.\n"
  100.                 else
  101.                         messageREPORT "fail.\n"
  102.                 fi
  103.         fi
  104. }

  105. restoreLIST()
  106. {
  107.         > ${restore_file_list}
  108. while read LINE
  109. do
  110.         find ${LINE} -print >> ${restore_file_list}
  111. done < ${backup_source_conf}

  112. }

  113. spaceCHECK()
  114. {
  115.         freespace=$(df "${1}"| sed -n '2p' | tr -s " " +|cut -f4 -d"+")

  116.         used=0
  117.         while read LINE
  118.         do
  119.                 duresault=$(du -s "${LINE}"|sed 's/\..*$//')
  120.                 usedtemp=${duresault%%/*}
  121.                 used=$(( ${used} + ${usedtemp} ))
  122.         done < "${backup_source_conf}"

  123.         freespace=$(( ${freespace} + 5000 ))

  124.         if [ "${used}" -ge "${freespace}" ] ; then
  125.                 return 0
  126.                 exit
  127.         else
  128.                 return 1
  129.         fi
  130. }


  131. backupCOMMAND()
  132. {
  133.         if [ -z ${1} ] ; then
  134.                 messageREPORT "Since you do not tell me the backup type.\n"
  135.                 messageREPORT "I will use normal type.\n"
  136.         fi
  137.         messageREPORT "creating ${backup_target_file_name} file ..."
  138.         tar -cPp -T ${1:-${backup_source_conf}} -f "${backup_tmp_dir}"/"${backup_target_file_name}"
  139.         messageREPORT "Done!\n"
  140. }

  141. backup_file_listCREATE()
  142. {
  143.         case "${backup_type}" in
  144.                 normal)
  145.                 backup_file_list="${backup_source_conf}"
  146.                 restoreLIST
  147.                 ;;
  148.                 differential)

  149.                         # to defferential is just to a normal backup.
  150.                         # so the flag file is the same with the normal type.

  151.                         backup_flag_file=${backup_config_dir}.normal${backup_flag}


  152.                         # if no flag file program will stop.
  153.                         messageREPORT "checking flag file ...\t "
  154.                         if [ ! -f ${backup_flag_file} ] ; then
  155.                                 messageREPORT "missing.\n"
  156.                                 messageREPORT "It seamed that you have never backup yet!\n"
  157.                                 messageREPORT "To avoid this message you should make a normal backup first.\n"
  158.                                 exit $?
  159.                         else
  160.                                 messageREPORT "done.\n"
  161.                         fi

  162.                         messageREPORT "updating the ${backup_type} list file ... "

  163.                         if [ -f ${backup_file_list} ] ; then
  164.                                 /bin/rm -f ${backup_file_list} # if exist a backup_list_file, prgram should rm it first.
  165.                         fi

  166.                         while read LINE
  167.                                 do
  168.                                         find ${LINE} -newer ${backup_flag_file} -print >> ${backup_file_list}
  169.                                 done < ${backup_source_conf}
  170.                         messageREPORT "done.\n"

  171.                         dirDEL "${backup_file_list}"
  172.                         restoreLIST

  173.                 ;;
  174.                 incremental)
  175.                 # if no file list file for incremental
  176.                 # program should check the normal flag file.
  177.                 # because it may be the first incremental backup.

  178.                 updatelistfile()
  179.                 {
  180.                         messageREPORT "updating the ${backup_type} list file ... "
  181.                         if [ -f ${backup_file_list} ] ; then
  182.                                 /bin/rm -f ${backup_file_list} # if exist a backup_list_file, prgram should rm it first.
  183.                         fi

  184.                         while read LINE
  185.                         do
  186.                         find ${LINE} -newer ${backup_flag_file} -print >> ${backup_file_list}
  187.                         done < ${backup_source_conf}
  188.                         messageREPORT "done.\n"

  189.                                 dirDEL "${backup_file_list}"
  190.                                 restoreLIST
  191.                 }

  192.                 if [ ! -f ${backup_flag_file} ] ; then
  193.                         backup_flag_file="${backup_config_dir}.normal${backup_flag}"
  194.                         flagfilecheck=false
  195.                 fi

  196.                 # check the flag file again.
  197.                 # if there is no flag file yet. that means that user never backup.
  198.                 # program should tell user to backup first.
  199.                 messageREPORT "checking flag file ...\t "
  200.                 if [ ! -f ${backup_flag_file} ] ; then
  201.                         messageREPORT "missing.\n"
  202.                         messageREPORT "It seamed that you have never backup yet!\n"
  203.                         messageREPORT "To avoid this message you should make a normal backup first.\n"
  204.                         exit $?
  205.                 else
  206.                         messageREPORT "found.\n"
  207.                 fi
  208.                 updatelistfile
  209.                 ;;
  210.                 *)
  211.                 messageREPORT "You should tell me the backup_type by \"-t\"\n"
  212.                 ;;
  213.         esac
  214. }

  215. tempCLEAN()
  216. {
  217.         if [ $# -lt 1 ] ; then
  218.                 messageREPORT "nothing to be clean.\n"
  219.         else
  220.                 messageREPORT "cleaning ${1}\t"
  221.                 if rm -rf "$1" ; then
  222.                         messageREPORT "done.\n"
  223.                 else
  224.                         messageREPORT "fail.\n"
  225.                         messageREPORT "remember to delete it!\n"
  226.                 fi
  227.         fi
  228. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-11-03 15:43 |只看该作者

  1. %cat backup_variable_configuration.default
  2. #configuration settings start
  3. backup_config_dir="${backup_config_dir:-../conf/}"
  4. backup_source_conf="${backup_source_conf:-${backup_config_dir}backup_source.conf}"
  5. backup_type="${backup_type:-normal}"

  6. backup_flag_path="${backup_flag_path:-../conf/}"
  7. backup_flag="${backup_flag:-_flag}"
  8. backup_flag_file="${backup_flag_file:-${backup_config_dir}.${backup_type}${backup_flag}}"

  9. backup_tmp_dir="${backup_tmp_dir:-/tmp/backup}"

  10. backup_file_list="${backup_file_list:-${backup_config_dir}.${backup_type}_file_list}"

  11. TIME_STAMP=$(date +%Y-%m-%d-%H-%M-%S)
  12. restore_file_list="${backup_tmp_dir}/.${TIME_STAMP}-${backup_type}-restore"

  13. listfiletemp="${backup_config_dir}.listfiletemp}$$"
  14. # files below including temp file will be removed
  15. # when you use -d option.
  16. backup_target_file_name="${backup_target_file_name:-${TIME_STAMP}-${backup_type}.tar.gz}"

  17. #files below are called tempfie.
  18. #if you use -f option these fiels will be removed.
  19. messagefile="${backup_config_dir}.${TIME_STAMP}-messagefile$$"

  20. #configuration settings end
复制代码


  1. cat makeconfig.sh
  2. #!/bin/sh

  3. . ./backup.lib.sh

  4. if [ -z ${1} ] ; then
  5.         if [ -f ./backup_variable_configuration.default ] ; then
  6.                 configfile=./backup_variable_configuration.default
  7.         else
  8.                 printf "Missing default configuration file!\n"
  9.                 exit $?
  10.         fi
  11. else
  12.         configfile=${1}
  13. fi

  14. sed -e '/^#configuration start/,/^#configuration end/p' -e '/^#/d'\
  15.                 -e '/^$/d' ${configfile}|cut -d"=" -f1 >> ./.configtemp$$

  16. printf "Creating config file ..."
  17. while read LINE
  18. do
  19.         echo "${LINE}=" >> ./.backup_variable_configuration$$
  20. done < ./.configtemp$$

  21. sleep 2
  22. printf "  done.\n"
  23. sleep 2

  24. mv ./.backup_variable_configuration$$ backup_variable_configuration
  25. printf "The configuration file is created.\n"
  26. printf "before it work,you chould complete it by yourself or you can use the default variable file.\n"

  27. /bin/rm -f ./.configtemp$$
复制代码

论坛徽章:
0
4 [报告]
发表于 2006-11-03 15:47 |只看该作者
沙发,第一次做

论坛徽章:
0
5 [报告]
发表于 2006-11-03 15:58 |只看该作者
说点体会:

1 不同的sh的确存在兼容与否的问题——当然,这是当然的。不过,很多Linux分版的/bin/sh都是到/bin/bash的连接,所以可能不是很容易体会到。比如在sh中就没有typeset;

2 动手写之前要有缜密的构思:一边想,一边写太痛苦了;

3 函数的返回值还是没有很好地理解,所以在这个脚本中用得乱七八糟;

4 exit到底后面跟个什么数,也还是不清楚。

论坛徽章:
0
6 [报告]
发表于 2006-11-03 17:13 |只看该作者
此贴被删除

[ 本帖最后由 chancey 于 2009-1-10 17:24 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-11-04 10:48 |只看该作者
原帖由 Knud 于 2006-11-3 15:58 发表
说点体会:

1 不同的sh的确存在兼容与否的问题——当然,这是当然的。不过,很多Linux分版的/bin/sh都是到/bin/bash的连接,所以可能不是很容易体会到。比如在sh中就没有typeset;

2 动手写之前要有缜密的构 ...


0~255 之間,根據Unix 的傳統, 返回 1 是 general error , 2 ..忘記了 , 127 是找不到這個命令
[victor@localhost ~]$ kkk
bash: kkk: command not found
[victor@localhost ~]$ echo $?
127
[victor@localhost ~]$
其它很隨意,  exit 不加 number 也有返回值, 只是使用者不能從 $? 估計錯誤的成因
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP