plain_inc_bak_8.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. def upload_s3(dirpath: str) -> None:
  2. import boto3, time
  3. real_filepath = ''
  4. compressed_filepath = compress_backup(dirpath)
  5. if c.S3_GPG_ENCRYPT and not c.NOGPG:
  6. real_filepath = gpg_encrypt_file(compressed_filepath)
  7. real_filepath = compressed_filepath if not real_filepath else real_filepath
  8. datepart = time.strftime("%Y%m%d%H%M%S")
  9. remote_filename = os.path.split(real_filepath)[1] + '.' + datepart
  10. with open(real_filepath, 'rb') as data:
  11. message('Uploading file to Amazon S3')
  12. if not c.DRY_RUN:
  13. s3 = boto3.client('s3', aws_access_key_id=c.S3_ACCESS_KEY,
  14. aws_secret_access_key=c.S3_SECRET_KEY)
  15. s3.upload_fileobj(data, c.S3_BUCKET, remote_filename)
  16. if not c.DRY_RUN:
  17. if c.S3_GPG_ENCRYPT:
  18. # Remove the local encrupted file
  19. os.unlink(real_filepath)
  20. os.unlink(compressed_filepath)
  21. message('File uploaded to S3 bucket "{}" as key "{}"'.
  22. format(c.S3_BUCKET, remote_filename))