亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python檢查 云備份進程是否正常運行代碼實例

系統(tǒng) 1756 0

這篇文章主要介紹了Python檢查 云備份進程是否正常運行代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下

場景:服務(wù)器自動備份數(shù)據(jù)庫文件,每兩小時生成一個新備份文件,通過云備份客戶端自動上傳,需要每天檢查是否備份成功。

實現(xiàn):本腳本實現(xiàn)檢查文件是否備份成功,進程是否正常運行,并且發(fā)送相關(guān)郵件提醒。

            
#! /usr/bin/env python
import os
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header 
from configparser import ConfigParser 
def SendMail(server,sender,pwd,receiver,msg):
  '''
  Conncet to Office365 mail server and sent emails
   
  '''
  email = smtplib.SMTP(server,587)
  email.starttls()
  email.ehlo(server)
  email.login(sender,pwd)
  email.sendmail(sender,receiver,msg)
  email.quit()     
def GetNewFiles(path,num):
  '''
  Get file lists and return the last num created files   
  '''
  lists = os.listdir(path)
  lists.sort(key=lambda fn:os.path.getctime(path+'\\'+fn))   
  return lists[-num : ]   
def CheckProcess(name):
  '''
  Check if the process exits and return result.
   
  ['\n', 'Image Name           PID Session Name    Session#  Mem Usage\n', '========================= ======== ================ =========== ============\n', 'Dropbox.exe         20484 Console          1   71,652 K\n', 'Dropbox.exe         23232 Console          1   2,456 K\n', 'Dropbox.exe         61120 Console          1   2,168 K\n']
  
  '''
  proc = []
  p = os.popen('tasklist /FI "IMAGENAME eq %s"' % name)
  for x in p:
    proc.append(x)
  p.close()
  return proc
   
def MailContent(path,num):
  '''
  make the mail contents
  '''
  content = []
   
  dropbox = CheckProcess('dropbox.exe')
  carboniteservice = CheckProcess('carboniteservice.exe')
   
  #IF process doesn't run
  if len(dropbox) < 2 or len(carboniteservice) < 2 :
    content.append("Dropbox or CarBonite doesn't run")
    s = '\n\t'.join(dropbox) + '\n\n' + '\n\t'.join(carboniteservice)
    content.append("Process Check Result:\n\t" + s)
    return content
   
  #Check if the backup files are correct.
  files = GetNewFiles(path,num)
  file_ctime = os.path.getctime(path + '\\' + files[0])
  now = time.time() - 86400
   
  if file_ctime > now :
    content.append("DB Backup Successfull")
    body = "\nThe Backup files are:\n\t" + '\n\t'.join(files)
    content.append(body)
    return content
  else :
    content.append("DB Backup Failed")
    body = "\nThe last backup sucessfull file is " + files[-1]
    content.append(body)
    return content  
def main():
   
  #server = 'smtp.office365.com'
  #sender = 'online@netbraintech.com'
  #receiver = ['gavin.yuan@netbraintech.com' , 'feng.liu@netbraintech.com']
  #pwd = 'Netbrain12'
   
  config = ConfigParser()
  config.read_file(open('config.ini'))
  path = config.get('os', 'path')
  receiver = config.get('email', 'receiver')
  server = config.get('email', 'server')
  sender = config.get('email', 'sender')
  pwd = config.get('email', 'pwd')   
  content = MailContent(path,12)
  #content = MailContent("D:\\test",6)
  mail_content = content[1]   
  msg = MIMEText(mail_content, "plain", "utf-8")
  msg["Subject"] = Header(content[0], "utf-8")
  msg["From"] = sender
  msg["To"] = Header(receiver)   
  SendMail(server,sender,pwd,receiver.split(','),msg.as_string()) 
if __name__ == '__main__':
  main()
          

ini配置文件內(nèi)容

            
[os]
path=D:\test
[email]
server=smtp.office365.com
sender=xxxx@outlook.com
pwd=xxxxx
receiver=xx@outlook.com,xxxxx@gmail.com
          

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91视频国产高清 | 成人一级 | jizzjizz中国护士第一次 | 九九伦理影院手机观看 | 西西做人爱免费视频 | 欧美国产综合在线 | 九九精品久久 | 99久久精品6在线播放 | 女孕学护士一级毛片 | 色综合中文字幕天天在线 | 亚洲三级中文字幕 | 日本欧美强乱视频在线 | 狠狠ai| 天天操天天射天天舔 | 国产精品videosse | 久久成人免费观看草草影院 | 九九成人| 亚洲产在线精品第一站不卡 | 一级一级毛片看看 | 毛片一级在线 | 国产亚洲美女精品久久久 | 国产色婷婷 | 成人在线免费视频播放 | 午夜影院欧美 | 草的爽免费视频 | 国产亚洲精品美女 | 高清欧美日本视频免费观看 | 2020国产成人免费视频 | 九九视频网站 | 亚洲成a人片77777kkk | 国内精品视频在线观看 | 日韩中文字幕精品久久 | 一级激情视频 | 狠狠色丁香婷婷综合精品视频 | 免费一级真人毛片 | 久久综合99re88久久爱 | 九九精品免费 | 欧美成人欧美激情欧美风情 | 午夜视频在线网站 | 91久久亚洲最新一本 | 国产午夜永久福利视频在线观看 |