Took Python and wrote a backup result monitoring program

Source: Internet
Author: User

1. Background notes

A backup solution was developed for the company in the previous period

Need to be informed every day whether the backup is OK

2. Process

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/92/CA/wKioL1kC_F6xWnhLAABYFnavzrY840.png "title=" 2017-04-28_161618.png "alt=" Wkiol1kc_f6xwnhlaabyfnavzry840.png "/>

3. Code
#!/usr/bin/env python# -*- coding: utf-8 -*-import smtplibimport  Email.mime.multipartimport email.mime.textimport timeimport subprocessdef mailalter (User, Receivers,subject,servername,status):     msg=email.mime.multipart.mimemultipart ()      msg[' from ']= ' [email protected] '     msg[' to ']= user     msg[' subject ']= subject    content =  ' Dear ITers:     %s bakcup status %s# #Powered  by voidyao000### #Life  is  short,you need python!##     "% (servername,status)     txt= Email.mime.text.MIMEText (content)     msg.attach (TXT)     smtp=smtplib     smtp=smtplib. SMTP ()     smtp.connect (' *.*.*.* ', ' + ')      #注意点: Receivers here are list charactersString     smtp.sendmail (' [email protected] ', RECEIVERS,STR (msg))      Smtp.quit () Def chkbak (servername):    user =  ' [Email protected],[email  protected] '     receivers = [' [email protected] ', ' [email  Protected] ',]    subjectok =  '%s %sbackup ok '% (Time.strftime ("%Y-%m-%d "), servername)     subjectfail =  '%s %sbackup fail '% (Time.strftime ("%y- %m-%d "), servername)     bakdate = time.strftime ("%y%m%d ")      logfile =  '/home/%sbackup/%sbak%s.log '  % (servername,servername,bakdate)      cmd =  ' tail -1 %s '  % (logfile)     cmd_call = subprocess. Popen (cmd,shell=true,stdout=subprocess. PIPE)     cmd_result = str (Cmd_call.stdout.read (), ' UTF8 '). Strip()     chk_line =  '%s backup sucessful '% (servername)      if chk_line ==cmd_result:        status =  ' Ok '         print (' Backup ok ')          mailalter (User,receivers,subjectok,servername, "OK")     else:         status =  ' fail '         print (" Backup fail ")         mailater (User,receivers,subjectfail, ServerName, "Fail") Chkbak ("servername")


This article is from the "Open World" blog, so be sure to keep this source http://voidyao000.blog.51cto.com/12458042/1920521

Took Python and wrote a backup result monitoring program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.