py_s3_pg_back.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python2.6
  2. #Coded by Anto !!
  3. # This script Uploades Database dump to s3
  4. import os
  5. import datetime
  6. import time
  7. import string
  8. import smtplib
  9. def s3upload(filename):
  10. try:
  11. from boto.s3.connection import S3Connection
  12. conn = S3Connection('access key', 'acces token')
  13. from boto.s3.key import Key
  14. b=conn.get_bucket('bucket name')
  15. k=b.new_key(filename)
  16. k.set_contents_from_filename(filename)
  17. #clear the pg dump in local
  18. syscommand='rm '+filename
  19. os.system(syscommand)
  20. print ("Uploaded to s3 and deleted from local !")
  21. except IOError as e:
  22. panic(e)
  23. print (e)
  24. # function to mail me when smthing goes wrong !
  25. def panic(stacktrace):
  26. print ("error!")
  27. SUBJECT = " Backup Failed !!"
  28. TO = "anto@urdomain.com"
  29. FROM = "postgres@urdomain.com"
  30. text = "Backup Failed"+stacktrace
  31. BODY = string.join((
  32. "From: %s" % FROM,
  33. "To: %s" % TO,
  34. "Subject: %s" % SUBJECT ,
  35. "",
  36. text
  37. ), "\r\n")
  38. server = smtplib.SMTP('localhost')
  39. server.sendmail(FROM, [TO], BODY)
  40. server.quit()