#!/bin/sh #Set up the locations.... #SRC is where TVH puts the files - e.g. '/storage/TVHRecordings/Recent' #This must be set within TVHeadEnd as a combination of # the Recording system path - e.g. '/storage/TVHRecordings' # and the format string - e.g. 'Recent/$t/$t$_e_$c_%F_%R$n.$x' #DST is where we want to move them to within TVH - e.g. '/storage/TVHRecordings/Stored' #In this example the files would be move from '/storage/TVHRecordings/Recent' to '/storage/TVHRecordings/Stored' SRC=/storage/TVHRecordings/Recent DST=/storage/TVHRecordings/Stored #Set paths to the files we need #Where this script is - SRP=/storage/nasscripts #Set paths to the commands we need #Note - for LibreElec 'rsync' is not provided as standard and must be added as a Kodi addon PTHBIN=/usr/bin PTHRSY=/storage/.kodi/addons/virtual.network-tools/bin #First - make sure another rsync isn't running echo `date +"%d/%m/%Y %H:%M:%S"` "Checking for lockfile" >> $SRP/rsync2nas.log #Delete the lock if over 4 hours (240m) - due to this script failing half way through - e.g. due to power off $PTHBIN/find $SRP/rsync-script-lock -mmin +240 -exec $PTHBIN/rm {} \; 2> /dev/null #Check if lock in place if [ -f $SRP/rsync-script-lock ] then echo "#ERROR Another rsync is in progress - exiting (if this is wrong remove the $SRP/rsync-script-lock file)" >> $SRP/rsync2nas.log echo "Another rsync is in progress - exiting (if this is wrong remove the $SRP/rsync-script-lock file)" exit 0 fi #Lock echo `date +"%d/%m/%Y %H:%M:%S"` "Locking... Looking for files to move to $DST..." >> $SRP/rsync2nas.log $PTHBIN/touch $SRP/rsync-script-lock #Create a list of completed (i.e. not modified since +1 mins) files to move cd $SRC $PTHBIN/find -type f -mmin +1 -print > $SRP/rsync2nas.files ############ start $FILE loop ############ while read FILE do #Copy the files over to the DST $PTHRSY/rsync --itemize-changes \ --partial --recursive --timeout=60 --bwlimit=10000 --relative \ --password-file=$SRP/#password file# \ --log-file=$SRP/rsync2nas.log \ "$FILE" rsync://#RsyncUsername#@#NetworkDrive#/TV #If the rsync was successful if [ $? -eq 0 ]; then echo `date +"%d/%m/%Y %H:%M:%S"` "Update TVH for $FILE" >> $SRP/rsync2nas.log FILE2=`echo $FILE | cut -c 3-` $PTHBIN/curl -s "http://localhost:9981/api/dvr/entry/filemoved" \ --data-urlencode "src=$SRC/$FILE2" --data-urlencode "dst=$DST/$FILE2" #If the TVH update was succesful (i.e. if server was running - it doesn't detect bad requests) if [ $? -eq 0 ]; then echo `date +"%d/%m/%Y %H:%M:%S"` "Deleting $SRC/$FILE" >> $SRP/rsync2nas.log $PTHBIN/rm "$FILE" else echo `date +"%d/%m/%Y %H:%M:%S"` "TVH update failed (TVH server not running?)" >> $SRP/rsync2nas.log fi else echo `date +"%d/%m/%Y %H:%M:%S"` "rsync failed" >> $SRP/rsync2nas.log fi done < $SRP/rsync2nas.files ############# end $FILE loop ############# #Unlock echo `date +"%d/%m/%Y %H:%M:%S"` "Transfer complete... Unlocking" >> $SRP/rsync2nas.log $PTHBIN/rm $SRP/rsync-script-lock #Remove empty directories in the source cd $SRC $PTHBIN/find * -type d -depth -exec $PTHBIN/rmdir --ignore-fail-on-non-empty {} \; #Remove empty directories in the destination cd $DST $PTHBIN/find * -type d -depth -exec $PTHBIN/rmdir --ignore-fail-on-non-empty {} \; 2> /dev/null