通过电子邮件将文件上传到服务器时收到通知

时间:2016-06-07 07:48:57

标签: php linux server

我的服务器被黑了,我想要一个脚本或任何东西,以便在我的网站上传或修改任何文件时通过电子邮件收到通知 我在看 https://stackoverflow.com/questions/35196640/get-notified-when-a-file-got-uploaded-to-serverBest way to monitor file system changes in linux 但我不明白的方式 你可以一步一步地指导我吗?

1 个答案:

答案 0 :(得分:0)

我有一个简单的解决方案,它还可以将我自己的更改备份到CMS。它适用于我管理的所有网站(大约十几个,一些平面HTML,一些Joomla,一些Wordpress,在几个不同的主机上。这已经节省了我几十次,从用户错误("嗨,我更新了我的) wordpress模板,现在整个网站被破坏了#34;)服务器范围内的黑客攻击("亲爱的托管客户,我们的Plesk最近遭到黑客入侵,请更改所有密码并检查您网站的内容")。

唯一要求:您需要访问至少每天运行一次的Linux计算机。对我而言,它是我每天使用的桌面,但您可以在自己的Web服务器上运行它。

无论如何,这是:

  • lftp
  • 中将所有FTP网站设置为书签
  • 为您托管的每个网站(git init && git commit -m "first commit"
  • 设置本地git存储库
  • 确保cron正在运行(在大多数系统上),并且它可以通过电子邮件向您发送每个作业的结果(您可能需要将@ localhost重定向到您的公共电子邮件地址)
  • 将此行添加到crontab

51 03 * * * ~/bin/updateMirrors.sh

  • 并将此文件另存为〜/ bin / updateMirrors.sh

    #!/bin/bash    
    # step through the list of FTP bookmarks, mirror each one.
    # seems to take anywhere from 2 to 10 hours
    # cron should email the results to you@localhost
    
    while read SITE; 
      do NAME=`echo $SITE|cut -d' ' -f1`;
      echo $NAME `date` ;
      cd ~/$NAME/httpdocs;
      # if there's hackage, try this without the -X *-cache-* lines (someone might evilly install a trojan that looks like a cache file)
      lftp $NAME -e "mirror --verbose -X *-cache-*;quit"|grep -E "Transfer|Permission";
      git add . && git commit -m "updateMirrors.sh"
      echo ---------------------------------------------------------------------------
    done <~/.lftp/bookmarks
    
    ## if you find hackage, do this:
    ## git log --name-only
    ## git checkout [last uuid before hacking started]
    ## lftp and mirror -eR --exclude .git*
    

在过去的几年里,每一个早晨,我都会在收件箱中收到一封电子邮件,如下所示:

wecan.be Tuesday 29 November 05:15:59 AEDT 2016 On branch master nothing to commit, working directory clean
---------------------------------------------------------------------------
handyWarhols Tuesday 29 November 05:17:46 AEDT 2016 On branch master nothing to commit, working directory clean
---------------------------------------------------------------------------
colbourneave Tuesday 29 November 05:17:53 AEDT 2016 Transferring file `components/com_content/models/cache.db'
Transferring file `logs/error.php'
[git_head 657d5dc] updateMirrors.sh
 2 files changed, 141 insertions(+), 2 deletions(-)
---------------------------------------------------------------------------

您可以看到为一个站点更新了错误日志。如果其中一个网站被黑了,那很明显,因为有新文件签入(一旦我弄清楚它是如何发生的,我可以回滚)。每当我或任何其他人向任何网站添加内容,或者我更新插件或模板时,都会检入新文件。