mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 10:03:40 +02:00
wip(22w9b): add azure
This commit is contained in:
parent
29ef6ff8dd
commit
ff0b3529e0
5 changed files with 75 additions and 9 deletions
10
.env.example
10
.env.example
|
@ -1,9 +1,9 @@
|
||||||
# Path to the local folder to back up
|
# Path to the local folder to back up
|
||||||
SOURCE_FOLDER=
|
SOURCE_FOLDER=""
|
||||||
# Name of the remote bucket (destination)
|
# Name of the remote bucket (destination)
|
||||||
TARGET_BUCKET=
|
TARGET_BUCKET=""
|
||||||
|
|
||||||
# Cloud provider (gcs, s3, azure)
|
# Cloud provider (gcs, s3, azure)
|
||||||
SERVICE_NAME=
|
SERVICE_NAME=""
|
||||||
# Path to service account key file
|
# Cloud provider access string or path to key file
|
||||||
SERVICE_KEY=
|
SERVICE_KEY=""
|
28
install.sh
Executable file
28
install.sh
Executable 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
|
|
@ -1,2 +1,4 @@
|
||||||
python-dotenv
|
python-dotenv
|
||||||
google-cloud-storage
|
google-cloud-storage
|
||||||
|
azure-storage-blob
|
||||||
|
boto3
|
31
src/cloud/azure.py
Normal file
31
src/cloud/azure.py
Normal 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
|
|
@ -28,14 +28,17 @@ class Database(SQLite):
|
||||||
# Check if item exists in the database
|
# Check if item exists in the database
|
||||||
def item_exists(self, item: Union[list, tuple]) -> bool:
|
def item_exists(self, item: Union[list, tuple]) -> bool:
|
||||||
sql = "SELECT anchor FROM manifest WHERE anchor = ?"
|
sql = "SELECT anchor FROM manifest WHERE anchor = ?"
|
||||||
res = self.query(sql, (item[0]))
|
res = self.query(sql, (item[0],))
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# Check if item should be backed up by comparing mtime and checksum
|
# Check if item should be backed up by comparing mtime and checksum
|
||||||
def check_item(self, item: Union[list, tuple]) -> bool:
|
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 = ?"
|
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
|
# New item or item changed, so back it up
|
||||||
if not db_item or (item != db_item[0]):
|
if not db_item or (item != db_item[0]):
|
||||||
|
@ -44,10 +47,12 @@ class Database(SQLite):
|
||||||
|
|
||||||
# Insert or update item in database
|
# Insert or update item in database
|
||||||
def set_item(self, item: Union[list, tuple]) -> bool:
|
def set_item(self, item: Union[list, tuple]) -> bool:
|
||||||
|
values = [item[0], item[1], item[0]]
|
||||||
sql = "UPDATE manifest SET anchor = ?, chksum = ? WHERE anchor = ?"
|
sql = "UPDATE manifest SET anchor = ?, chksum = ? WHERE anchor = ?"
|
||||||
|
|
||||||
if not self.item_exists(item):
|
if not self.item_exists(item):
|
||||||
sql = f"INSERT INTO manifest ({self.columns}) VALUES (?, ?)"
|
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
|
return True
|
Loading…
Add table
Reference in a new issue