Add feature to ignore cameras

Implements #1
This commit is contained in:
Sebastian Goscik
2022-02-21 00:35:02 +00:00
parent e491965e04
commit 0dd9e8e91b
4 changed files with 41 additions and 18 deletions

View File

@@ -17,6 +17,7 @@ ENV RCLONE_RETENTION=7d
ENV RCLONE_DESTINATION=my_remote:/unifi_protect_backup ENV RCLONE_DESTINATION=my_remote:/unifi_protect_backup
ENV VERBOSITY="v" ENV VERBOSITY="v"
ENV TZ=UTC ENV TZ=UTC
ENV IGNORE_CAMERAS=""
VOLUME [ "/root/.config/rclone/" ] VOLUME [ "/root/.config/rclone/" ]

View File

@@ -61,32 +61,39 @@ Options:
`gdrive:/backups/unifi_protect` [required] `gdrive:/backups/unifi_protect` [required]
--retention TEXT How long should event clips be backed up --retention TEXT How long should event clips be backed up
for. Format as per the `--max-age` argument for. Format as per the `--max-age` argument
of rclone` of `rclone`
(https://rclone.org/filtering/#max-age-don- (https://rclone.org/filtering/#max-age-don-
t-transfer-any-file-older-than-this) t-transfer-any-file-older-than-this)
--ignore-camera TEXT IDs of cameras for which events should not
be backed up. Use multiple times to ignore
multiple IDs. If being set as an environment
variable the IDs should be separated by
whitespace.
-v, --verbose How verbose the logging output should be. -v, --verbose How verbose the logging output should be.
None: Only log info messages created by None: Only log info messages created by
`unifi-protect-backup`, and all warnings `unifi-protect-backup`, and all warnings
-v: Only log info & debug messages created -v: Only log info & debug messages
by `unifi-protect-backup`, and all warnings created by `unifi-protect-backup`, and
all warnings
-vv: Log info & debug messages created by -vv: Log info & debug messages created
`unifi-protect-backup`, command output, and by `unifi-protect-backup`, command
all warnings output, and all warnings
-vvv Log debug messages created by `unifi- -vvv Log debug messages created by
protect-backup`, command output, all info `unifi-protect-backup`, command output,
messages, and all warnings all info messages, and all warnings
-vvvv: Log debug messages created by `unifi- -vvvv: Log debug messages created by
protect-backup` command output, all info `unifi-protect-backup` command output,
messages, all warnings, and websocket data all info messages, all warnings, and
websocket data
-vvvvv: Log websocket data, command output, -vvvvv: Log websocket data, command
all debug messages, all info messages and output, all debug messages, all info
all warnings [x>=0] messages and all warnings [x>=0]
--help Show this message and exit. --help Show this message and exit.
``` ```
@@ -99,6 +106,7 @@ always take priority over environment variables):
- `UFP_SSL_VERIFY` - `UFP_SSL_VERIFY`
- `RCLONE_RETENTION` - `RCLONE_RETENTION`
- `RCLONE_DESTINATION` - `RCLONE_DESTINATION`
- `IGNORE_CAMERAS`
## Docker Container ## Docker Container
You can run this tool as a container if you prefer with the following command. You can run this tool as a container if you prefer with the following command.

View File

@@ -32,6 +32,14 @@ from unifi_protect_backup import UnifiProtectBackup
help="How long should event clips be backed up for. Format as per the `--max-age` argument of " help="How long should event clips be backed up for. Format as per the `--max-age` argument of "
"`rclone` (https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this)", "`rclone` (https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this)",
) )
@click.option(
'--ignore-camera',
'ignore_cameras',
multiple=True,
envvar="IGNORE_CAMERAS",
help="IDs of cameras for which events should not be backed up. Use multiple times to ignore "
"multiple IDs. If being set as an environment variable the IDs should be separated by whitespace.",
)
@click.option( @click.option(
'-v', '-v',
'--verbose', '--verbose',

View File

@@ -3,7 +3,7 @@ import asyncio
import logging import logging
import pathlib import pathlib
import shutil import shutil
from typing import Callable, Optional from typing import Callable, List, Optional
import aiocron import aiocron
from pyunifiprotect import ProtectApiClient from pyunifiprotect import ProtectApiClient
@@ -167,6 +167,7 @@ class UnifiProtectBackup:
retention (str): How long should event clips be backed up for. Format as per the retention (str): How long should event clips be backed up for. Format as per the
`--max-age` argument of `rclone` `--max-age` argument of `rclone`
(https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this) (https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this)
ignore_cameras (List[str]): List of camera IDs for which to not backup events
verbose (int): How verbose to setup logging, see :func:`setup_logging` for details. verbose (int): How verbose to setup logging, see :func:`setup_logging` for details.
_download_queue (asyncio.Queue): Queue of events that need to be backed up _download_queue (asyncio.Queue): Queue of events that need to be backed up
_unsub (Callable): Unsubscribe from the websocket callback _unsub (Callable): Unsubscribe from the websocket callback
@@ -180,6 +181,7 @@ class UnifiProtectBackup:
verify_ssl: bool, verify_ssl: bool,
rclone_destination: str, rclone_destination: str,
retention: str, retention: str,
ignore_cameras: List[str],
verbose: int, verbose: int,
port: int = 443, port: int = 443,
): ):
@@ -197,6 +199,7 @@ class UnifiProtectBackup:
retention (str): How long should event clips be backed up for. Format as per the retention (str): How long should event clips be backed up for. Format as per the
`--max-age` argument of `rclone` `--max-age` argument of `rclone`
(https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this) (https://rclone.org/filtering/#max-age-don-t-transfer-any-file-older-than-this)
ignore_cameras (List[str]): List of camera IDs for which to not backup events
verbose (int): How verbose to setup logging, see :func:`setup_logging` for details. verbose (int): How verbose to setup logging, see :func:`setup_logging` for details.
""" """
setup_logging(verbose) setup_logging(verbose)
@@ -212,6 +215,7 @@ class UnifiProtectBackup:
verify_ssl=verify_ssl, verify_ssl=verify_ssl,
subscribed_models={ModelType.EVENT}, subscribed_models={ModelType.EVENT},
) )
self.ignore_cameras = ignore_cameras
self._download_queue: asyncio.Queue = asyncio.Queue() self._download_queue: asyncio.Queue = asyncio.Queue()
self._unsub: Callable[[], None] self._unsub: Callable[[], None]
@@ -312,6 +316,8 @@ class UnifiProtectBackup:
assert isinstance(msg.new_obj, Event) assert isinstance(msg.new_obj, Event)
if msg.action != WSAction.UPDATE: if msg.action != WSAction.UPDATE:
return return
if msg.new_obj.camera_id in self.ignore_cameras:
return
if msg.new_obj.end is None: if msg.new_obj.end is None:
return return
if msg.new_obj.type not in {EventType.MOTION, EventType.SMART_DETECT}: if msg.new_obj.type not in {EventType.MOTION, EventType.SMART_DETECT}: