mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 10:03:40 +02:00
wip(22w8e): add filesystem handler
This commit is contained in:
parent
4a5ec18e05
commit
3f51be9e55
10 changed files with 57 additions and 8 deletions
18
backup.py
18
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")
|
print("OK")
|
|
@ -2,7 +2,7 @@ from dotenv import load_dotenv
|
||||||
|
|
||||||
from .glob import file_exists
|
from .glob import file_exists
|
||||||
from .db import Database
|
from .db import Database
|
||||||
from .gcs import StorageClient
|
from .fs import FileSystem
|
||||||
|
|
||||||
if not file_exists(".env"):
|
if not file_exists(".env"):
|
||||||
raise FileNotFoundError("Environment variable file does not exist. Copy '.env.example' to '.env'")
|
raise FileNotFoundError("Environment variable file does not exist. Copy '.env.example' to '.env'")
|
||||||
|
|
1
src/cloud/__init__.py
Normal file
1
src/cloud/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .gcs import client as GoogleCloudStorage
|
|
@ -1 +1,2 @@
|
||||||
|
from .sqlite import dbname
|
||||||
from .database import Database
|
from .database import Database
|
|
@ -4,8 +4,9 @@ class Database(SQLite):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def backup_candidate(self, anchor: str) -> bool:
|
# Test if a candidate item should be backed up
|
||||||
sql = f"SELECT anchor, mtime, chksum FROM manifest WHERE anchor = '{anchor}'"
|
def check_item(self, obj: list) -> bool:
|
||||||
|
sql = f"SELECT anchor, mtime, chksum FROM manifest WHERE anchor = '{obj.anchor}'"
|
||||||
data = self.query(sql)
|
data = self.query(sql)
|
||||||
|
|
||||||
return True
|
return True
|
|
@ -4,6 +4,8 @@ import sqlite3 as sqlite
|
||||||
|
|
||||||
from ..glob import file_exists
|
from ..glob import file_exists
|
||||||
|
|
||||||
|
dbname = ".cloudbackup.db"
|
||||||
|
|
||||||
class SQLite():
|
class SQLite():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.db = sqlite.connect(self.get_db_path())
|
self.db = sqlite.connect(self.get_db_path())
|
||||||
|
@ -34,15 +36,14 @@ class SQLite():
|
||||||
|
|
||||||
# Get path to database file
|
# Get path to database file
|
||||||
def get_db_path(self) -> str:
|
def get_db_path(self) -> str:
|
||||||
name = ".cloudbackup.db"
|
|
||||||
path = os.getenv("SOURCE_FOLDER")
|
path = os.getenv("SOURCE_FOLDER")
|
||||||
|
|
||||||
# Append db file name if absent
|
# Append db file name if absent
|
||||||
if not path.endswith(name):
|
if not path.endswith(dbname):
|
||||||
# Append tailing slash if absent
|
# Append tailing slash if absent
|
||||||
if path[-1] != "/":
|
if path[-1] != "/":
|
||||||
path += "/"
|
path += "/"
|
||||||
path += name
|
path += dbname
|
||||||
return path
|
return path
|
||||||
|
|
||||||
# Prepare a fresh db with the expected table structure
|
# Prepare a fresh db with the expected table structure
|
||||||
|
|
1
src/fs/__init__.py
Normal file
1
src/fs/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .fs import FileSystem
|
30
src/fs/fs.py
Normal file
30
src/fs/fs.py
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue