Project

General

Profile

How to move TVH recording to a network drive and update ... ยป script.txt

Bash script - Nic Butcher, 2017-10-26 01:16

 
1
#!/bin/sh
2

    
3
#Set up the locations....
4
#SRC is where TVH puts the files - e.g. '/storage/TVHRecordings/Recent'
5
#This must be set within TVHeadEnd as a combination of
6
#  the Recording system path - e.g. '/storage/TVHRecordings'
7
#      and the format string - e.g. 'Recent/$t/$t$_e_$c_%F_%R$n.$x'
8
#DST is where we want to move them to within TVH - e.g. '/storage/TVHRecordings/Stored'
9
#In this example the files would be move from '/storage/TVHRecordings/Recent' to '/storage/TVHRecordings/Stored'
10

    
11
SRC=/storage/TVHRecordings/Recent
12
DST=/storage/TVHRecordings/Stored
13

    
14
#Set paths to the files we need
15
#Where this script is -
16
SRP=/storage/nasscripts
17

    
18
#Set paths to the commands we need
19
#Note - for LibreElec 'rsync' is not provided as standard and must be added as a Kodi addon
20
PTHBIN=/usr/bin
21
PTHRSY=/storage/.kodi/addons/virtual.network-tools/bin
22

    
23
#First - make sure another rsync isn't running
24
echo `date +"%d/%m/%Y %H:%M:%S"` "Checking for lockfile" >> $SRP/rsync2nas.log
25

    
26
#Delete the lock if over 4 hours (240m) - due to this script failing half way through - e.g. due to power off
27
$PTHBIN/find $SRP/rsync-script-lock -mmin +240 -exec $PTHBIN/rm {} \; 2> /dev/null
28

    
29
#Check if lock in place
30
if [ -f $SRP/rsync-script-lock ]
31
then
32
 echo "#ERROR Another rsync is in progress - exiting (if this is wrong remove the $SRP/rsync-script-lock file)" >> $SRP/rsync2nas.log
33
 echo "Another rsync is in progress - exiting (if this is wrong remove the $SRP/rsync-script-lock file)"
34
 exit 0
35
fi
36

    
37
#Lock
38
echo `date +"%d/%m/%Y %H:%M:%S"` "Locking... Looking for files to move to $DST..."  >> $SRP/rsync2nas.log
39
$PTHBIN/touch $SRP/rsync-script-lock
40

    
41

    
42
#Create a list of completed (i.e. not modified since +1 mins) files to move
43
cd $SRC
44
$PTHBIN/find -type f -mmin +1 -print > $SRP/rsync2nas.files
45

    
46
############ start $FILE loop ############
47
while read FILE
48
do
49

    
50
#Copy the files over to the DST
51
 $PTHRSY/rsync --itemize-changes \
52
 --partial --recursive --timeout=60 --bwlimit=10000 --relative \
53
 --password-file=$SRP/#password file# \
54
 --log-file=$SRP/rsync2nas.log \
55
 "$FILE" rsync://#RsyncUsername#@#NetworkDrive#/TV
56

    
57
 #If the rsync was successful
58
  if [ $? -eq 0 ]; then
59
   echo `date +"%d/%m/%Y %H:%M:%S"` "Update TVH for $FILE" >> $SRP/rsync2nas.log
60
   FILE2=`echo $FILE | cut -c 3-`
61
   $PTHBIN/curl -s "http://localhost:9981/api/dvr/entry/filemoved" \
62
   --data-urlencode "src=$SRC/$FILE2" --data-urlencode "dst=$DST/$FILE2"
63

    
64
   #If the TVH update was succesful (i.e. if server was running - it doesn't detect bad requests)
65
   if [ $? -eq 0 ]; then
66
     echo `date +"%d/%m/%Y %H:%M:%S"` "Deleting $SRC/$FILE" >> $SRP/rsync2nas.log
67
     $PTHBIN/rm "$FILE"
68
   else
69
     echo `date +"%d/%m/%Y %H:%M:%S"` "TVH update failed (TVH server not running?)" >> $SRP/rsync2nas.log
70
   fi
71
  else
72
   echo `date +"%d/%m/%Y %H:%M:%S"` "rsync failed" >> $SRP/rsync2nas.log
73
 fi
74

    
75
done < $SRP/rsync2nas.files
76
############# end $FILE loop #############
77

    
78
#Unlock
79
echo `date +"%d/%m/%Y %H:%M:%S"` "Transfer complete... Unlocking" >> $SRP/rsync2nas.log
80
$PTHBIN/rm $SRP/rsync-script-lock
81

    
82
#Remove empty directories in the source
83
cd $SRC
84
$PTHBIN/find * -type d -depth -exec $PTHBIN/rmdir --ignore-fail-on-non-empty {} \;
85

    
86
#Remove empty directories in the destination
87
cd $DST
88
$PTHBIN/find * -type d -depth -exec $PTHBIN/rmdir --ignore-fail-on-non-empty {} \; 2> /dev/null
89

    
90

    
91

    
92

    
    (1-1/1)