[关闭]
@cyysu 2017-10-17T08:35:13.000000Z 字数 3345 阅读 716

日志备份脚本说明文档

  • 时间:2017年9月23日
  • 作者:MJ_DZ chen yuan
  • 邮箱:chenyuan@ypmingjiang.cn
  • 版本:4.0
  • 描述:此脚本完成SD卡和U盘的备份。

脚本编写


脚本内容

  1. #!/bin/bash
  2. #==============================================================================
  3. # Author : MJ_DZ chenyuan
  4. #
  5. # Email : cyysu.github.io@gmail.com
  6. #
  7. # Last modified : 2017-08-27 15:53
  8. #
  9. # Filename : backup.sh
  10. #
  11. # Description : Backup /var/log/ files to the USB device and this Script
  12. # will start at 6:00 pm to backup today log file. The script
  13. # will backup All week and generate (1-7).tat.gz in the USB
  14. # device. when the total log files greater than 100M,this
  15. # scripts will delete the gateway's log files after backup
  16. # this time.In the future ,we can submit this backup to our
  17. # server.
  18. #==============================================================================
  19. # make sure the backup directory
  20. backupDir=/var/log
  21. destDir=/opt/CacheUSB
  22. countFile=/opt/.num.txt
  23. device1="sdb1"
  24. device2="sdc1"
  25. device3="sdc1"
  26. device4="sda1"
  27. SD1="mmcblk0"
  28. SD2="mmcblk1"
  29. SD3="mmcblk2"
  30. SD4="mmcblk4"
  31. sdDir=/media/CacheSD
  32. DATE=`date +"%Y-%m-%d"`
  33. size1=100000
  34. ISSD=0
  35. ISUSB=0
  36. # find usb device
  37. findUSB(){
  38. for device in `ls /dev/sd* | cut -d "/" -f 3`
  39. do
  40. if [ $? -eq 0 ];then
  41. if [[ $device == $device2 || $device == $device1 || $device == $device3 || $device == $device4 ]];then
  42. ISUSB=1
  43. if [ -e $destDir ];then
  44. umount -fl $destDir
  45. mount /dev/$device $destDir
  46. echo -e "\033[41m `date`: mount usb device succes!!!\033[0m"
  47. else
  48. # device is find and the directory is not exist so create directory to mount device
  49. mkdir $destDir && mount /dev/$device $destDir
  50. echo -e "\033[41m `date`: Create directory succes and mount usb device succes!!!\033[0m"
  51. fi
  52. fi
  53. fi
  54. done
  55. }
  56. # backup files to SD card
  57. findSD(){
  58. for sd in `ls /dev/mmcblk* | cut -d "/" -f3`
  59. do
  60. if [ $? -eq 0 ];then
  61. if [[ $sd == $SD1 || $sd == $SD2 || $sd == $SD3 || $sd == $SD4 ]];then
  62. ISSD=1
  63. if [ -e $sdDir ];then
  64. umount -fl $sdDir
  65. mount /dev/$sd $sdDir
  66. echo -e "\033[32m `date`: mount sd device succes!!!\033[0m"
  67. else
  68. mkdir $sdDir && mount /dev/$sd $sdDir
  69. echo -e "\033[32m `date`: Create directory succes and mount sd device succes!!!\033[0m"
  70. fi
  71. fi
  72. fi
  73. done
  74. }
  75. #statistic folders size
  76. FolderSize(){
  77. folderSize=`du -s ${backup} | cut -d "/" -f1 | cut -d "." -f1`
  78. echo $folderSize
  79. #test $folderSize -gt $size1 && echo yes
  80. if [ $folderSize -gt 150000 ];then
  81. echo -e "\033[41m `date`: The log folder's size is larger than 100M!!! The scripts will delete the log files \033[0m"
  82. #rm -rf $backupDir/*
  83. fi
  84. }
  85. # copy file from source to the destination
  86. CopyFilesFromSource(){
  87. cd $backupDir
  88. # package file to the destination directory and check if the log size is larger than 100M
  89. if [ $ISUSB -eq 1 ];then
  90. tar cvf $destDir/$DATE.tar ./*
  91. else
  92. tar cvf $sdDir/$DATE.tar ./*
  93. fi
  94. sync
  95. sync
  96. sync
  97. }
  98. # main function
  99. main(){
  100. # find device
  101. findUSB
  102. # check the system if exist usb device or sd device
  103. if [ $ISUSB -eq 1 ];then
  104. echo -e "\033[32m `date`: usb device probe succes!!! System will not probe SD device!!! \033[0m"
  105. else
  106. echo -e "\033[41m `date`: usb device probe failed!!! System will probe SD device!!! \033[0m"
  107. findSD
  108. fi
  109. # if usb exist and run this code funciton
  110. if [ $ISUSB -eq 1 ];then
  111. if [[ -d $destDir && $? -eq 0 ]];then
  112. # statistic run count and rename the backup file's name
  113. #cd $destDir && echo "1" >> $countFile
  114. cd $destDir
  115. #BackupFileName=`awk '{print NR}' ${countFile} | tail -n1`
  116. #if [ ${BackupFileName} -eq 7 ];then
  117. # cat /dev/null > $countFile
  118. #fi
  119. #BackupFileName=$DATE
  120. #export BackupFileName
  121. # copy backup files current directory
  122. CopyFilesFromSource
  123. # because the backup is complete. if the scripts check the size is larger than 100M will delete /var/log/ all files
  124. FolderSize
  125. fi
  126. fi
  127. # if sd device exist and run this code funciton
  128. if [ $ISSD -eq 1 ];then
  129. if [[ -d $sdDir && $? -eq 0 ]];then
  130. CopyFilesFromSource
  131. FolderSize
  132. fi
  133. fi
  134. }
  135. # run start here
  136. case "$1" in
  137. start)
  138. main
  139. ;;
  140. stop)
  141. ;;
  142. status)
  143. echo $?
  144. ;;
  145. *)
  146. echo "Usage autostart.sh [start|stop]" >&2
  147. exit 3
  148. esac

脚本说明

  1. 此脚本实现了U盘和SD卡进行备份,详细的内容可以在脚本里面的注释。

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注