more work
This commit is contained in:
11
README.md
11
README.md
@@ -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
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
uv
|
uv
|
||||||
ruff
|
ruff
|
||||||
rich
|
rich
|
||||||
requests
|
requests
|
||||||
|
click
|
||||||
@@ -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__':
|
||||||
|
|||||||
@@ -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}'
|
||||||
|
)
|
||||||
|
|||||||
@@ -10,21 +10,15 @@ 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, ''):
|
data = json.loads(line)
|
||||||
try:
|
console.print(data)
|
||||||
data = json.loads(line)
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception(e)
|
|
||||||
else:
|
|
||||||
console.print(data)
|
|
||||||
|
|
||||||
if loki_url is not None:
|
if loki_url is not None:
|
||||||
send_to_loki(loki_url=loki_url, line=line, backup='snapshots')
|
send_to_loki(loki_url=loki_url, line=line, backup='snapshots')
|
||||||
|
|||||||
Reference in New Issue
Block a user