"""The host's own backups a restart would abort (napalm-device-types' ``RunningBackupsMixin``). A file-level backup on a Linux host is a process of a backup tool -- restic, borg, borgmatic, duplicity, rsnapshot and the like -- started by a systemd timer, a cron line or by hand. One round trip, as the login user (every process's command line and cgroup are readable to it), reads the host's clock, the processes with their age, and each one's cgroup; the systemd service a backup runs in is its job, the service a timer starts, which is how netOrk shows the timer's scheduled job. What a tool is doing is read from its subcommand: ``restic backup`` is a backup, ``restic forget`` a prune, ``restic mount`` none. ``borg serve`` counts: it is the receiving end of another host's backup. A ``ps`` without ``etimes`` (busybox) fails the reading rather than report nothing. """ from __future__ import annotations import posixpath import re from typing import TYPE_CHECKING, NamedTuple from napalm_device_types import RunningBackupDict, RunningBackupKind, RunningBackupsMixin #: ``NOW ``, one ``pidelapsedcgroupargs`` line per process, ``END``. RUNNING_BACKUPS_COMMAND = ( 'echo "NOW $(date +%s)"; ' "out=$(LC_ALL=C ps -eo pid=,etimes=,args= 2>/dev/null) || exit 1; " "printf '%s\\n' \"$out\" | while read -r pid et args; do " 'printf \'%s\\t%s\\t%s\\t%s\\n\' "$pid" "$et" ' '"$(tail -n 1 /proc/$pid/cgroup 2>/dev/null)" "$args"; ' "done; echo END" ) _READ_TIMEOUT = 30 class _Tool(NamedTuple): #: subcommand -> what it does; None: not a backup (a mount, a restore, a listing). subcommands: dict[str, RunningBackupKind | None] #: What the tool does when no listed subcommand is given; None: nothing to stop for. default: RunningBackupKind | None = None _RESTIC: dict[str, RunningBackupKind | None] = { "backup": "backup", "forget": "prune", "prune": "prune", "check": "verify", "copy": "sync", } _BORG: dict[str, RunningBackupKind | None] = { "create": "backup", "serve": "backup", "prune": "prune", "compact": "prune", "check": "verify", } _TOOLS: dict[str, _Tool] = { "restic": _Tool(_RESTIC), "resticprofile": _Tool(_RESTIC), "autorestic": _Tool({"backup": "backup", "forget": "prune", "check": "verify"}), "borg": _Tool(_BORG), "borgbackup": _Tool(_BORG), "borgmatic": _Tool( {"extract": None, "list": None, "info": None, "restore": None, "mount": None}, "backup", ), "duplicity": _Tool( { "restore": None, "collection-status": None, "list-current-files": None, "verify": "verify", "cleanup": "prune", "remove-older-than": "prune", "remove-all-but-n-full": "prune", "remove-all-inc-of-but-n-full": "prune", }, "backup", ), "duply": _Tool( { "backup": "backup", "bkp": "backup", "incr": "backup", "full": "backup", "purge": "prune", "verify": "verify", } ), "rsnapshot": _Tool({}, "backup"), "rdiff-backup": _Tool( {"-r": None, "--restore-as-of": None, "restore": None, "list": None}, "backup" ), "proxmox-backup-client": _Tool({"backup": "backup"}), "kopia": _Tool({"snapshot": "backup", "maintenance": "prune"}), "btrbk": _Tool({"run": "backup", "resume": "backup", "snapshot": "backup", "prune": "prune"}), } #: What may stand before the tool on its command line: an interpreter or a wrapper, #: their options and plain numbers or durations ("nice -n 19", "timeout 3h"). _WRAPPERS = frozenset( {"sh", "bash", "dash", "env", "nice", "ionice", "nocache", "timeout", "chrt", "perl"} ) _PYTHON = re.compile(r"python[0-9.]*") _ARGUMENT = re.compile(r"-.*|\d+[smhd]?") #: Services that start other people's commands: a backup run from them has no job of its own. _NOT_JOBS = frozenset( {"cron.service", "crond.service", "cronie.service", "anacron.service", "atd.service"} | {"ssh.service", "sshd.service"} ) def _tool(args: str) -> tuple[_Tool, list[str]] | None: """The backup tool *args* runs and its arguments, or None for any other process.""" words = args.split() for index, word in enumerate(words): name = posixpath.basename(word) if name in _TOOLS: return _TOOLS[name], words[index + 1 :] if not (name in _WRAPPERS or _PYTHON.fullmatch(name) or _ARGUMENT.fullmatch(word)): return None return None def backup_kind(args: str) -> RunningBackupKind | None: """What the process with command line *args* is doing for a backup, or None.""" found = _tool(args) if found is None: return None tool, rest = found for word in rest: if word in tool.subcommands: return tool.subcommands[word] return tool.default def _job(cgroup: str) -> str | None: """The systemd service a process runs in, when that service is its own job.""" unit = posixpath.basename(cgroup.rsplit(":", 1)[-1]) return unit if unit.endswith(".service") and unit not in _NOT_JOBS else None def _clock(lines: list[str]) -> int: if not lines or not lines[0].startswith("NOW "): raise RuntimeError("get_running_backups: the host did not say what time it is") try: return int(lines[0].split()[1]) except (IndexError, ValueError) as exc: raise RuntimeError(f"get_running_backups: not a time: {lines[0]!r}") from exc def parse_running_backups(output: str) -> list[RunningBackupDict]: """The backups in ``RUNNING_BACKUPS_COMMAND``'s *output*. :raises RuntimeError: when the reading was cut short or never had the clock. """ lines = output.splitlines() now = _clock(lines) if lines[-1].strip() != "END": raise RuntimeError("get_running_backups: the process list was cut short") found: list[RunningBackupDict] = [] for line in lines[1:-1]: parts = line.split("\t", 3) if len(parts) != 4 or not parts[1].isdigit(): continue pid, elapsed, cgroup, args = parts kind = backup_kind(args) if kind is not None: since = now - int(elapsed) found.append( {"kind": kind, "job": _job(cgroup), "task": pid, "guests": [], "since": since} ) return found class LinuxRunningBackupsMixin(RunningBackupsMixin): """``get_running_backups`` over the driver's own session, as the login user.""" if TYPE_CHECKING: # pragma: no cover - the driver's own, declared for type checkers def _send(self, command: str, read_timeout: float = 100) -> str: ... def get_running_backups(self) -> list[RunningBackupDict]: return parse_running_backups(self._send(RUNNING_BACKUPS_COMMAND, _READ_TIMEOUT))