代码语言:javascript复制
#!/bin/bash
base_dir=/data/Mysql-BackCenter
delete_day=30
delete_log_dir=/data/log/delete
download_dir=$base_dir/ZZB-7
vardate=$(date %Y%m%d)
datebefore=$(date -d "1 days ago" %Y%m%d)
cdmdump=$base_dir/cdb_dump_tool
[[ -d $delete_log_dir ]] || mkdir -p $delete_log_dir
print_to_log(){
echo "[ $(date " %Y-%m-%d %H:%M:%S") ] $1" >> $delete_log_dir/delete.log
}
print_log(){
echo "[ $(date " %Y-%m-%d %H:%M:%S") ] $1" >> $delete_log_dir/download.log
}
function delete_file(){
delete_total_num=$(find $base_dir -type f -mtime $delete_day |grep "tar.gz$"|wc -l)
print_to_log "开始删除$delete_day天前的数据压缩包..."
print_to_log "删除数量:$delete_total_num"
if [[ $delete_total_num -ne 0 ]];then
for i in $(find $base_dir -type f -mtime $delete_day |grep "tar.gz$")
do
print_to_log "开始删除: $i"
rm -f $i
if [[ $? -eq 0 ]];then
print_to_log "删除$i,成功!"
else
print_to_log "删除$i,失败...!"
fi
done
else
print_to_log "$delete_day天前的历史压缩数据不存在..."
fi
print_to_log "删除结束."
}
function download_sql(){
cd $base_dir
[[ -d $download_dir/$vardate ]] || mkdir -p $download_dir/$vardate
$cdmdump APPID 实例名称 $datebefore $download_dir/$vardate
if [[ $? -eq 0 ]];then
print_log "Download 实例名称 $datebefore 成功!"
download_file_size=$(du -sh $download_dir/$vardate |awk '{print $1}')
print_log "$download_dir/$vardate 源目录大小: $download_file_size"
cd $base_dir/$download_dir
tar -zcf $vardate.tar.gz $vardate
rm -fr $download_dir/$vardate
else
print_log "Download 实例名称 $datebefore 失败!"
fi
}
delete_file
download_sql