improved python backup script

This commit is contained in:
John Lancaster
2024-05-25 21:52:11 -05:00
parent 6d16387e4b
commit fb95a111e4
2 changed files with 24 additions and 10 deletions

View File

@@ -3,26 +3,40 @@
import json
import logging
import subprocess
from pathlib import Path
from rich.console import Console
from rich.progress import Progress
from .console import console, logger
from .loki import send_to_loki
from restic.console import console, logger
from restic.loki import send_to_loki
def main(command: str, loki_url: str):
def main(backup_dir: Path, loki_url: str):
cmd = f'restic backup {backup_dir} --json --tag python-script'
logger.debug(f'Running cmd [bright_black]{cmd}[/]')
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True
cmd, 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)
try:
data = json.loads(line)
except Exception as e:
logger.exception(e)
else:
if data['message_type'] == 'status':
progress.update(task, completed=data['percent_done'])
progress.update(
task,
completed=1.0,
refresh=True,
description=f'Finished [yellow]{data["snapshot_id"]}[/]',
)
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'
@@ -43,5 +57,4 @@ if __name__ == '__main__':
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')
main(Path(os.environ['BACKUP_DIR']), 'http://192.168.1.107:3100/loki/api/v1/push')