diff --git a/backup.py b/backup.py index 14e5981..4a5ad57 100644 --- a/backup.py +++ b/backup.py @@ -1,4 +1,18 @@ -from src import Database, StorageClient +import sys +from src import Database, FileSystem -client = Database() +class Backup(FileSystem): + def __init__(self, argv): + super().__init__() + self.db = Database() + + def backup(self, obj: list) -> bool: + db_response = self.db.check_obj(obj) + return True + + def backup_all(self): + for item in self.all(): + self.backup(item) + +Backup(sys.argv).backup_all() print("OK") \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py index 69526d3..c9d748d 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -2,7 +2,7 @@ from dotenv import load_dotenv from .glob import file_exists from .db import Database -from .gcs import StorageClient +from .fs import FileSystem if not file_exists(".env"): raise FileNotFoundError("Environment variable file does not exist. Copy '.env.example' to '.env'") diff --git a/src/cloud/__init__.py b/src/cloud/__init__.py new file mode 100644 index 0000000..2a02487 --- /dev/null +++ b/src/cloud/__init__.py @@ -0,0 +1 @@ +from .gcs import client as GoogleCloudStorage \ No newline at end of file diff --git a/src/gcs/__init__.py b/src/cloud/gcs/__init__.py similarity index 100% rename from src/gcs/__init__.py rename to src/cloud/gcs/__init__.py diff --git a/src/gcs/client.py b/src/cloud/gcs/client.py similarity index 100% rename from src/gcs/client.py rename to src/cloud/gcs/client.py diff --git a/src/db/__init__.py b/src/db/__init__.py index bae9c71..ff55f12 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -1 +1,2 @@ +from .sqlite import dbname from .database import Database \ No newline at end of file diff --git a/src/db/database.py b/src/db/database.py index 03391a3..c2afba3 100644 --- a/src/db/database.py +++ b/src/db/database.py @@ -4,8 +4,9 @@ class Database(SQLite): def __init__(self): super().__init__() - def backup_candidate(self, anchor: str) -> bool: - sql = f"SELECT anchor, mtime, chksum FROM manifest WHERE anchor = '{anchor}'" + # Test if a candidate item should be backed up + def check_item(self, obj: list) -> bool: + sql = f"SELECT anchor, mtime, chksum FROM manifest WHERE anchor = '{obj.anchor}'" data = self.query(sql) return True \ No newline at end of file diff --git a/src/db/sqlite.py b/src/db/sqlite.py index 8ee5281..5a40b8b 100644 --- a/src/db/sqlite.py +++ b/src/db/sqlite.py @@ -4,6 +4,8 @@ import sqlite3 as sqlite from ..glob import file_exists +dbname = ".cloudbackup.db" + class SQLite(): def __init__(self): self.db = sqlite.connect(self.get_db_path()) @@ -34,15 +36,14 @@ class SQLite(): # Get path to database file def get_db_path(self) -> str: - name = ".cloudbackup.db" path = os.getenv("SOURCE_FOLDER") # Append db file name if absent - if not path.endswith(name): + if not path.endswith(dbname): # Append tailing slash if absent if path[-1] != "/": path += "/" - path += name + path += dbname return path # Prepare a fresh db with the expected table structure diff --git a/src/fs/__init__.py b/src/fs/__init__.py new file mode 100644 index 0000000..91c2a7b --- /dev/null +++ b/src/fs/__init__.py @@ -0,0 +1 @@ +from .fs import FileSystem \ No newline at end of file diff --git a/src/fs/fs.py b/src/fs/fs.py new file mode 100644 index 0000000..8473d30 --- /dev/null +++ b/src/fs/fs.py @@ -0,0 +1,30 @@ +import os +import zlib + +from ..db import dbname + +class FileSystem: + def __init__(self): + self.path = FileSystem.get_path() + + @staticmethod + def get_path() -> str: + return os.getenv("SOURCE_FOLDER") + + @staticmethod + def chksum(data: str) -> str: + return zlib.crc32(data) + + # Get metadata from candidate file or folder + def make_obj(self, anchor: str) -> list: + mtime = os.path.getmtime(anchor) + if os.path.isdir(anchor): + + + data = [anchor, mtime, chksum] + return obj + + def all(self) -> list: + content = os.listdir(self.path) + content = list(map(self.make_obj, content)) + return content \ No newline at end of file