Guest User

Untitled

a guest
Apr 20th, 2018
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #encoding:utf-8
  3. #
  4. # backs up the database and emails it
  5.  
  6. import os, sys, time, commands
  7.  
  8. ################################################
  9. ### Config
  10.  
  11. DATE = time.strftime("%Y%m%d_%H%M")
  12.  
  13. # MySQL login
  14. SQL_USER = "user"
  15. SQL_PASS = "pass"
  16. SQL_DB = "db"
  17.  
  18. # SMTP server
  19. SERVER = 'localhost'
  20.  
  21. # Email addresses
  22. BACKUP_EMAIL=["email@gmail.com"] # Array of email(s)
  23. FROM_EMAIL = "system@localhost" # Only one email
  24.  
  25. # Temp backup locations
  26. DB_BACKUP="/home/mvp/backups/find_db-%(date)s.sql.bz2" % {'date':DATE}
  27.  
  28. # Email subjects
  29. DB_EMAIL_SUBJECT="%(date)s FIND database" % {'date':DATE}
  30.  
  31.  
  32. ### end Config
  33. ################################################
  34.  
  35. ################################################
  36. ### Process config
  37. sql_backup_command = "mysqldump -u %(SQL_USER)s -p%(SQL_PASS)s %(SQL_DB)s | bzip2 - > %(DB_BACKUP)s" % {
  38. 'DB_BACKUP':DB_BACKUP,
  39. 'SQL_USER':SQL_USER,
  40. 'SQL_PASS':SQL_PASS,
  41. 'SQL_DB':SQL_DB
  42. }
  43.  
  44. # end Process config
  45. ################################################
  46.  
  47. ################################################
  48. ### Main application
  49. def main():
  50. """Main backup function"""
  51. print "Backing commencing at %s" % (DATE)
  52.  
  53. # Run commands
  54. print "Creating db backup..."
  55. sql_status,sql_cmd_out = commands.getstatusoutput(sql_backup_command)
  56. if sql_status == 0:
  57. db_file_size = round(float( os.stat(DB_BACKUP)[6] ) /1024/1024, 2) # Get file-size in MB
  58. print "..successful (%.2fMB)" % (db_file_size)
  59. try:
  60. send_mail(
  61. send_from = FROM_EMAIL,
  62. send_to = BACKUP_EMAIL,
  63. subject = DB_EMAIL_SUBJECT,
  64. text = "Database backup",
  65. files = [DB_BACKUP],
  66. server = SERVER
  67. )
  68. print "Sending db backup successful"
  69. except Exception,errormsg:
  70. print "Sending db backup FAILED. Error was:",errormsg
  71. #end try
  72.  
  73. # Remove backup file
  74. print "Removing db backup..."
  75. try:
  76. os.remove(DB_BACKUP)
  77. print "...successful"
  78. except Exception, errormsg:
  79. print "...FAILED. Error was: %s" % (errormsg)
  80. #end try
  81. else:
  82. print "Creating db backup FAILED. Output was:", sql_cmd_out
  83. #end if sql_status
  84.  
  85. #end main
  86. ################################################
  87.  
  88. ################################################
  89. # Send email function
  90.  
  91. # needed email libs..
  92. import smtplib
  93. from email.MIMEMultipart import MIMEMultipart
  94. from email.MIMEBase import MIMEBase
  95. from email.MIMEText import MIMEText
  96. from email.Utils import COMMASPACE, formatdate
  97. from email import Encoders
  98.  
  99. def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
  100. assert type(send_to)==list
  101. assert type(files)==list
  102.  
  103. msg = MIMEMultipart()
  104. msg['From'] = send_from
  105. msg['To'] = COMMASPACE.join(send_to)
  106. msg['Date'] = formatdate(localtime=True)
  107. msg['Subject'] = subject
  108.  
  109. msg.attach( MIMEText(text) )
  110.  
  111. for f in files:
  112. part = MIMEBase('application', "octet-stream")
  113. try:
  114. part.set_payload( open(f,"rb").read() )
  115. except Exception, errormsg:
  116. raise IOError("File not found: %s"%(errormsg))
  117. Encoders.encode_base64(part)
  118. part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
  119. msg.attach(part)
  120. #end for f
  121.  
  122. smtp = smtplib.SMTP(server)
  123. smtp.sendmail(send_from, send_to, msg.as_string())
  124. smtp.close()
  125. #end send_mail
  126. ################################################
  127.  
  128. if __name__ == '__main__':
  129. main()
Add Comment
Please, Sign In to add comment