Script for Deleting Old Exported Video Folders

Forum for questions and support relating to the 1.28.x releases only.
Locked
User avatar
snake
Posts: 337
Joined: Sat May 21, 2016 2:20 am

Script for Deleting Old Exported Video Folders

Post by snake »

Code: Select all

#!/bin/bash
FILESYSTEM=/dev/sdc1
MAXCAPACITY=90

#deprecated
#DIRTODEL=/videodir/
LOGFILE=/videodir/log

#delete oldest two folders
FILESTODEL=2
#array of all folders to delete old folders from. 
FOLDERS=(       "/videodir/monitor1/"
			  "/videodir/monitor2/" )
			  
			  for folderstodel in "${FOLDERS[@]}"
        do
#check if devsdc1 capacity from df is gt / greater than of maxcapacityallowed
        if [ $(df -P $folderstodel  | awk '{gsub("%",""); capacity=$5}; END { print capacity }') -gt $MAXCAPACITY ]
                then 

# cd to dir, list in order to time modified, list last 2, send that list to rm (delete newlines)
# added var for tail, and rf for rm, since i am deleting directories
                        cd $folderstodel && ls -t | tail -n $FILESTODEL | xargs -d '\n'  rm -rf ;
                        echo 'deleted 2 oldest folders at the below time' >> $LOGFILE;
                        date >> $LOGFILE;

# not passing | to ls but using && as we are not passing return values or stdout
                else
                        echo 'not full yet at the below time' >> $LOGFILE;
                        date >> $LOGFILE;
                fi
done


User avatar
snake
Posts: 337
Joined: Sat May 21, 2016 2:20 am

Re: Script for Deleting Old Exported Video Folders

Post by snake »

Here is the other script I made to put created videos exported by the filter into daily directories.

I am no programmer, so bash away.

From https://wiki.zoneminder.com/Dummies_Guide

Code: Select all

#!/bin/bash

#list of all current camera folders
FOLDERS=(       "/videodir1/monitor1/"
			  "/videodir1/monitor2/" )



#in order to use command here, you must use `command` or $(command)
DAY=`date -I`



for folderstomove in "${FOLDERS[@]}"
        do
# must include cd in path or it will default to orig pwd
# xargs will pass results of echo into mkdir
                cd $folderstomove && echo $DAY | xargs mkdir 
                cd $folderstomove && mv ./*avi $DAY
        done


#script ran once a day

User avatar
snake
Posts: 337
Joined: Sat May 21, 2016 2:20 am

Re: Script for Deleting Old Exported Video Folders

Post by snake »

Previous delete code only walked through as many array members as needed until capacity was under.

This will pass through the complete array, if capacity over. This way, the first few folders are not the only ones deleted from.

Had issue with if syntax, when moving things. All if must be completed with fi in bash.

Code: Select all

#!/bin/bash
FILESYSTEM=/dev/sdc1

#percent
MAXCAPACITY=90

LOGFILE=/videodir/log

#delete one oldest folder item from each folder
# if you organize by day, it deletes one day from each folder
FILESTODEL=1

#array of all folders to delete old folders from. 
FOLDERS=(       "/videodir/monitor1/" 
                "/videodir/monitor2/"    )



# have it check filesystem size first, and if over capacity, 
# delete one folder, ergo one day from each monitor

#check if file partition capacity from df is gt / greater than of maxcapacityallowed

if [ $(df -P $FILESYSTEM  | awk '{gsub("%",""); capacity=$5}; END { print capacity }') -gt $MAXCAPACITY ]
        then 

        for folderstodel in "${FOLDERS[@]}"
                do

# cd to dir, list in order to time modified, list last 1, send that list to rm (delete newlines)
# added var for tail, and rf for rm, since i am deleting directories
                        cd $folderstodel && ls -t | tail -n $FILESTODEL | xargs -d '\n'  rm -rf ;
                        echo 'deleted 1 oldest folders from one monitor at the below time' >> $LOGFILE;
                        echo "monitor name is:" >> $LOGFILE;
                        echo $folderstodel >> $LOGFILE;
                        date >> $LOGFILE;

# not passing | to ls but using && as we are not passing return values or stdout
                done

        else
                echo 'not full yet at the below time' >> $LOGFILE;
                date >> $LOGFILE;

fi
Locked