未来星开发团队的官网

【监控文件夹并将增加和删除的文件列表发送邮件完美脚本】-未来星开发开发团队

最近公司需求,需要监控一个远程服务器的目录是否有上传数据,因此写了这个脚本,又增加和删除,正好温习了一下md5sum 和awk命令,我太笨了,总是忘记。

记录一下:有任何问题联系我【Q9715234】

 

 

#!/bin/bash
#@file: wactch folds
#@author: feiyuanxing 【feiyuanxing@baidu.com】
#@date: 2016-12-13
#@version: 2.1.0

PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 监控的目录
DIR=/home/users/feiyuanxing/
# 日期变量
DATE=`date +%F_%H:%M`
# 临时文件
TMP_A=/home/users/feiyuanxing/script/bijiao/tmp/a.txt
TMP_B=/home/users/feiyuanxing/script/bijiao/tmp/b.txt
TMP_C=/home/users/feiyuanxing/script/bijiao/tmp/c.txt
# 发送的字符串
oneLog=/home/users/feiyuanxing/script/bijiao/log/oneLog.log
# 日志文件
LOG=/home/users/feiyuanxing/script/bijiao/log/foldchange.log

# 遍历指定目录下的文件大小及路径并重定向到日志文件
#find $DIR -print0 | xargs -0 du -sb > $TMP_B
find $DIR -type f -print0 | xargs -0 md5sum |sort -k2n |uniq > $TMP_B
# 比较目录变化,并将变化的文件写入日志
DIFFDEL=$(diff $TMP_B $TMP_A | grep "> " | sed 's/> //g')
DIFFADD=$(diff $TMP_A $TMP_B | grep "> " | sed 's/< //g')

if [[ -z "$DIFFADD" && -z "$DIFFDEL" ]] ; then
# echo "Nothing change" >> ${oneLog}
echo "Nothing change"
else
echo "Here is the change" >> ${oneLog}
echo "" >> $LOG
echo "增加的内容为:" >> ${oneLog}
echo "$DIFFADD" |tee $TMP_C >> ${oneLog}
echo "删除的内容为:" >> ${oneLog}
echo "$DIFFDEL" |tee $TMP_C >> ${oneLog}

if [ -s $TMP_C ];
then
echo "" >> ${oneLog}
echo "It modified at $DATE" ${oneLog}
echo "It modified at $DATE" >> ${oneLog}
echo ${oneLog} >> ${LOG}
mail -s "$(echo -e "狒狒:更新通知\nContent-Type: text/html")"  feiyuanxing@baidu.com < ${oneLog}
echo hhh`cat ${oneLog}`hhh
echo "发送邮件成功"

echo " " > ${oneLog}
# 将当前监控的目录结构覆盖为初始状态
find $DIR -type f -print0 | xargs -0 md5sum |sort -k2n |uniq > $TMP_A
# find $DIR -print0 | xargs -0 du -sb > $TMP_A
fi
fi
echo "====================================" >> $LOG
#清理临时文件
#rm -rf $TMP_B $TMP_C

posted @ 2017-01-09 15:03 费元星的博客 阅读(...) 评论(...) 编辑 收藏
未来星开发团队的官网