Files
cleanup-smartcam-data/README.md
T
2026-08-06 18:33:25 +08:00

181 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# cleanup-smartcam-data 部署说明
![cleanup-smartcam-data 自动清理策略](assets/cleanup-smartcam-data-flow.png)
## 文件结构
```text
cleanup-smartcam-data/
├── README.md
├── assets/
│ ├── cleanup-smartcam-data-flow.svg(旧版源文件)
│ └── cleanup-smartcam-data-flow.png(当前流程图)
├── bin/
│ └── cleanup-smartcam-data
├── systemd/
│ ├── cleanup-smartcam-data.service
│ ├── cleanup-smartcam-data-resume.timer
│ └── cleanup-smartcam-data.timer
└── scripts/
├── install.sh
└── uninstall.sh
```
## RK3588 安装位置
| 项目文件 | 设备安装位置 |
|---|---|
| `bin/cleanup-smartcam-data` | `/usr/local/sbin/cleanup-smartcam-data` |
| `systemd/cleanup-smartcam-data.service` | `/etc/systemd/system/cleanup-smartcam-data.service` |
| `systemd/cleanup-smartcam-data.timer` | `/etc/systemd/system/cleanup-smartcam-data.timer` |
| `systemd/cleanup-smartcam-data-resume.timer` | `/etc/systemd/system/cleanup-smartcam-data-resume.timer` |
运行状态目录由程序自动创建:
```text
/var/lib/cleanup-smartcam-data/
/run/cleanup-smartcam-data.lock
/userdata/data/.recording-paused-low-disk
```
## 环境要求
- smart-cam 容器名:`smart-cam`
- 数据目录:`/userdata/data`
- 容器内成功记录:`/smart-cam/kafka_sender/processed_files.log`
- `smart_camera` 支持读取 `/smart-cam/data/.recording-paused-low-disk`
(与宿主机 `/userdata/data/.recording-paused-low-disk` 是同一挂载文件)
- 宿主机使用 Docker 和 systemd
## 安装
将整个目录复制到目标设备,例如:
```text
/home/zhangw/cleanup-smartcam-data
```
执行:
```bash
cd /home/zhangw/cleanup-smartcam-data
sudo ./scripts/install.sh
```
安装脚本会:
1. 校验清理脚本语法;
2. 安装程序和 systemd unit;
3. 启用 `cleanup-smartcam-data.timer`;
4. 删除旧名称 `userdata-video-cleanup` 的安装文件;
5. 不停止或重启 smart-cam Docker。
## 清理规则
- 开机2分钟后首次检查;
- 之后每60分钟检查一次;
- 每次定时检查都会安全清理已确认上传成功的数据,不以空闲空间阈值作为清理条件;
- 只清理 `processed_files.log` 中确认成功、且超过60分钟的数据;
- 同时删除同一任务的:
```text
/userdata/data/videos/<base>_annotated.mp4
/userdata/data/videos_raw/<base>_raw.mp4
/userdata/data/annotations/<base>.xml
```
- 超过60分钟的0字节 raw,如果未被进程占用,也会按上述三件套删除;
- 每次运行都会对`videos_raw/videos/annotations`按base进行完整性对账;
- 三个成员齐全才视为完整三件套;
- 缺少任意成员且所有现存成员均超过60分钟、未被进程打开时,删除该base的全部残留;
- 新录制、仍被进程打开或未满60分钟的文件不会被完整性对账误删;
- 已确认上传的正常清理在删除前会再次要求raw、annotated和XML三者齐全,缺一项整组跳过;
- scan 以一次扫描目录为原子单位;只有全部小图和`meta.json`的SFTP
上传均完成、Kafka消息确认成功,并写入
`/userdata/data/upload_processed_scans.log`后才允许清理;
- 已确认的scan还必须超过60分钟、`meta.json`状态为`completed`且目录内
没有文件被进程打开,随后才会整目录删除;
- 对`<base>_raw.mp4.partN.mp4`录制残留执行整组安全检查:
- 同一base的所有part都必须超过60分钟、全部为0字节且均未被进程打开;
- 如果正式`<base>_raw.mp4`已经存在,只删除符合条件的残留part,保留正常三件套;
- 如果正式raw不存在,删除全部残留part及对应的annotated视频和XML;
- 任一part仍较新、非0字节或正在使用时,整组跳过;
- 空闲低于3GiB时只创建“禁止录像”标记,立即结束正在生成的录像,并停止产生新的
`videos`、`videos_raw`、`annotations`;持续检测、推理、RTSP/公网直播、scan和
Producer上传均不暂停;
- 禁止录像期间每2分钟检查一次;
- 清理使空闲恢复到5GiB后删除标记并自动允许录像;持续检测一直运行,之后达到
15/25鸟类确认门控即可重新产生三件套,并停止2分钟快速检查;
- 从旧版本升级时,如果发现旧的 `capture-paused` 状态,程序会先向
`smart_camera` 发送一次 `SIGCONT` 并删除旧状态,之后不再使用
`SIGSTOP/SIGCONT` 控制整个进程;
- 无法读取成功记录时不删除数据。
## 验证
```bash
sudo bash -n /usr/local/sbin/cleanup-smartcam-data
systemctl status cleanup-smartcam-data.timer
systemctl list-timers --all | grep cleanup-smartcam-data
sudo systemctl start cleanup-smartcam-data.service
systemctl status cleanup-smartcam-data.service
journalctl -u cleanup-smartcam-data.service -n 100 --no-pager
```
确认成功记录可读:
```bash
docker exec smart-cam \
test -r /smart-cam/kafka_sender/processed_files.log
test -r /userdata/data/upload_processed_scans.log
```
确认 Docker 未重启:
```bash
docker inspect smart-cam --format \
'running={{.State.Running}} restart={{.RestartCount}} started={{.State.StartedAt}}'
```
## 查看日志
```bash
journalctl -u cleanup-smartcam-data.service -f
docker logs -f -t --tail 200 smart-cam
```
日志标识:
```text
[DISK_GUARD][INFO]
[DISK_GUARD][WARNING]
[DISK_GUARD][CRITICAL]
[DISK_GUARD][RECOVERED]
```
## 升级
在目标设备进入新版目录后重新执行:
```bash
cd /home/zhangw/cleanup-smartcam-data
sudo ./scripts/install.sh
```
## 卸载
```bash
cd /home/zhangw/cleanup-smartcam-data
sudo ./scripts/uninstall.sh
```
卸载不会自动删除当前的录像禁止标记;如需立即恢复录像,请确认空间充足后删除:
```text
/userdata/data/.recording-paused-low-disk
```