Saturday, March 13, 2010

Archiving Surveillance Video

Script Time!
One using Bash to archive some files, and one using perl to send email alerts.

Even with my video captures only being triggered by motion... all the cars passing by, windy days, or cats that wander aimlessly around outside lead to images and .swf files to start building up fast. In the last week I had over 40000 .jpg files.. so along with turning down the sensitivity of the motion capture a bit, and moving a hanging plant out of the camera's view, I went ahead and made an archive script.

#!/bin/bash
#when archiving, toss the still images, keep the .swf videos. 
#This uses a for loop to do it because motion can actually create 
#more files than the rm command can handle by it's self.


for i in $(ls /motion/ |grep .jpg); do rm /motion/$i; done


#make a temp archive folder and drop the videos in it
mkdir /tmp/archv
mv /motion/*.swf /tmp/archv/.


#anything you use more than once should be a variable
timestamp=$(date |awk '{print $2$3"-"$6}')


#build all the videos into a timestamped tarball for storage
tar czvf archive-$timestamp.tar.gz /tmp/archv


#check to make sure the new archive was made
#if it wasnt, leave the tmp file alone and send an email alert
if [ -f archive-$timestamp.tar.gz ]
then
  rm -r /tmp/archv
else
  perl /root/mail.pl
fi


#while we're here, just check on the disk usage
#and send an email alert if its over 50%
if [ $(df -h |grep /dev/sda1|awk '{print $5}'|cut -d% -f1) -ge 50 ]
then
  /root/useagealert.pl
fi

The two perl scripts that are called are simple mailers, great little templates for interacting with sendmail. Here is an example of one:

#!/usr/bin/perl


$title='archive';
$to='me@myaddress.com';
$from= 'archive@myserver.org';
$subject='Archive Failed';


open(MAIL, "|/usr/sbin/sendmail -t");


## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "Archive process failed, please check the logs\n";


close(MAIL);

The archive script should then be added to the crontab, mine is set to archive twice a week for now, which will usually give me time to save images if I need to.

0 comments:

Post a Comment