mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 01:53:42 +02:00
wip(22w9b): add gcs upload
This commit is contained in:
parent
bc9ba0783f
commit
7fa595584f
6 changed files with 37 additions and 14 deletions
|
@ -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=
|
||||
SERVICE_KEY=
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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()
|
||||
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
|
|
@ -10,6 +10,6 @@ CREATE TABLE manifest (
|
|||
|
||||
INSERT INTO flags
|
||||
VALUES
|
||||
("ZIP", 1),
|
||||
("COMPRESS", 1),
|
||||
("BUCKET_OK", 0),
|
||||
("INIT", 1);
|
|
@ -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])}"
|
||||
|
|
Loading…
Add table
Reference in a new issue