备份并删除7天以前数据

需求:

有根据年月日时分创建的目录结构中数据需要按照每天进行备份,

原始目录

[root@LAB_1 logs]# cd /home/logs/2/2017/12/06/

备份目录

[root@LAB_1 bak]# cd /home/logs/bak/

备份脚本在备份目录中,计划任务中,每天凌晨1点备份并压缩7天之前的数据,并将老的文件目录给删除。

脚本如下:

#!/bin/bash

#7 days ago

S_Path=/home/logs/2

D_Path=/home/logs/bak

S_FileName=$(date -d "-7 days" "+%Y/%m/%d")

D_FileName=$(date -d "-7 days" "+%Y%m%d")

cd $S_Path/

tar -zcvf $D_Path/$D_FileName.tar.gz $S_FileName

if [ $? == 0 ];then

echo "back 7 days ago file sucessful!"

rm -rf $S_FileName

echo "delete 7 days ago file sucessful!"

else

echo "back 7 days ago file fail!"

fi

你可能感兴趣的:(备份并删除7天以前数据)