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