Skip to content

Instantly share code, notes, and snippets.

@Mostafa-Hamdy-Elgiar
Created February 24, 2017 13:05
Show Gist options
  • Save Mostafa-Hamdy-Elgiar/18ff7b75f90b701317d625862ca7e78b to your computer and use it in GitHub Desktop.
Save Mostafa-Hamdy-Elgiar/18ff7b75f90b701317d625862ca7e78b to your computer and use it in GitHub Desktop.
This is a Python script to compare between the databases in Mysql Engine and the databases in backup solution, also send an E-mail about the databases not in the backup plan.
#!/usr/bin/python
#this script check the databases created in the Mysql engine and not in the backup script.
import os , commands , sys
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
DBs=os.popen('grep ^"DBNAMES" /opt/scripts/automysqlbackup.sh | cut -d"=" -f2') #Get all DBs in backup scripts
DBs=DBs.readlines()
DBs=DBs[0][1:-2]
mysqlDBs=os.popen('mysql -uroot -pxxxxx -e "show databases"')
mysqlDBs=mysqlDBs.readlines()
DBs=DBs.split(' ')
fromaddr="[email protected]"
toaddrs="[email protected]"
msg = MIMEMultipart()
msg['From']="[email protected]"
msg['To']="[email protected]"
msg['Subject']="Database backup check"
notrequire = ['Database' , 'information_schema' , 'mysql' , 'performance_schema' , 'test'] #this is a lines showed in show databases command and reqired in backup.
notback = []
strnotback = ''
noDBs = len(mysqlDBs)
#print noDBs
for i in range(noDBs) :
if mysqlDBs[i][0:-1] in DBs :
continue
elif mysqlDBs[i][0:-1] in notrequire :
continue
else :
notback.append(mysqlDBs[i][0:-1])
#print notback
if len(notback) > 0 :
print notback
print 'sending mail'
strnotback = ' , '.join(notback)
print strnotback
msg.attach(MIMEText('databases not in daily backup on server xxxxxxxxxx '+strnotback))
server = smtplib.SMTP('mail.mans.edu.eg',25)
server.starttls();
server.ehlo();
server.login('[email protected]','xxxxxxxxx')
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment