Created
April 16, 2013 19:01
-
-
Save maltzsama/5398616 to your computer and use it in GitHub Desktop.
MySQL Backup scrip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #-*- coding:utf-8 -*- | |
| ########################################### | |
| # mysqlbkp.py | |
| # Desc: backup script writting python to backup all MySQL databases. | |
| # Autor: Demetrius Albuquerque | |
| # email: demetrius.albuquerque@yahoo.com.br | |
| # versão: 0.1 data: 08/11/2012 | |
| ########################################### | |
| import os | |
| import time | |
| import socket | |
| import smtplib | |
| from email.MIMEMultipart import MIMEMultipart | |
| from email.MIMEBase import MIMEBase | |
| from email.MIMEText import MIMEText | |
| def mail(to, subject, text): | |
| msg = MIMEMultipart() | |
| msg['From'] = gmail_user | |
| msg['To'] = to | |
| msg['Subject'] = subject | |
| msg.attach(MIMEText(text)) | |
| mailServer = smtplib.SMTP("smtp.gmail.com", 587) | |
| mailServer.ehlo() | |
| mailServer.starttls() | |
| mailServer.ehlo() | |
| mailServer.login(gmail_user, gmail_pwd) | |
| mailServer.sendmail(gmail_user, to, msg.as_string()) | |
| mailServer.close() | |
| def getIp(): | |
| return socket.gethostbyname(socket.gethostname()) | |
| def setBackupDir(backupdir,hostname): | |
| basedir = backupdir + hostname + '/' | |
| return basedir | |
| def performBackup(username, password, hostname,filestamp,backupdir,receptor): | |
| database_list_command = "mysql -u %s -p%s -h %s --silent -N -e 'show databases;'" %(username, password, hostname) | |
| listDataBase = os.popen(database_list_command).readlines() | |
| listDBBkped = [] | |
| for database in listDataBase: | |
| database = database.strip() | |
| print database | |
| if database == 'information_schema' or database == 'performance_schema': | |
| continue | |
| else: | |
| listDBBkped.append(database) | |
| filename = "%s%s-%s.sql" % (backupdir,database, filestamp) | |
| os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s --routines --single-transaction| gzip -c > %s.gz" % (username, password, hostname, database, filename)) | |
| messagem = "This is a confirmation of backup from host: %s\nSaved at: %s\nDatabases: %s" %(hostname,backupdir, listDBBkped) | |
| subject = "Backup MySQL hostname %s"%hostname | |
| mail(receptor, | |
| subject, | |
| messagem) | |
| backupdir = '/back/' | |
| username = 'root' | |
| password = 'root' | |
| hostnames = ['localhost'] | |
| filestamp = time.strftime('%Y-%m-%d') | |
| gmail_user = "sender@gmail.com" | |
| gmail_pwd = "PASSWORD" | |
| receptor = "email@email.at" | |
| for hostname in hostnames: | |
| print hostname | |
| backupdirco = setBackupDir(backupdir,hostname) | |
| print backupdirco | |
| performBackup(username, password, hostname, filestamp, backupdirco, receptor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment