Page 1 of 1

Script - Better than PurgeWhenFull

Posted: Sat Jun 16, 2007 4:35 pm
by tburt11
I am running an older version of ZM, but I wanted to post this into the current release, as I believe it may be usefull still.

I have seven cameras, and the problem with PurgeWhenFull is that it does not pick the image directory that is fullest. When they fill up to 32K files, the images stop recording.

Deleting the images is only part of the problem, as there are still entries in the database which need to be purged as well.

I wrote these crummy scripts (I was lazy) and they do the job wonderfully. I wanted to share them.

The script also preserves your archived images.

Here is my crontab entry:

0 10 * * * /usr/local/bin/zmclean 1 > /dev/null 2>&1
1 10 * * * /usr/local/bin/zmclean 4 > /dev/null 2>&1
2 10 * * * /usr/local/bin/zmclean 7 > /dev/null 2>&1
3 10 * * * /usr/local/bin/zmclean 8 > /dev/null 2>&1
4 10 * * * /usr/local/bin/zmclean 9 > /dev/null 2>&1
5 10 * * * /usr/local/bin/zmclean 11 > /dev/null 2>&1
6 10 * * * /usr/local/bin/zmclean 12 > /dev/null 2>&1

zmclean:

#!/bin/bash

if [ x$1 == x ]
then
echo "missing param"
exit 1
fi

folder="/var/www/html/events/$1"

cd $folder

p=`/bin/pwd`

if [ x$p != x/var/www/html/events/$1 ]
then
echo "wrong folder"
exit 1
fi

/usr/bin/find . -type d -mtime +30 -exec /usr/local/bin/zmzap {} \;

and
zmzap:


#!/bin/bash

if [ x$1 == x ]
then
echo "missing param"
exit 1
fi

echo $1 | grep './'
if [ $? == 0 ]
then
targ=`echo $1 | /bin/cut -c 3-`
else
targ=$1
fi

if [ ! -d $targ ]
then
echo "missing target dir"
exit 1
fi


x=`/usr/bin/mysql -uzm -pzm << YYYY
use zm;
select Archived from Events where id = $targ;
quit
YYYY
`
y=`echo $x | /bin/cut -c 10`

if [ $y == 1 ]
then
echo "archived"
exit 0
fi

echo $targ
/bin/rm -rf $targ

/usr/bin/mysql -uzm -pzm << XXXX
use zm;
delete from Events where id = $targ;
delete from Frames where EventId = $targ;
quit
XXXX

Posted: Mon Jul 02, 2007 4:12 am
by grimsy
thanks for posting that. Works a charm.

Posted: Mon Mar 17, 2008 3:27 am
by ynn
I tried this script just now, and got this error messages

/usr/local/bin/zmzap: line 32: [: ==: unary operator expected 492869
ERROR 1045 (28000): Access denied using 'zm'@'localhost' (using password: YES

did i did wrong configuration?

Posted: Mon Mar 17, 2008 6:36 am
by timcraig
ynn wrote:I tried this script just now, and got this error messages

/usr/local/bin/zmzap: line 32: [: ==: unary operator expected 492869
ERROR 1045 (28000): Access denied using 'zm'@'localhost' (using password: YES

did i did wrong configuration?
You have a different Mysql username or password then what the script uses.

In the line:
x=`/usr/bin/mysql -uzm -pzm << YYYY

replace "-uzm" to "-u<your mysql username> and "-pzm" to "-p<your mysql password>"

Posted: Mon Mar 17, 2008 1:33 pm
by ynn
Thanks for quick reply, how many events will it be deleted by this script? like in Purgewhenfull we can set it like 300 events only when it reach 80% of disk capacity.

thanks.