Amazon S3 backup


/ Published in: Python
Save to your folder(s)

A script to make auto-backups to Amazon S3 using boto library


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2.  
  3. import sys,boto,os,time
  4. from boto.s3.key import Key
  5. import time
  6. from datetime import date,timedelta
  7. from settings import *
  8.  
  9. ZIPFILE = os.path.join("/tmp/",str(date.today())+".tar.bz2")
  10.  
  11. # print percent complete
  12. def percent_cb(complete, total):
  13. sys.stdout.write('.')
  14. sys.stdout.flush()
  15.  
  16. # fileupload
  17. def upload(filename):
  18.  
  19. print '-> Uploading %s...' % filename
  20.  
  21. count =0
  22. while True:
  23. try:
  24. conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  25. bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME)
  26. k = Key(bucket)
  27. k.key = os.path.basename(filename)
  28. k.set_contents_from_filename(filename,cb=percent_cb, num_cb=10)
  29. return True
  30. except:
  31. print sys.exc_info()
  32. time.sleep(30)
  33. count+=1
  34.  
  35. if count>=10:
  36. break
  37.  
  38. return False
  39.  
  40. def make():
  41. print "-> Backup BD..."
  42. print "-> Compress..."
  43. os.system("tar cvjf %s /etc/ /var/www/ /root/scripts/ /var/webapps/ --exclude=\"nod32-update\" --exclude=\"fermasa\"" % ZIPFILE)
  44.  
  45. def clean_old():
  46. print "-> Cleaning old backup..."
  47. yesterday = date.today() - timedelta(1)
  48.  
  49. # one backup per week
  50. if (yesterday.isoweekday()%7) == 0:
  51. return
  52.  
  53. count =0
  54. while True:
  55. try:
  56. conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  57. bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME)
  58. bucket.delete_key(str(yesterday)+".tar.bz2")
  59. break
  60. except:
  61. print sys.exc_info()
  62. time.sleep(30)
  63. count+=1
  64.  
  65. if count>=10:
  66. break
  67.  
  68. def clean():
  69. os.system("rm -vf %s" % ZIPFILE)
  70.  
  71. def main():
  72. try:
  73. make()
  74. if (upload(ZIPFILE)): clean_old()
  75. clean()
  76. except KeyboardInterrupt:
  77. os.system("rm -vf %s" % ZIPFILE)
  78.  
  79.  
  80. if __name__ == "__main__":
  81. main()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.