脚本说明:
把需要备份的目录或文件写在project.list文件中,一行一个;
脚本通过for循环来读取project.list文件,对每行文件进行tar压缩;
根据备份日期作为文件名;
相关操作都会记录到日志文件中;
自动删除备份时间超过30天的文件。
01
|
#!/bin/bash
|
02
|
#author: TangLu
|
03
|
#date: 2015-07-16
|
04
|
#role: backup
|
05
|
##################################################
|
06
|
BAKDIR=/backup
|
07
|
BAKLOGDIR=/backup/log
|
08
|
LOGFILE=/backup/log/bak.` date + "%Y-%m-%d" `.log
|
09
|
##################################################
|
10
|
[ ! -d $BAKDIR ] && mkdir $BAKDIR
|
11
|
[ ! -d $BAKLOGDIR ] && mkdir $BAKLOGDIR
|
12
|
13
|
echo "--------------------------------------------------" >> $LOGFILE
|
14
|
echo "!!!BACKUP START!!! at `date +" %Y-%m-%d %H:%M:%S "`" >> $LOGFILE
|
15
|
echo "--------------------------------------------------" >> $LOGFILE
|
16
|
echo " " >> $LOGFILE
|
17
|
cd $BAKDIR
|
18
|
19
|
for i in ` cat /shell/project.list`
|
20
|
do
|
21
|
BAKFILE=$BAKDIR/$i.` date + "%Y-%m-%d" `. tar .gz
|
22
|
if [ -f $BAKFILE ]
|
23
|
then
|
24
|
echo "backup file $BAKFILE is exist !" >>$LOGFILE
|
25
|
else
|
26
|
tar -zcf $BAKFILE $i > /dev/null 2>&1
|
27
|
echo "backup $i done into $BAKFILE " >> $LOGFILE
|
28
|
echo " " >> $LOGFILE
|
29
|
fi
|
30
|
31
|
OLDFILE=$BAKDIR/$i.` date -- date = "30 days ago" + "%Y-%m-%d" `. tar .gz
|
32
|
if [ -f $OLDFILE ]
|
33
|
then
|
34
|
rm -rf $OLDFILE
|
35
|
echo "Delete old files success!" >> $LOGFILE
|
36
|
fi
|
37
|
done
|
38
|
39
|
echo "--------------------------------------------------" >> $LOGFILE
|
40
|
echo "!!!BACKUP END!!! at `date +" %Y-%m-%d %H:%M:%S "`" >> $LOGFILE
|
41
|
echo "--------------------------------------------------" >> $LOGFILE
|