transitioned to python
This commit is contained in:
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