Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 自动备份脚本 Linux shell 第二版

脚本比上一版新增了(http://www.linuxidc.com/Linux/2012-11/73781.htm),记录时间戳功能。可以将你实时修改的文件进行备份。适合一些个人照片,文档,代码的备份;./backup_file  目录下 应该有一个 list_file 目录用来存储文件列表和文件时间戳。$1 为你需要备份的目录  。   $2  为备份到的目录$1 不能为空, 后续会继续修改一些bug, 并进一步完善脚本,若你发现那一段代码存在bug ,可以告知博主。或者你有更好的实现方法,也可以一起探讨。还有现在这个脚本,目前还不能备份子目录,只能备份文件。 以后发布将会加上这个对子目录备份的功能。
  1. #!/bin/sh
  2. # name backup file
  3. # author acanoe
  4. # timedate 2012_11_9 13:50
  5. # versions 0.1
  6. judge_your_input()
  7. {
  8. if [ -z $1 ] ; then
  9. echo "please input your updatedir!"
  10. echo ""
  11. echo "example:"
  12. echo "./back_file /backdir /updatedir"
  13. echo "like this example"
  14. echo ""
  15. exit
  16. fi
  17. if [ -z $2 ] ; then
  18. echo "please input your backdir!"
  19. echo ""
  20. echo "example:"
  21. echo "./back_file /backdir /updatedir"
  22. echo "like this example"
  23. echo ""
  24. exit
  25. fi
  26. }
  27. clear_record()
  28. {
  29. if [ -e ./list_file/old_time_ioc ] ; then
  30. rm ./list_file/old_time_ioc
  31. fi
  32. if [ ! -e ./list_file/new_time_ioc ] ; then
  33. echo "create a new_time_ioc"
  34. touch ./list_file/new_time_ioc
  35. fi
  36. if [ -e ./list_file/new_time_ioc ] ; then
  37. mv ./list_file/new_time_ioc ./list_file/old_time_ioc
  38. else
  39. touch ./list_file/old_time_ioc
  40. fi
  41. }
  42. find_change_file()
  43. {
  44. ls $1 > ./list_file/update_file_list
  45. ls $2 > ./list_file/back_file_list
  46. while read line
  47. do
  48. R=$(echo $line)
  49. echo "$R `stat $1/$R | sed -n "7p"`" >> ./list_file/new_time_ioc
  50. if [ ! -e ./list_file/new_time_ioc ] ; then
  51. echo "create a new_time_ioc"
  52. touch ./list_file/new_time_ioc
  53. fi
  54. done < ./list_file/update_file_list
  55. # while read line
  56. # do
  57. # R=$(echo $line)
  58. # echo "$R `stat $2/$R | sed -n "7p"`" >> ./list_file/old_time_ioc
  59. # echo "$R"
  60. # done < ./list_file/back_file_list
  61. diff ./list_file/new_time_ioc ./list_file/old_time_ioc > ./list_file/diff_file
  62. sed -n "/</p" ./list_file/diff_file > ./list_file/file
  63. sed -e "s/< //g" ./list_file/file > ./list_file/the_file
  64. sed -e "s/< //g" ./list_file/file > ./list_file/the_file
  65. }
  66. back_back_file()
  67. {
  68. while read line
  69. do
  70. R=$(echo $line | sed -e "s/ .*//g")
  71. echo $R
  72. cp $1/$R $2
  73. done < ./list_file/the_file
  74. }
  75. find_not_exist()
  76. {
  77. ls $1 > ./list_file/file_list
  78. ls $2 > ./list_file/back_list
  79. diff ./list_file/file_list ./list_file/back_list > ./list_file/diff_file
  80. sed -n "/</p" ./list_file/diff_file > ./list_file/file
  81. sed -e "s/< //g" ./list_file/file > ./list_file/the_file
  82. while read line
  83. do
  84. R=$(echo $line)
  85. echo $R
  86. cp -rf $1/$R $2
  87. done < ./list_file/the_file
  88. }
  89. judge_your_input $1 $2
  90. clear_record
  91. find_change_file $1 $2
  92. back_back_file $1 $2
  93. find_not_exist $1 $2