@cyysu
2017-10-17T08:35:13.000000Z
字数 3345
阅读 716
- 时间: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 directory
backupDir=/var/log
destDir=/opt/CacheUSB
countFile=/opt/.num.txt
device1="sdb1"
device2="sdc1"
device3="sdc1"
device4="sda1"
SD1="mmcblk0"
SD2="mmcblk1"
SD3="mmcblk2"
SD4="mmcblk4"
sdDir=/media/CacheSD
DATE=`date +"%Y-%m-%d"`
size1=100000
ISSD=0
ISUSB=0
# find usb device
findUSB(){
for device in `ls /dev/sd* | cut -d "/" -f 3`
do
if [ $? -eq 0 ];then
if [[ $device == $device2 || $device == $device1 || $device == $device3 || $device == $device4 ]];then
ISUSB=1
if [ -e $destDir ];then
umount -fl $destDir
mount /dev/$device $destDir
echo -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 device
mkdir $destDir && mount /dev/$device $destDir
echo -e "\033[41m `date`: Create directory succes and mount usb device succes!!!\033[0m"
fi
fi
fi
done
}
# backup files to SD card
findSD(){
for sd in `ls /dev/mmcblk* | cut -d "/" -f3`
do
if [ $? -eq 0 ];then
if [[ $sd == $SD1 || $sd == $SD2 || $sd == $SD3 || $sd == $SD4 ]];then
ISSD=1
if [ -e $sdDir ];then
umount -fl $sdDir
mount /dev/$sd $sdDir
echo -e "\033[32m `date`: mount sd device succes!!!\033[0m"
else
mkdir $sdDir && mount /dev/$sd $sdDir
echo -e "\033[32m `date`: Create directory succes and mount sd device succes!!!\033[0m"
fi
fi
fi
done
}
#statistic folders size
FolderSize(){
folderSize=`du -s ${backup} | cut -d "/" -f1 | cut -d "." -f1`
echo $folderSize
#test $folderSize -gt $size1 && echo yes
if [ $folderSize -gt 150000 ];then
echo -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 destination
CopyFilesFromSource(){
cd $backupDir
# package file to the destination directory and check if the log size is larger than 100M
if [ $ISUSB -eq 1 ];then
tar cvf $destDir/$DATE.tar ./*
else
tar cvf $sdDir/$DATE.tar ./*
fi
sync
sync
sync
}
# main function
main(){
# find device
findUSB
# check the system if exist usb device or sd device
if [ $ISUSB -eq 1 ];then
echo -e "\033[32m `date`: usb device probe succes!!! System will not probe SD device!!! \033[0m"
else
echo -e "\033[41m `date`: usb device probe failed!!! System will probe SD device!!! \033[0m"
findSD
fi
# if usb exist and run this code funciton
if [ $ISUSB -eq 1 ];then
if [[ -d $destDir && $? -eq 0 ]];then
# statistic run count and rename the backup file's name
#cd $destDir && echo "1" >> $countFile
cd $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 directory
CopyFilesFromSource
# because the backup is complete. if the scripts check the size is larger than 100M will delete /var/log/ all files
FolderSize
fi
fi
# if sd device exist and run this code funciton
if [ $ISSD -eq 1 ];then
if [[ -d $sdDir && $? -eq 0 ]];then
CopyFilesFromSource
FolderSize
fi
fi
}
# run start here
case "$1" in
start)
main
;;
stop)
;;
status)
echo $?
;;
*)
echo "Usage autostart.sh [start|stop]" >&2
exit 3
esac
此脚本实现了U盘和SD卡进行备份,详细的内容可以在脚本里面的注释。
支付宝 微信