/ Published in: Python
A script to make auto-backups to Amazon S3 using boto library
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/env python import sys,boto,os,time from boto.s3.key import Key import time from datetime import date,timedelta from settings import * ZIPFILE = os.path.join("/tmp/",str(date.today())+".tar.bz2") # print percent complete def percent_cb(complete, total): sys.stdout.write('.') sys.stdout.flush() # fileupload def upload(filename): print '-> Uploading %s...' % filename count =0 while True: try: conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME) k = Key(bucket) k.key = os.path.basename(filename) k.set_contents_from_filename(filename,cb=percent_cb, num_cb=10) return True except: print sys.exc_info() time.sleep(30) count+=1 if count>=10: break return False def make(): print "-> Backup BD..." print "-> Compress..." os.system("tar cvjf %s /etc/ /var/www/ /root/scripts/ /var/webapps/ --exclude=\"nod32-update\" --exclude=\"fermasa\"" % ZIPFILE) def clean_old(): print "-> Cleaning old backup..." yesterday = date.today() - timedelta(1) # one backup per week if (yesterday.isoweekday()%7) == 0: return count =0 while True: try: conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(AWS_STORAGE_BUCKET_NAME) bucket.delete_key(str(yesterday)+".tar.bz2") break except: print sys.exc_info() time.sleep(30) count+=1 if count>=10: break def clean(): os.system("rm -vf %s" % ZIPFILE) def main(): try: make() if (upload(ZIPFILE)): clean_old() clean() except KeyboardInterrupt: os.system("rm -vf %s" % ZIPFILE) if __name__ == "__main__": main()