@cyysu
2017-10-17T08:35:13.000000Z
字数 3345
阅读 778
- 时间:2017年9月23日
- 作者:MJ_DZ chen yuan
- 邮箱:chenyuan@ypmingjiang.cn
- 版本:4.0
- 描述:此脚本完成SD卡和U盘的备份。
脚本编写
#!/bin/bash#==============================================================================# Author : MJ_DZ chenyuan## Email : cyysu.github.io@gmail.com## Last modified : 2017-08-27 15:53## Filename : backup.sh## Description : Backup /var/log/ files to the USB device and this Script# will start at 6:00 pm to backup today log file. The script# will backup All week and generate (1-7).tat.gz in the USB# device. when the total log files greater than 100M,this# scripts will delete the gateway's log files after backup# this time.In the future ,we can submit this backup to our# server.#==============================================================================# make sure the backup directorybackupDir=/var/logdestDir=/opt/CacheUSBcountFile=/opt/.num.txtdevice1="sdb1"device2="sdc1"device3="sdc1"device4="sda1"SD1="mmcblk0"SD2="mmcblk1"SD3="mmcblk2"SD4="mmcblk4"sdDir=/media/CacheSDDATE=`date +"%Y-%m-%d"`size1=100000ISSD=0ISUSB=0# find usb devicefindUSB(){for device in `ls /dev/sd* | cut -d "/" -f 3`doif [ $? -eq 0 ];thenif [[ $device == $device2 || $device == $device1 || $device == $device3 || $device == $device4 ]];thenISUSB=1if [ -e $destDir ];thenumount -fl $destDirmount /dev/$device $destDirecho -e "\033[41m `date`: mount usb device succes!!!\033[0m"else# device is find and the directory is not exist so create directory to mount devicemkdir $destDir && mount /dev/$device $destDirecho -e "\033[41m `date`: Create directory succes and mount usb device succes!!!\033[0m"fififidone}# backup files to SD cardfindSD(){for sd in `ls /dev/mmcblk* | cut -d "/" -f3`doif [ $? -eq 0 ];thenif [[ $sd == $SD1 || $sd == $SD2 || $sd == $SD3 || $sd == $SD4 ]];thenISSD=1if [ -e $sdDir ];thenumount -fl $sdDirmount /dev/$sd $sdDirecho -e "\033[32m `date`: mount sd device succes!!!\033[0m"elsemkdir $sdDir && mount /dev/$sd $sdDirecho -e "\033[32m `date`: Create directory succes and mount sd device succes!!!\033[0m"fififidone}#statistic folders sizeFolderSize(){folderSize=`du -s ${backup} | cut -d "/" -f1 | cut -d "." -f1`echo $folderSize#test $folderSize -gt $size1 && echo yesif [ $folderSize -gt 150000 ];thenecho -e "\033[41m `date`: The log folder's size is larger than 100M!!! The scripts will delete the log files \033[0m"#rm -rf $backupDir/*fi}# copy file from source to the destinationCopyFilesFromSource(){cd $backupDir# package file to the destination directory and check if the log size is larger than 100Mif [ $ISUSB -eq 1 ];thentar cvf $destDir/$DATE.tar ./*elsetar cvf $sdDir/$DATE.tar ./*fisyncsyncsync}# main functionmain(){# find devicefindUSB# check the system if exist usb device or sd deviceif [ $ISUSB -eq 1 ];thenecho -e "\033[32m `date`: usb device probe succes!!! System will not probe SD device!!! \033[0m"elseecho -e "\033[41m `date`: usb device probe failed!!! System will probe SD device!!! \033[0m"findSDfi# if usb exist and run this code funcitonif [ $ISUSB -eq 1 ];thenif [[ -d $destDir && $? -eq 0 ]];then# statistic run count and rename the backup file's name#cd $destDir && echo "1" >> $countFilecd $destDir#BackupFileName=`awk '{print NR}' ${countFile} | tail -n1`#if [ ${BackupFileName} -eq 7 ];then# cat /dev/null > $countFile#fi#BackupFileName=$DATE#export BackupFileName# copy backup files current directoryCopyFilesFromSource# because the backup is complete. if the scripts check the size is larger than 100M will delete /var/log/ all filesFolderSizefifi# if sd device exist and run this code funcitonif [ $ISSD -eq 1 ];thenif [[ -d $sdDir && $? -eq 0 ]];thenCopyFilesFromSourceFolderSizefifi}# run start herecase "$1" instart)main;;stop);;status)echo $?;;*)echo "Usage autostart.sh [start|stop]" >&2exit 3esac
此脚本实现了U盘和SD卡进行备份,详细的内容可以在脚本里面的注释。
支付宝 微信