wip(22w9b): add azure

This commit is contained in:
Victor Westerlund 2022-03-03 02:32:52 +01:00
parent 29ef6ff8dd
commit ff0b3529e0
5 changed files with 75 additions and 9 deletions

View file

@ -1,9 +1,9 @@
# Path to the local folder to back up
SOURCE_FOLDER=
SOURCE_FOLDER=""
# Name of the remote bucket (destination)
TARGET_BUCKET=
TARGET_BUCKET=""
# Cloud provider (gcs, s3, azure)
SERVICE_NAME=
# Path to service account key file
SERVICE_KEY=
SERVICE_NAME=""
# Cloud provider access string or path to key file
SERVICE_KEY=""

28
install.sh Executable file
View file

@ -0,0 +1,28 @@
install () {
python3 -m pip install $1
}
install python-dotenv
# Install Python libraries for cloud provider
case $1 in
"gcs")
install google-cloud-storage
;;
"azure")
install azure-storage-blob
;;
"aws")
install boto3
;;
*) ;;
esac
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
cp .env.example .env
sed -i "s/SERVICE_NAME=\"\"/SERVICE_NAME=\"$1\"" .env
fi

View file

@ -1,2 +1,4 @@
python-dotenv
google-cloud-storage
azure-storage-blob
boto3

31
src/cloud/azure.py Normal file
View file

@ -0,0 +1,31 @@
import os
from azure.storage.blob import BlobServiceClient
from ..fs.utils import get_file
class StorageClient:
def __init__(self):
self.client = BlobServiceClient.from_connection_string(os.getenv("SERVICE_KEY"))
self._error = None
@property
def error(self):
return self._error
@error.setter
def error(self, state):
self._error = state
def upload(self, path: str) -> bool:
name = get_file(path)
blob = self.client.get_blob_client(container=os.getenv("TARGET_BUCKET"), blob=name)
try:
with open(path, "rb") as f:
blob.upload_blob(f,overwrite=True)
return True
except Exception as e:
if e.response.status_code == 403:
self.error = "Account lacks 'storage.objects.create' permissions on this bucket "
return False

View file

@ -28,14 +28,17 @@ class Database(SQLite):
# Check if item exists in the database
def item_exists(self, item: Union[list, tuple]) -> bool:
sql = "SELECT anchor FROM manifest WHERE anchor = ?"
res = self.query(sql, (item[0]))
res = self.query(sql, (item[0],))
return res
# Check if item should be backed up by comparing mtime and checksum
def check_item(self, item: Union[list, tuple]) -> bool:
if os.getenv("FORCE_UPLOAD"):
return True
sql = f"SELECT {self.columns} FROM manifest WHERE anchor = ?"
db_item = self.query(sql, (item[0]))
db_item = self.query(sql, (item[0],))
# New item or item changed, so back it up
if not db_item or (item != db_item[0]):
@ -44,10 +47,12 @@ class Database(SQLite):
# Insert or update item in database
def set_item(self, item: Union[list, tuple]) -> bool:
values = [item[0], item[1], item[0]]
sql = "UPDATE manifest SET anchor = ?, chksum = ? WHERE anchor = ?"
if not self.item_exists(item):
sql = f"INSERT INTO manifest ({self.columns}) VALUES (?, ?)"
self.query(sql, (item[0], item[1], item[0]))
values.pop()
self.query(sql, values)
return True