关于指定路径下目录文件的自动备份

自动备份项目

  1. 具备配置文件
  2. 每天定时更新,并详细记录日志
  3. 对文件压缩
  4. 保留3天的备份数据

配置文件

  1. 包含需要备份的路径形式:path1:path2:path3
  2. 目标目录:打包到的安全目录
#filename: .back.rc
#filepath:/home/xinyue/shell/.back.rc
backup_path=/home/xinyue/get:/home/xinyue/ceshi
dest_dir=/data
log=/var/log/backup.log
## 日志
   时间      操作    对象     在磁盘上的数据增加还是减少,多大,是否有错误信息,提示性信息等
2018-08-17  12:10  backup  /home/xinyue/cehi --> ERROR 权限错误
2018-08-17  12:10  backup  /home/xinyue/cehi --> home_xinyue_ceshi_2018_8_17.tar.gz +12K
2018-08-17  12:11  backup  home_xinyue_ceshi_2018_8_17.tar.gz --> remove -12K


错误日志记录操作
权限错误【ERROR】Permission denied继续执行其他
目标文件夹为空【ERROR】The Dest dir %s not exist结束
待备份文件夹不存在【ERROR】The dir %s not exist继续执行其他


代码:

#!/bin/bash

source /home/xinyue/shell/.back.rc

TIME_NOW=`date +"%Y-%m-%d %H:%M"`
TIME_FILE=`date +"%Y_%m_%d_%H_%M"`

if [[ ! -d $dest_dir ]];then
    echo "$TIME_NOW 【ERROR】 The Dest dir $dest_dir not exists" >> $log
    exit
fi

echo "$TIME_NOW backup started!" >> $log

for i in `echo $backup_path | tr ":" "\n"`;do
    if [[ ! -d $i ]];then
        echo "%TIME_NOW backup 【ERROR】 The dir $i not exist" >> $log
        continue
    fi
    echo "$i started"
    target=`echo $i | cut -d '/' -f "2-" | tr '/' '_'`
    target="${target}_${TIME_FILE}.tar.gz"
    tar -czPf ${dest_dir}/${target} $i
    ls -al  "${dest_dir}/${target}"
    if [[ $? -eq 0 ]];then
        size=`du -h ${dest_dir}/${target} | cut -f 1`
        echo "$TIME_NOW backup $i --> $target +${size}" >> $log
    else
        echo "$TIME_NOW 【ERROR】 Something .... " >> $log
    fi
done

Del_list=`find $dest_dir -name "*.tar.gz" -mmin +2`    #两分钟删除一次

for i in $Del_list;do
    size=`du -h $i | cut -f 1`
    rm -f $i
    echo "$TIME_NOW delete $i --> remove -${size}" >> $log
done


#### 关于定删除(corntab) 1.创建corntab
#path: ~/.bashrc
export EDITOR=vim

2.每隔一分钟备份一次
#定期删除(分 时 日 月 周 bash /路径)
#corntab -e
#sudo !!
*/1 * * * * bash /home/xinyue/shell/shell8.sh

还想关于corntab更详细的话 恩…. 自行百度吧 = =


​        这段代码设置的是每隔一分钟对文件目录进行一次备份,每两分钟删除一次(想要更改时间间隔也很容易),把该配置的目录文件都建立了,想要执行他要首先要赋予权限,在backup.log下执行以下代码:


tail -f backup.log



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值