【Linux Shell脚本编程】自动备份与删除历史备份脚本

tanglu Shell 2015-07-16 4296 0

脚本说明:

把需要备份的目录或文件写在project.list文件中,一行一个;

脚本通过for循环来读取project.list文件,对每行文件进行tar压缩;

根据备份日期作为文件名;

相关操作都会记录到日志文件中;

自动删除备份时间超过30天的文件。 

#!/bin/bash
#author: TangLu
#date: 2015-07-16
#role: backup
##################################################
BAKDIR=/backup
BAKLOGDIR=/backup/log
LOGFILE=/backup/log/bak.`date +"%Y-%m-%d"`.log
##################################################
 [ ! -d $BAKDIR ] &&  mkdir $BAKDIR
 [ ! -d $BAKLOGDIR ] && mkdir $BAKLOGDIR

echo "--------------------------------------------------" >> $LOGFILE
echo "!!!BACKUP START!!! at `date +"%Y-%m-%d %H:%M:%S"`" >> $LOGFILE
echo "--------------------------------------------------" >> $LOGFILE
echo " " >> $LOGFILE
cd $BAKDIR

for i in `cat /shell/project.list`
 do
   BAKFILE=$BAKDIR/$i.`date +"%Y-%m-%d"`.tar.gz
 if [ -f $BAKFILE ]
    then
      echo "backup file $BAKFILE is exist !" >>$LOGFILE
    else
      tar -zcf $BAKFILE $i  > /dev/null 2>&1
      echo "backup $i done into $BAKFILE ">> $LOGFILE
      echo " " >> $LOGFILE
 fi

 OLDFILE=$BAKDIR/$i.`date --date="30 days ago" +"%Y-%m-%d"`.tar.gz
 if [ -f $OLDFILE ]
    then
        rm -rf $OLDFILE
        echo "Delete old files success!" >> $LOGFILE
 fi
 done

echo "--------------------------------------------------" >> $LOGFILE
echo "!!!BACKUP END!!! at `date +"%Y-%m-%d %H:%M:%S"`" >> $LOGFILE
echo "--------------------------------------------------" >> $LOGFILE


 

版权声明
本站所有文章均为原创,转载请注明出处!小站维护不易,如果对您有所帮助,希望能点击一下站内广告,谢谢!

评论