mirror of
https://codeberg.org/vlw/cloud-backup.git
synced 2025-09-14 10:03:40 +02:00
wip(22w8a): add wip db and gcs client
This commit is contained in:
parent
9071d6d9fe
commit
edc3b6fb9d
17 changed files with 154 additions and 0 deletions
4
.env.example
Normal file
4
.env.example
Normal file
|
@ -0,0 +1,4 @@
|
|||
SOURCE_FOLDER=
|
||||
TARGET_BUCKET=
|
||||
|
||||
GOOGLE_APPLICATION_CREDENTIALS=
|
50
.gitignore
vendored
Normal file
50
.gitignore
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
# Bootstrapping #
|
||||
#################
|
||||
/node_modules
|
||||
/public/hot
|
||||
/public/storage
|
||||
/storage/*.key
|
||||
/vendor
|
||||
.env
|
||||
.env.backup
|
||||
.phpunit.result.cache
|
||||
Homestead.json
|
||||
Homestead.yaml
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
public/robots.txt
|
||||
|
||||
|
||||
# OS generated files #
|
||||
######################
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
Icon?
|
||||
ehthumbs.db
|
||||
Thumbs.db
|
||||
.directory
|
||||
|
||||
# Tool specific files #
|
||||
#######################
|
||||
# vim
|
||||
*~
|
||||
*.swp
|
||||
*.swo
|
||||
# sublime text & textmate
|
||||
*.sublime-*
|
||||
*.stTheme.cache
|
||||
*.tmlanguage.cache
|
||||
*.tmPreferences.cache
|
||||
# Eclipse
|
||||
.settings/*
|
||||
# JetBrains, aka PHPStorm, IntelliJ IDEA
|
||||
.idea/*
|
||||
# NetBeans
|
||||
nbproject/*
|
||||
# Visual Studio Code
|
||||
.vscode
|
||||
# Sass preprocessor
|
||||
.sass-cache/
|
14
.theia/launch.json
Normal file
14
.theia/launch.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Python: Current File",
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"program": "${file}",
|
||||
"console": "integratedTerminal"
|
||||
}
|
||||
]
|
||||
}
|
4
backup.py
Normal file
4
backup.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from src import SQLite, StorageClient
|
||||
|
||||
client = SQLite()
|
||||
print("OK")
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
python-dotenv
|
||||
google-cloud-storage
|
10
src/__init__.py
Normal file
10
src/__init__.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from dotenv import load_dotenv
|
||||
|
||||
from .glob import file_exists
|
||||
from .db import SQLite
|
||||
from .gcs import StorageClient
|
||||
|
||||
if not file_exists(".env"):
|
||||
raise FileNotFoundError("Environment variable file does not exist. Copy '.env.example' to '.env'")
|
||||
|
||||
load_dotenv()
|
BIN
src/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
src/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/glob.cpython-39.pyc
Normal file
BIN
src/__pycache__/glob.cpython-39.pyc
Normal file
Binary file not shown.
1
src/db/__init__.py
Normal file
1
src/db/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .sqlite import SQLite
|
BIN
src/db/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
src/db/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/db/__pycache__/sqlite.cpython-39.pyc
Normal file
BIN
src/db/__pycache__/sqlite.cpython-39.pyc
Normal file
Binary file not shown.
43
src/db/sqlite.py
Normal file
43
src/db/sqlite.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
import os
|
||||
import sqlite3 as sqlite
|
||||
|
||||
from ..glob import file_exists
|
||||
|
||||
class SQLite():
|
||||
def __init__(self):
|
||||
self.db = sqlite.connect(self.get_db_path())
|
||||
self.cursor = self.db.cursor()
|
||||
|
||||
self.init()
|
||||
|
||||
def query(self, sql: str):
|
||||
result = self.cursor.execute(sql)
|
||||
|
||||
if result.rowcount < 1:
|
||||
return False
|
||||
|
||||
return result
|
||||
|
||||
def get_db_path(self) -> str:
|
||||
name = ".gcsarchive.db"
|
||||
path = os.getenv("SOURCE_FOLDER")
|
||||
|
||||
# Append db file name if absent
|
||||
if not path.endswith(name):
|
||||
# Append tailing slash if absent
|
||||
if path[-1] != "/":
|
||||
path += "/"
|
||||
path += name
|
||||
return path
|
||||
|
||||
def configure_db(self):
|
||||
metadata_sql = "CREATE TABLE metadata (key text, value text)"
|
||||
metadata = self.query(metadata_sql)
|
||||
|
||||
def init(self):
|
||||
# Set up db if it's fresh
|
||||
hasmeta_sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata'"
|
||||
if not self.query(hasmeta_sql):
|
||||
self.configure_db()
|
||||
|
||||
return True
|
1
src/gcs/__init__.py
Normal file
1
src/gcs/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .client import StorageClient
|
BIN
src/gcs/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
src/gcs/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/gcs/__pycache__/client.cpython-39.pyc
Normal file
BIN
src/gcs/__pycache__/client.cpython-39.pyc
Normal file
Binary file not shown.
21
src/gcs/client.py
Normal file
21
src/gcs/client.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
import os
|
||||
from os.path import exists
|
||||
from google.cloud import storage
|
||||
|
||||
class StorageClient(storage.Client):
|
||||
def __init__(self, bucket: str = None):
|
||||
if not bucket:
|
||||
bucket = os.getenv("TARGET_BUCKET")
|
||||
|
||||
if not self.gcloud_key_exists():
|
||||
raise Exception("GOOGLE_APPLICATION_CREDENTIALS has to point to a key file")
|
||||
|
||||
super().__init__()
|
||||
|
||||
# Check if env var is set to a key file
|
||||
def gcloud_key_exists(self) -> bool:
|
||||
keyfile = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
|
||||
|
||||
if not keyfile or not exists(keyfile):
|
||||
return False
|
||||
return True
|
4
src/glob.py
Normal file
4
src/glob.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
import os.path
|
||||
|
||||
def file_exists(file: str) -> bool:
|
||||
return os.path.isfile(file)
|
Loading…
Add table
Reference in a new issue