Compare commits

..

8 Commits

Author SHA1 Message Date
John Lancaster
e17894e49c added docker sdk to dependencies 2024-06-15 15:49:34 -05:00
John Lancaster
326d2c0910 added to gitignore 2024-06-15 15:08:05 -05:00
John Lancaster
08e02d12db improved logic in forget 2024-06-03 21:10:21 -05:00
John Lancaster
80842bc09c added prune before getting the size 2024-06-03 21:04:30 -05:00
John Lancaster
4e7fa8c87a improved printout 2024-06-03 20:52:52 -05:00
John Lancaster
2e2a777b11 improved regex for printout 2024-06-03 20:50:02 -05:00
John Lancaster
3957e0c869 updated comment 2024-06-03 20:27:44 -05:00
John Lancaster
cae3f2bbf4 type hint 2024-05-28 18:33:12 -05:00
7 changed files with 21 additions and 10 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
__pycache__ __pycache__
*.egg-info *.egg-info
build/ build/
.env
key

View File

@@ -10,8 +10,8 @@ Purpose:
Recommended to put these in the relevant `~/.bashrc` file Recommended to put these in the relevant `~/.bashrc` file
| Env Variable | Description | | Env Variable | Description |
| ------------------- | ------------------------------------------------------------------------------------------ | |---------------------|--------------------------------------------------------------------------------------------|
| `HOSTNAME` | Network hostname of where the backup is running | | `HOSTNAME` | Network hostname of where the backup is running. Used to tag the backups in restic |
| `BACKUP_DIR` | Directory to back up | | `BACKUP_DIR` | Directory to back up |
| `RESTIC_REPOSITORY` | Directory for the restic repository. This is usually on a mount point made from Proxmox | | `RESTIC_REPOSITORY` | Directory for the restic repository. This is usually on a mount point made from Proxmox |
| `RESTIC_PASSWORD` | Password for the restic repository | | `RESTIC_PASSWORD` | Password for the restic repository |

View File

@@ -12,4 +12,4 @@ authors = [
license = { file = "LICENSE" } license = { file = "LICENSE" }
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = ["rich", "requests", "click"] dependencies = ["rich", "requests", "click", "docker"]

View File

@@ -3,3 +3,4 @@ ruff
rich rich
requests requests
click click
docker

View File

@@ -18,7 +18,7 @@ def manage_containers(project: str, services: list[str]):
for c in client.containers.list() for c in client.containers.list()
if c.labels['com.docker.compose.project'] == project if c.labels['com.docker.compose.project'] == project
) )
service_dict = { service_dict: dict[str, Container] = {
c.labels['com.docker.compose.service']: c for c in project_containers c.labels['com.docker.compose.service']: c for c in project_containers
} }
containers: list[Container] = [service_dict[s] for s in services] containers: list[Container] = [service_dict[s] for s in services]

View File

@@ -10,6 +10,7 @@ from rich.logging import RichHandler
from restic import size, snapshots from restic import size, 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
from restic.prune import prune
class KeepStrategy(BaseModel): class KeepStrategy(BaseModel):
@@ -85,8 +86,12 @@ def main(loki_url: str, dry_run: bool, **kwargs):
except Exception: except Exception:
console.print_exception() console.print_exception()
else: else:
snapshots.snapshot(loki_url) prune(loki_url, dry_run)
size.get_size(loki_url) if not dry_run:
snapshots.snapshot(loki_url)
size.get_size(loki_url)
else:
logger.debug('Skipping getting size because of dry run')
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -12,7 +12,7 @@ from restic.console import console, logger
from restic.loki import send_to_loki from restic.loki import send_to_loki
field_regex = re.compile( field_regex = re.compile(
r'^(?P<name>[\w ]+):\s+(?P<blobs>\d+) blobs \/ (?P<size>\d+(\.\d+)? (M|G)iB)', re.MULTILINE r'^(?P<name>[\w ]+):\s+(?P<blobs>\d+) blobs \/ (?P<size>\d+(\.\d+)? (?:Mi|Gi)?B)', re.MULTILINE
) )
@@ -23,6 +23,8 @@ def convert_size(size_str: str) -> int:
scale = 8589934592 scale = 8589934592
elif size_str.endswith('MiB'): elif size_str.endswith('MiB'):
scale = 8388608 scale = 8388608
else:
scale = 1
return int(round(base_size * scale)) return int(round(base_size * scale))
@@ -48,7 +50,7 @@ def prune(loki_url: str = None, dry_run: bool = False):
sys.exit(1) sys.exit(1)
d = {f['name']: {'blobs': f['blobs'], 'size': f['size']} for f in field_gen(result.stdout)} d = {f['name']: {'blobs': f['blobs'], 'size': f['size']} for f in field_gen(result.stdout)}
console.print(d) logger.debug(json.dumps(d, indent=4))
if loki_url is not None and not dry_run: if loki_url is not None and not dry_run:
send_to_loki(loki_url, line=json.dumps(d), backup='prune') send_to_loki(loki_url, line=json.dumps(d), backup='prune')