more work

This commit is contained in:
John Lancaster
2024-05-27 00:08:38 -05:00
parent 6a546c92ec
commit 17f9fa203e
5 changed files with 33 additions and 19 deletions

View File

@@ -1,9 +1,20 @@
# Restic Scripts # Restic Scripts
## Environment Variables
| Env Variable | Description |
|--------------|--------------------------------------------------------------------------------------------|
| `BACKUP_DIR` | Directory to back up |
| `LOKI_URL` | Push URL for Loki. Should include the port and end with something like `/loki/api/v1/push` |
## Usage ## Usage
### Python ### Python
```shell
python -m restic.snapshots
```
```shell ```shell
python -m restic.backup python -m restic.backup
``` ```

View File

@@ -2,3 +2,4 @@ uv
ruff ruff
rich rich
requests requests
click

View File

@@ -8,12 +8,13 @@ from rich.console import Console
from rich.logging import RichHandler from rich.logging import RichHandler
from rich.progress import Progress from rich.progress import Progress
from restic import loki, snapshots
from restic.console import console, logger from restic.console import console, logger
from restic.loki import send_to_loki from restic.loki import send_to_loki
def run(backup_dir: Path, loki_url: str = None): def run(backup_dir: Path, loki_url: str = None, tag: str = 'python-script'):
cmd = f'restic backup {backup_dir} --json --tag python-script' cmd = f'restic backup {backup_dir} --json --tag {tag}'
logger.debug(f'Running cmd [bright_black]{cmd}[/]') logger.debug(f'Running cmd [bright_black]{cmd}[/]')
process = subprocess.Popen( process = subprocess.Popen(
@@ -54,14 +55,16 @@ def run(backup_dir: Path, loki_url: str = None):
envvar='BACKUP_DIR', envvar='BACKUP_DIR',
) )
@click.option('--loki-url', type=str, help='Loki URL for logging', envvar='LOKI_URL') @click.option('--loki-url', type=str, help='Loki URL for logging', envvar='LOKI_URL')
def main(backup_dir: Path, loki_url: str = None): @click.option('--tag', type=str, help='Tag to use in restic')
def main(backup_dir: Path, loki_url: str = None, tag: str = None):
console = Console() console = Console()
logging.basicConfig( logging.basicConfig(
level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)] level='DEBUG', format='%(message)s', handlers=[RichHandler(markup=True, console=console)]
) )
run(backup_dir, loki_url) run(backup_dir, loki_url, tag)
snapshots.run(loki_url)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -22,4 +22,9 @@ def send_to_loki(loki_url: str, line: str, backup: str):
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
else: else:
logger.info(f'Sent line to loki at {loki_url} {resp.text}') if resp.status_code == 204:
logger.info(f'Sent line to loki at {loki_url}')
else:
logger.error(
f'Problem sending log line to Loki. Response status code {resp.status_code}: {resp.text}'
)

View File

@@ -10,20 +10,14 @@ from restic.loki import send_to_loki
def run(loki_url: str = None): def run(loki_url: str = None):
cmd = 'restic snapshots --json' cmd = ['restic', 'snapshots', '--json']
logger.debug(f'Running cmd [bright_black]{cmd}[/]') with console.status('Getting snapshots...'):
process = subprocess.Popen( logger.debug(f'Running cmd [bright_black]{" ".join(cmd)}[/]')
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True result = subprocess.run(cmd, capture_output=True, text=True)
)
with process.stdout: line = result.stdout
for line in iter(process.stdout.readline, ''):
try:
data = json.loads(line) data = json.loads(line)
except Exception as e:
logger.exception(e)
else:
console.print(data) console.print(data)
if loki_url is not None: if loki_url is not None: