运用tar -g参数对LINUX的指定目录进行全量和增量备份的脚本

[root@V71 13]# cat /shell/bak.sh 
#!/bin/bash
if [ -z "$*" ];then                                                   #提供给脚本的参数长度为零,提示输入备份的目录
    echo -e "Pls enter your backup directories\n"
    exit
fi
bakdir=/backup                                                    #存放备份的路径
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
WEEK=`date +%u`
fielname1=bak.tar.gz
sectrue=$?

if [ ! -d $bakdir/$YEAR/$MONTH/$DAY ];then
    mkdir -p $bakdir/$YEAR/$MONTH/$DAY
    echo -e "created dir "$bakdir/$YEAR/$MONTH/$DAY"\n"
fi

 

#全量备份的函数,计划周六执行全量备份

function fullbak(){
if [ "$WEEK" -eq "6" ];then
    rm -rf $bakdir/snapshot$1
        time1=`date '+%Y%m%d%H%M%S'`
    cd $bakdir/$YEAR/$MONTH/$DAY ;tar -g $bakdir/snapshot$1 -czvf $1$fielname1 $2
    [ "$sectrue" == "0" ] && echo -e $time1" full backup successfully " $2"\n" 
fi
}

 

#增量备份的函数,计划除了周六执行增量备份

function inbak(){
if [ "$WEEK" -ne "6" ];then
        time1=`date '+%Y%m%d%H%M%S'`
    cd $bakdir/$YEAR/$MONTH/$DAY ;tar -g $bakdir/snapshot$1 -czvf $1$time1$fielname1 $2
    [ "$sectrue" == "0" ] && echo -e  $time1" backup successfully " $2"\n" 
fi
}


alldirs=$*
for dirs in ${alldirs[@]}                      #使用FOR循环并调用全量和增量备份的函数对脚本所有的参数,也就是目录进行备份
do
basename1=`basename $dirs`       #待备份目录的basename必须在全部参数当中唯一,否则将覆盖前面的备份
fullbak $basename1 $dirs              #向函数提供$1和$2的参数,用于识别不同的备份目录,此$1,$2为函数的参数,不同于脚本的参数
inbak $basename1 $dirs

done

 

再放到每天的计划任务执行即可

[root@V71 13]# crontab -e

12 10 * * * sh /shell/bak.sh /201810 /loggz >> /tmp/baklog.txt

 

备份完成的情况:

[root@V71 13]# cat /tmp/baklog.txt 
/201810/
20191013101201 backup successfully  /201810

/loggz/
/loggz/192.168.156.71192.168.122.1/
/loggz/192.168.156.71192.168.122.1/tomcat/
20191013101201 backup successfully  /loggz

你可能感兴趣的:(运用tar -g参数对LINUX的指定目录进行全量和增量备份的脚本)