脚本说明:
把需要备份的目录或文件写在project.list文件中,一行一个;
脚本通过for循环来读取project.list文件,对每行文件进行tar压缩;
根据备份日期作为文件名;
相关操作都会记录到日志文件中;
自动删除备份时间超过30天的文件。
#!/bin/bash
#author: TangLu
#date: -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