transitioned to python
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.egg-info
|
||||||
15
pyproject.toml
Normal file
15
pyproject.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "restic"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Lightweight wrapper around restic"
|
||||||
|
authors = [
|
||||||
|
{ name = "John Lancaster", email = "32917998+jsl12@users.noreply.github.com" },
|
||||||
|
]
|
||||||
|
license = { file = "LICENSE" }
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
|
||||||
|
dependencies = ["rich", "requests"]
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
uv
|
||||||
|
ruff
|
||||||
|
rich
|
||||||
|
requests
|
||||||
0
src/restic/__init__.py
Normal file
0
src/restic/__init__.py
Normal file
47
src/restic/backup.py
Executable file
47
src/restic/backup.py
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from rich.console import Console
|
||||||
|
from rich.progress import Progress
|
||||||
|
|
||||||
|
from .console import console, logger
|
||||||
|
from .loki import send_to_loki
|
||||||
|
|
||||||
|
|
||||||
|
def main(command: str, loki_url: str):
|
||||||
|
process = subprocess.Popen(
|
||||||
|
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True
|
||||||
|
)
|
||||||
|
|
||||||
|
with process.stdout, Progress(console=console, transient=True) as progress:
|
||||||
|
task = progress.add_task('Running backup', total=1.0)
|
||||||
|
for line in iter(process.stdout.readline, ''):
|
||||||
|
data = json.loads(line)
|
||||||
|
if data['message_type'] == 'status':
|
||||||
|
progress.update(task, completed=data['percent_done'])
|
||||||
|
progress.update(task, completed=1.0, refresh=True)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f'[yellow]{data["snapshot_id"]}[/] done, with {data["files_new"]} new files, {data["files_changed"]} files changed. Added {data["data_added"] / 10**6:.1f} MB out of {data["total_bytes_processed"] / 10**6:.1f} MB'
|
||||||
|
)
|
||||||
|
|
||||||
|
send_to_loki(loki_url=loki_url, line=line, backup='summary')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import os
|
||||||
|
|
||||||
|
from rich.console import Console
|
||||||
|
from rich.logging import RichHandler
|
||||||
|
|
||||||
|
console = Console()
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)]
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd = f'restic backup {os.environ["BACKUP_DIR"]} --tag python-script --json'
|
||||||
|
main(cmd, 'http://192.168.1.107:3100/loki/api/v1/push')
|
||||||
7
src/restic/console.py
Normal file
7
src/restic/console.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from rich.console import Console
|
||||||
|
|
||||||
|
console = Console()
|
||||||
|
|
||||||
|
logger = logging.getLogger('restic_parser')
|
||||||
25
src/restic/loki.py
Normal file
25
src/restic/loki.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import os
|
||||||
|
from time import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from .console import logger
|
||||||
|
|
||||||
|
|
||||||
|
def send_to_loki(loki_url: str, line: str, backup: str):
|
||||||
|
ns = round(time() * 1_000_000_000)
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
'streams': [
|
||||||
|
{
|
||||||
|
'stream': {'host': os.environ['HOSTNAME'], 'backup': backup},
|
||||||
|
'values': [[str(ns), line]],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
resp = requests.post(loki_url, json=payload, timeout=1)
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
|
else:
|
||||||
|
logger.info(f'Sent line to loki at {loki_url} {resp.text}')
|
||||||
Reference in New Issue
Block a user