diff --git a/.env.example b/.env.example index 74dc8b6..9ba18a7 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,6 @@ SOURCE_FOLDER= TARGET_BUCKET= # Cloud provider "gcs, aws, azure" -PROVIDER_NAME= +SERVICE_NAME= # Path to service account key file -PROVIDER_KEY= \ No newline at end of file +SERVICE_KEY= \ No newline at end of file diff --git a/src/backup.py b/src/backup.py index 2206eb1..7f625f8 100644 --- a/src/backup.py +++ b/src/backup.py @@ -13,7 +13,7 @@ class Backup(FileSystem): self.db = Database() self.cloud = StorageClient() - self.zip = self.db.get_flag("ZIP") + self.compress = self.db.get_flag("COMPRESS") # Backup a file or folder def backup_item(self, item: Union[list, str]) -> bool: @@ -36,7 +36,7 @@ class Backup(FileSystem): blob = item # Upload as zip archive - if self.zip: + if self.compress: blob = FileSystem.zip(blob) # Upload to cloud @@ -45,10 +45,13 @@ class Backup(FileSystem): if self.db.set_item(item): print("OK") else: - print("OK, BUT: Failed to update database") + print("OK, but failed to update database") else: print("FAILED") - + + # Remove temp zip + if self.compress: + FileSystem.delete(blob) return # Scan TARGET_FOLDER for files and folders to back up diff --git a/src/cloud/__init__.py b/src/cloud/__init__.py index fe8d569..dccc947 100644 --- a/src/cloud/__init__.py +++ b/src/cloud/__init__.py @@ -6,7 +6,7 @@ import importlib class Storage: def __init__(self): self._service = None - self.service = os.getenv("PROVIDER_NAME") + self.service = os.getenv("SERVICE_NAME") @property def service(self): @@ -19,7 +19,7 @@ class Storage: service = "gcs" module = importlib.import_module("src.cloud." + service) - self._service = module.StorageClient(os.getenv("TARGET_BUCKET")) + self._service = module.StorageClient() @staticmethod def get_args(values): diff --git a/src/cloud/gcs.py b/src/cloud/gcs.py index 992e481..94d2716 100644 --- a/src/cloud/gcs.py +++ b/src/cloud/gcs.py @@ -1,10 +1,26 @@ +import os from google.cloud import storage +from ..fs.utils import get_file + # Client for Google Cloud Storage class StorageClient: - def __init__(self, bucket): - client = storage.Client() - self.bucket = client.bucket(bucket) + def __init__(self): + os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.getenv("SERVICE_KEY") - def upload(self, item): - blob = self.bucket.blob() \ No newline at end of file + client = storage.Client() + self.bucket = client.bucket(self.get_bucket()) + + def get_bucket(self): + return os.getenv("TARGET_BUCKET") + + def upload(self, path: str) -> bool: + name = get_file(path) + blob = self.bucket.blob(name) + + try: + with open(path, "rb") as f: + blob.upload_from_file(f) + return True + except: + return False \ No newline at end of file diff --git a/src/db/config.sql b/src/db/config.sql index 23bb830..0c60e01 100644 --- a/src/db/config.sql +++ b/src/db/config.sql @@ -10,6 +10,6 @@ CREATE TABLE manifest ( INSERT INTO flags VALUES - ("ZIP", 1), + ("COMPRESS", 1), ("BUCKET_OK", 0), ("INIT", 1); \ No newline at end of file diff --git a/src/fs/fs.py b/src/fs/fs.py index 3f7e664..00314d9 100644 --- a/src/fs/fs.py +++ b/src/fs/fs.py @@ -20,6 +20,10 @@ class FileSystem: encoded = data.encode("utf-8") return zlib.crc32(encoded) + @staticmethod + def delete(path: str) -> bool: + return os.remove(path) + @staticmethod def zip(item) -> str: dest = f"{tempfile.gettempdir()}/{str(item[1])}"