/vakap/components/tgz.py

https://github.com/ombu/vakap · Python · 41 lines · 36 code · 5 blank · 0 comment · 8 complexity · 08c419e98fd209e19c02560eb1920fbd MD5 · raw file

  1. from fabric.decorators import task
  2. from fabric.api import settings, cd, hide, run, env
  3. from fabric.contrib.files import exists as file_exists
  4. from base import Component, s3_upload, s3_file_exists, s3_latest_file_in_bucket
  5. class TgzComponent(Component):
  6. def __init__(self, site_name, raw_data):
  7. super(type(self), self).__init__(site_name, raw_data)
  8. def backup(self):
  9. with settings(host_string=self.host_string):
  10. try:
  11. backup_files(self.site_name, self.site_path, self.tmpdir)
  12. except AttributeError:
  13. backup_files(self.site_name, self.site_path)
  14. def status(self):
  15. date = s3_latest_file_in_bucket(env.s3_bucket, self.site_name)
  16. print("%s last backed up: %s" % (self.__class__.__name__, date))
  17. @task
  18. def backup_files(site_name, path, tmpdir='/tmp'):
  19. from time import gmtime, strftime
  20. with cd(path):
  21. date = strftime("%Y.%m.%d", gmtime())
  22. gpg_file = 'files-%s.tgz.gpg' % date
  23. local_file = "%s/vakap-%s" % (tmpdir, gpg_file)
  24. s3_dest = "s3://%s/%s/%s" % (env.s3_bucket, site_name, gpg_file)
  25. if s3_file_exists(s3_dest):
  26. print " - File exists: %s. Skipping" % s3_dest
  27. return
  28. else:
  29. print " - Taring and gziping directory: %s => %s" % (path, tmpdir)
  30. if file_exists('current'):
  31. run("tar czh current | gpg --encrypt --recipient %s > %s" %
  32. (env.gpg_key, local_file))
  33. else:
  34. run("tar czh . | gpg --encrypt --recipient %s > %s" %
  35. (env.gpg_key, local_file))
  36. s3_upload(local_file, s3_dest)