自动备份项目
- 具备配置文件
- 每天定时更新,并详细记录日志
- 对文件压缩
- 保留3天的备份数据
配置文件
- 包含需要备份的路径形式:path1:path2:path3
- 目标目录:打包到的安全目录
#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