Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #encoding:utf-8
- #
- # backs up the database and emails it
- import os, sys, time, commands
- ################################################
- ### Config
- DATE = time.strftime("%Y%m%d_%H%M")
- # MySQL login
- SQL_USER = "user"
- SQL_PASS = "pass"
- SQL_DB = "db"
- # SMTP server
- SERVER = 'localhost'
- # Email addresses
- BACKUP_EMAIL=["email@gmail.com"] # Array of email(s)
- FROM_EMAIL = "system@localhost" # Only one email
- # Temp backup locations
- DB_BACKUP="/home/mvp/backups/find_db-%(date)s.sql.bz2" % {'date':DATE}
- # Email subjects
- DB_EMAIL_SUBJECT="%(date)s FIND database" % {'date':DATE}
- ### end Config
- ################################################
- ################################################
- ### Process config
- sql_backup_command = "mysqldump -u %(SQL_USER)s -p%(SQL_PASS)s %(SQL_DB)s | bzip2 - > %(DB_BACKUP)s" % {
- 'DB_BACKUP':DB_BACKUP,
- 'SQL_USER':SQL_USER,
- 'SQL_PASS':SQL_PASS,
- 'SQL_DB':SQL_DB
- }
- # end Process config
- ################################################
- ################################################
- ### Main application
- def main():
- """Main backup function"""
- print "Backing commencing at %s" % (DATE)
- # Run commands
- print "Creating db backup..."
- sql_status,sql_cmd_out = commands.getstatusoutput(sql_backup_command)
- if sql_status == 0:
- db_file_size = round(float( os.stat(DB_BACKUP)[6] ) /1024/1024, 2) # Get file-size in MB
- print "..successful (%.2fMB)" % (db_file_size)
- try:
- send_mail(
- send_from = FROM_EMAIL,
- send_to = BACKUP_EMAIL,
- subject = DB_EMAIL_SUBJECT,
- text = "Database backup",
- files = [DB_BACKUP],
- server = SERVER
- )
- print "Sending db backup successful"
- except Exception,errormsg:
- print "Sending db backup FAILED. Error was:",errormsg
- #end try
- # Remove backup file
- print "Removing db backup..."
- try:
- os.remove(DB_BACKUP)
- print "...successful"
- except Exception, errormsg:
- print "...FAILED. Error was: %s" % (errormsg)
- #end try
- else:
- print "Creating db backup FAILED. Output was:", sql_cmd_out
- #end if sql_status
- #end main
- ################################################
- ################################################
- # Send email function
- # needed email libs..
- import smtplib
- from email.MIMEMultipart import MIMEMultipart
- from email.MIMEBase import MIMEBase
- from email.MIMEText import MIMEText
- from email.Utils import COMMASPACE, formatdate
- from email import Encoders
- def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
- assert type(send_to)==list
- assert type(files)==list
- msg = MIMEMultipart()
- msg['From'] = send_from
- msg['To'] = COMMASPACE.join(send_to)
- msg['Date'] = formatdate(localtime=True)
- msg['Subject'] = subject
- msg.attach( MIMEText(text) )
- for f in files:
- part = MIMEBase('application', "octet-stream")
- try:
- part.set_payload( open(f,"rb").read() )
- except Exception, errormsg:
- raise IOError("File not found: %s"%(errormsg))
- Encoders.encode_base64(part)
- part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
- msg.attach(part)
- #end for f
- smtp = smtplib.SMTP(server)
- smtp.sendmail(send_from, send_to, msg.as_string())
- smtp.close()
- #end send_mail
- ################################################
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment