[Auto]Copy và gửi email đến cho administrator khi có những file mới trong thư mục

Mình có một file python 2 viết đã lâu rồi trong Centos, có nhiệm vụ là kiểm tra một thư mục chung nếu thư mục ấy có những file mới sẽ gửi kèm đến email cho administrator. Có vài hàm check thư mục, thư mục con, check thời gian của file. Cách chạy là
./checkFile.py thu_muc_nao_do
Sau đấy đưa vào crontab để check hàng ngày.

from datetime import datetime
import zipfile  
import sys
import os
import shutil
import stat
import time

TO="Mymail"
def sendMailx(zipF):
    cmd = """echo "{b}" | mailx -s "{s}" -a "{f1}" "{to}"  2>/dev/null""".format(
        b="send", s=SUBJECT, f1=zipF, to=TO
    )
    # send mail
    os.system(cmd)


filePath=sys.argv[1]    
current_time = datetime.now()
tmp_dateformat =  str(current_time.strftime('%m%d%Y'))
tmp_dateformatnew =  str(current_time.strftime('%m-%d-%y %T'))
SUBJECT = "Send attach file -"+tmp_dateformatnew
current_zipfile = "my_sendmail_files" + '_' + tmp_dateformat  
cfd = str(os.getcwd())
now_time = time.time()
#check folder empty
dirContents = os.listdir(filePath)
if len(dirContents) == 0:
    print('Folder is Empty. Nothing to do!')
    sys.exit(0)

current_zipfile = cfd + '/' + current_zipfile
os.mkdir(current_zipfile)

tmp_num = -1

for tmp_N in range(101):
    if tmp_num != -1:
        break
    for folderName, subfolders, filenames in os.walk(filePath):
        if tmp_num != -1:
            break
        for filename in filenames:
            current_file = ''
            #tmp_zip_file = ''
            current_file = folderName + '/'+ filename
            fileStatsObj = os.stat ( current_file )
            current_time = fileStatsObj[stat.ST_ATIME]
            m_time =  fileStatsObj[stat.ST_MTIME]
            if round((now_time - m_time)/(24*3600)) == tmp_N:
                tmp_num = tmp_N
                break

if tmp_num < 0:
    print('Folder has not any file. Nothing to do!')
    shutil.rmtree(current_zipfile)
    sys.exit(0)
    
    
for folderName, subfolders, filenames in os.walk(filePath):
    for filename in filenames:
        current_file = ''
        #tmp_zip_file = ''
        current_file = folderName + '/'+ filename
        fileStatsObj = os.stat ( current_file )
        current_time = fileStatsObj[stat.ST_ATIME]
        m_time =  fileStatsObj[stat.ST_MTIME]
        if round((now_time - m_time)/(24*3600)) == tmp_num:
            #print current_file
            #print tmp_num
            shutil.copy(current_file, current_zipfile)
            os.utime(current_file, (current_time, m_time))
            
#check folder empty
#dirC = os.listdir(current_zipfile)
#if len(dirC) == 0:
#    print('Folder has not any file. Nothing to do!')
#    shutil.rmtree(current_zipfile)
#    sys.exit(0)

shutil.make_archive(current_zipfile, 'zip', current_zipfile)
current_zipfile_z = current_zipfile + ".zip"
print current_zipfile_z
if tmp_num == 0:
    print "zip file contains all files are in current date!"
else:
    print "zip file contains all files are in " + str(tmp_num)+" days !"

#send mail
sendMailx(current_zipfile_z)
os.remove(current_zipfile_z)
shutil.rmtree(current_zipfile)
2 Likes