Open akivacp opened 3 years ago
It won't work for that file structure. I made it with the following in mind:
/any/mount/path/WyzeCams/
Feel free to modify the script to your needs. I'm not going to have much time to work on this, as I'm trying to work on HclX/WyzeHacks #62. If I have some time after that issue, I'll revisit this one.
i asked ChatGPT, and here's the code it provided.
To set up this script for a multi-location NFS server where you have multiple cameras in different locations (like the structure you described), you'll need to modify the script slightly to accommodate the hierarchy of directories. Here’s a step-by-step guide on how to adjust the script:
1. Modify the
get_dirs
Function Update the get_dirs function to handle the additional level of subdirectories representing different houses or locations.2. Example Structure Handling Given the structure:
/mnt/usb/WyzeCams/ ├───House1 │ ├───FrontDoor │ └───Garage └───House2 ├───Backyard └───Driveway
3. Script Adjustments Here is the adjusted script:
#! /usr/bin/env bash function set_vars() { # Read in env variables set -o allexport; source .env; set +o allexport; # Append the `WyzeCams/` directory to the NFS root case "$NFS_ROOT" in */) NFS_ROOT="${NFS_ROOT}WyzeCams/" ;; *) NFS_ROOT="${NFS_ROOT}/WyzeCams/" ;; esac # Init vars to store dirs in CAM_REC_DIR_LIST=() VID_DIR_LIST=() } function get_dirs() { # Loop through all locations in the $NFS_ROOT for loc_dir in ${NFS_ROOT}*; do # Test that the location is a directory if [ -d "$loc_dir" ]; then # Loop through all camera directories in the location for cam_dir in $loc_dir/*; do # Test that the camera directory is a directory if [ -d "$cam_dir" ]; then # For each record directory in the camera directory for cam_rec_dir in $cam_dir/record/*; do # Test that the record directory is a directory if [ -d "$cam_rec_dir" ]; then # Store the record directories in the array CAM_REC_DIR_LIST+=("$cam_rec_dir") # Loop through video directories in the record directory for vid_dir in $cam_rec_dir/*; do # Test that the video directory is a directory if [ -d "$vid_dir" ]; then # Store the video directories in an array VID_DIR_LIST+=("$vid_dir") fi done fi done fi done fi done } function is_file_young() { # Test that the file exists if [ ! -f "$1" ]; then # NFS might have dropped? let's exit echo "file ${1} does not exist!" exit 1 fi # seconds in $REC_RETENTION_HOURS hours MAXAGE=$(bc <<< "${REC_RETENTION_HOURS}*60*60") # file age in seconds = current_time - file_modification_time. FILEAGE=$(($(date +%s) - $(stat -c '%Y' "${1}"))) # Test the file age against the $REC_RETENTION_HOURS test $FILEAGE -lt $MAXAGE && { # If the file is young return true #echo "${1} is less than ${REC_RETENTION_HOURS} hours old" return 0 } # If the file is old return false #echo "${1} is older than ${REC_RETENTION_HOURS} hours" return 1 } function clean_recordings() { # Loop through all video dirs for dir in "${VID_DIR_LIST[@]}"; do # Ensure that it is a directory if [ -d "$dir" ]; then # Loop through all MP4 files in the directory for vid in "$dir"/*.mp4; do # Ensure that $vid is a file if [ -f "$vid" ]; then # Ensure the video is older than the configured value of $REC_RETENTION_HOURS if ! is_file_young "$vid"; then echo "removing old file: $vid" rm "$vid" fi fi done fi done } function prune_dirs() { # Loop over all video dirs for dir in "${VID_DIR_LIST[@]}"; do # Ensure that it is a directory if [ -d "$dir" ]; then # Test if the dir is empty if [ ! "$(ls -A "$dir")" ]; then echo "removing empty directory: $dir" rm -rf "$dir" fi fi done # Loop over all record dirs for dir in "${CAM_REC_DIR_LIST[@]}"; do # Ensure that it is a directory if [ -d "$dir" ]; then # Test if the dir is empty if [ ! "$(ls -A "$dir")" ]; then echo "removing empty directory: $dir" rm -rf "$dir" fi fi done } set_vars; get_dirs; clean_recordings; prune_dirs;
Key Changes
- Iterate through locations: Added an extra loop to iterate through the locations (House1, House2, etc.).
- Nest camera directories: Adjusted the loops to accommodate the nested structure.
Assumptions
- The .env file correctly sets the NFS_ROOT variable.
- The directory structure adheres to the specified hierarchy.
- The record directories within each camera directory contain subdirectories with video files.
Testing After implementing these changes, run the script to ensure it correctly navigates through your directory structure, cleans old video files, and prunes empty directories. Adjust paths and environment variables as necessary for your specific setup.
I have a NFS server with multiple locations with multiple cameras. How would this script set up? For example