more work
This commit is contained in:
11
README.md
11
README.md
@@ -1,9 +1,20 @@
|
||||
# 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
|
||||
|
||||
### Python
|
||||
|
||||
```shell
|
||||
python -m restic.snapshots
|
||||
```
|
||||
|
||||
```shell
|
||||
python -m restic.backup
|
||||
```
|
||||
|
||||
@@ -2,3 +2,4 @@ uv
|
||||
ruff
|
||||
rich
|
||||
requests
|
||||
click
|
||||
@@ -8,12 +8,13 @@ from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
from rich.progress import Progress
|
||||
|
||||
from restic import loki, snapshots
|
||||
from restic.console import console, logger
|
||||
from restic.loki import send_to_loki
|
||||
|
||||
|
||||
def run(backup_dir: Path, loki_url: str = None):
|
||||
cmd = f'restic backup {backup_dir} --json --tag python-script'
|
||||
def run(backup_dir: Path, loki_url: str = None, tag: str = 'python-script'):
|
||||
cmd = f'restic backup {backup_dir} --json --tag {tag}'
|
||||
|
||||
logger.debug(f'Running cmd [bright_black]{cmd}[/]')
|
||||
process = subprocess.Popen(
|
||||
@@ -54,14 +55,16 @@ def run(backup_dir: Path, loki_url: str = None):
|
||||
envvar='BACKUP_DIR',
|
||||
)
|
||||
@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()
|
||||
|
||||
logging.basicConfig(
|
||||
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__':
|
||||
|
||||
@@ -22,4 +22,9 @@ def send_to_loki(loki_url: str, line: str, backup: str):
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
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}'
|
||||
)
|
||||
|
||||
@@ -10,21 +10,15 @@ from restic.loki import send_to_loki
|
||||
|
||||
|
||||
def run(loki_url: str = None):
|
||||
cmd = 'restic snapshots --json'
|
||||
cmd = ['restic', 'snapshots', '--json']
|
||||
|
||||
logger.debug(f'Running cmd [bright_black]{cmd}[/]')
|
||||
process = subprocess.Popen(
|
||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True
|
||||
)
|
||||
with console.status('Getting snapshots...'):
|
||||
logger.debug(f'Running cmd [bright_black]{" ".join(cmd)}[/]')
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
|
||||
with process.stdout:
|
||||
for line in iter(process.stdout.readline, ''):
|
||||
try:
|
||||
data = json.loads(line)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
else:
|
||||
console.print(data)
|
||||
line = result.stdout
|
||||
data = json.loads(line)
|
||||
console.print(data)
|
||||
|
||||
if loki_url is not None:
|
||||
send_to_loki(loki_url=loki_url, line=line, backup='snapshots')
|
||||
|
||||
Reference in New Issue
Block a user