Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kevinmilner/56d1610acd139524a22f1b6ac7288f75 to your computer and use it in GitHub Desktop.
Save kevinmilner/56d1610acd139524a22f1b6ac7288f75 to your computer and use it in GitHub Desktop.
Synology Surveillance Station USB storage archive workaround cron job
#!/bin/bash
# Synology Surveillance Station doesn't let you store recordings on USB storage, which means that you must
# use the internal (often mirrored) disks. This is less than ideal. Unfortunately, just symbolically
# linking the surveillance store directory to one on USB storage causes surveillance station to fail after
# a day or so. This workaround still uses your main storage for recording, but moves older files to USB storage
# for archival. The moved files themselves are symbolically linked back to the original storage location.
#
# Script should be run regularly, either by editing /etc/crontab or through Synology's built in script scheduling.
# Be sure to edit the user inputs below, and set up SS to keep recordings for longer than the external archive period.
#
# I run in via cron with this entry in my /etc/crontab:
# #minute hour mday month wday who command
# 49 * * * * root /volume1/synology_surveillance_usb_archiver.sh >> /volume1/synology_surveillance_usb_archiver_log.txt
# *********** BEGIN USER INPUTS ***********
# source directory where you store your surveillance footage
SOURCE="/volume1/surveillance"
# desination directory where you which to archive surveillance footage
DEST="/volumeUSB1/usbshare1-2/surveillance"
# media at least this many minutes old will be moved to external storage
MINS=1440
# archives at least this many days old will be purged permanently.
EXT_ARCHIVE_DAYS=14
# *********** END USER INPUTS ***********
echo "**************************"
date
if [[ ! -e $DEST ]];then
mkdir $DEST
fi
# process any new recordings
echo "Looking for new recordings to move..."
for SUBDIR in `find $SOURCE -mindepth 1 -maxdepth 1 -type d`
do
# echo "Processing $SUBDIR"
DIRNAME=`basename $SUBDIR`
for MEDIA in `find $SUBDIR -mindepth 2 -maxdepth 2 -mmin +$MINS -type f -name '*.mp4'`
do
FILENAME=`basename $MEDIA`
DATE_PATH=`dirname $MEDIA`
DATE_NAME=`basename $DATE_PATH`
echo " Processing media: $FILENAME for camera $DIRNAME on $DATE_NAME"
SUB_DEST="$DEST/$DIRNAME/$DATE_NAME"
echo " Moving $MEDIA to $SUB_DEST"
mkdir -p $SUB_DEST
mv $MEDIA $SUB_DEST/
ln -s $SUB_DEST/$FILENAME $MEDIA
done
done
echo "DONE"
echo
# now purge archives
echo "Looking for old archived recordings to purge..."
for MEDIA in `find $DEST -mindepth 3 -maxdepth 3 -mtime +$EXT_ARCHIVE_DAYS -type f -name '*.mp4'`
do
echo " Removing $MEDIA"
FILENAME=`basename $MEDIA`
DATE_PATH=`dirname $MEDIA`
DATE_NAME=`basename $DATE_PATH`
CAM_PATH=`dirname $DATE_PATH`
CAM_NAME=`basename $CAM_PATH`
echo " Removing $FILENAME from $DATE_PATH"
rm $MEDIA
LINK_DIR="$SOURCE/$CAM_NAME/$DATE_NAME"
echo " Removing link from $LINK_DIR"
rm $LINK_DIR/$FILENAME
done
echo "DONE"
echo
# now purge empty directories
echo "Looking for empty recording directories to purge..."
for MDIR in `find $SOURCE -mindepth 2 -maxdepth 2 -type d -empty -name '20*'`
do
echo " Removing empty source dir: $MDIR"
rmdir $MDIR
done
for MDIR in `find $DEST -mindepth 2 -maxdepth 2 -type d -empty -name '20*'`
do
echo " Removing empty archive dir: $MDIR"
rmdir $MDIR
done
echo "DONE"
date
echo "**************************"
@Unlearn
Copy link

Unlearn commented Dec 6, 2017

Thank you for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment