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


    最近公司需求,需要监控一个远程服务器的目录是否有上传数据,因此写了这个脚本,又增加和删除,正好温习了一下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 "狒狒:更新通知 Content-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

  • 相关阅读:
    解决com.xpand.. starter-canal 依赖引入问题
    缓存预热加入二级缓存
    缓存预热的实现
    ShardingSphere 中有哪些分布式主键实现方式?
    ShardingSphere 如何实现系统的扩展性
    如何系统剖析 ShardingSphere 的代码结构?
    SharingSphere的数据脱敏
    ShardingSphere的分布式事务
    Qt 事件过滤器原理(installEventFilter函数)
    Qt Event 以及 Event Filter 事件处理
  • 原文地址:https://www.cnblogs.com/feiyuanxing/p/6265313.html
Copyright © 2020-2023  润新知