More - read comments in script.
I was inspired by http://www.zoneminder.com/forums/viewtopic.php?t=9466
Code: Select all
#!/bin/bash
# zm_free_space
# Script used for deleting ZoneMinder Events when amount of free space on
# partition is below minimum
# Only the oldest and not archived events are deleted.
# Script created by Wojciech Sciesinski wojteks [ at ] wojteks [dot] net
# Script version 0.1 - 2008.08.08
# Tested with ZoneMinder v. 1.22.3
# ZoneMinder events must be stored on another partition than another system data
# - use symlink and FollowSymLinks in Apache configuration
# Finding events for delete take a lot of time ! then don't create a big partition or
# don't run this script frequently
# Script based on http://www.zoneminder.com/forums/viewtopic.php?t=9466
# Standard configuration file for ZoneMinder
ZM_CONFIG=/etc/zm.conf
# Read ZM_CONFIG
. $ZM_CONFIG
# Volume with ZoneMinder Data
ZM_DATA_VOLUME=/dev/hda7
# Mount point for ZM_DATA_VOLUME
ZM_DATA_MOUNT_POINT=/mnt/hda7
# Minimal amount of free space on ZM_DATA_VOLUME (kB)
# 1GB=1048576kB
ZM_MINIMAL_SPACE=1048576
# First delete files/events older then - in days
ZM_FIRST_DELETE=7
# Events the are the younger then is not deleted - in days
ZM_LAST_DELETE=1
# Log file for script activity
# Remember about adding this log file to logrotate !
ZM_SCRIPT_LOG=/var/log/zm/zm_free_space.log
function free_space(){
ZM_FREE_SPACE=`df $ZM_DATA_VOLUME | grep -v Filesystem | awk '{ print $4 }'`
#echo "Free space is "$ZM_FREE_SPACE
if [ $ZM_FREE_SPACE -lt $ZM_MINIMAL_SPACE ]
then
# Not enough free space
return 0
else
#Enough free space
return 1
fi
}
N=$ZM_FIRST_DELETE
#Iterating for days
while [ $N -ge $ZM_LAST_DELETE ] ;
do
#Checking amount free space on partition
free_space
if [ $? -eq 0 ]
then
#echo "Not enough free space !!"
#echo "I'm looking for directores/events older than "$N
for i in `nice -n 19 find $ZM_DATA_MOUNT_POINT -type d -mtime +$N`
do
ZM_EVENT_ID=`basename $i`
#echo "Current event number is " $ZM_EVENT_ID
#Selecting archive status for event from database
x=`mysql -h$ZM_DB_HOST -u$ZM_DB_USER -p$ZM_DB_PASS << YYYY
use $ZM_DB_NAME;
select Archived from Events
where id = $ZM_EVENT_ID;
quit
YYYY
`
#It's not work correctly :-(
#Deleting directory if data for event is not in database
# if [ -n "$x" ]
# then
# rm -r $i
#
# logger -f $ZM_SCRIPT_LOG -p local3.info Only files for ZoneMinder event number $ZM_EVENT_ID has been deleted from $i
#
# continue
#
# fi
#Preparing string for comparision
y=`echo $x | cut -c 10`
#Checking archive status for event
if [ $y -eq 1 ]
then
continue
fi
# Moving event to another disc in next this script release ?
#echo "Deleting event number"$ZM_EVENT_ID
# Deleting event data from disc
rm -r $i
# Deleting event data from database
a=`mysql -h$ZM_DB_HOST -u$ZM_DB_USER -p$ZM_DB_PASS << XXXX
use $ZM_DB_NAME;
delete from Events where id = $ZM_EVENT_ID;
delete from Frames where EventId = $ZM_EVENT_ID;
quit
XXXX
`
# Sending info to syslog
logger -f $ZM_SCRIPT_LOG -p local3.info ZoneMinder event number $ZM_EVENT_ID has been deleted
#Checking amount of free space on partition
free_space
# Next event is deleted only if necessary
if [ $? -ne 0 ]
then
break
fi
done
else
break
fi
let "N -= 1"
done
#Checking amount of free space on partition
free_space
if [ $? -eq 0 ]
then
logger -f $ZM_SCRIPT_LOG -p local3.error Not enough free space on $ZM_DATA_VOLUME but not events to delete
fi