Getting a PI to update when on a network

Now we have a RV I wanted the pi to copy media files over when it is on the network. I have a share for our DVR where the media files are placed (HD Homerun + Emby) and wanted to copy certain TV shows over automatically when the RV is home and I put it on the network.

Now I can go down the BASH scripting route but I do not know that space very well. However I do know the PowerShell space well so installing it made sense. Following the instructions Installing PowerShell on Linux I made a cheat sheet so it is faster if I have to rebuild t a later time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
###################################
# Prerequisites

# Update package lists
sudo apt-get update

# Install libunwind8 and libssl1.0
# Regex is used to ensure that we do not install libssl1.0-dev, as it is a variant that is not required
sudo apt-get install '^libssl1.0.[0-9]$' libunwind8 -y

###################################
# Download and extract PowerShell

# Grab the latest tar.gz
wget https://github.com/PowerShell/PowerShell/releases/download/v7.0.3/powershell-7.0.3-linux-arm32.tar.gz

# Make folder to put powershell
mkdir ~/powershell

# Unpack the tar.gz file
tar -xvf ./powershell-7.0.3-linux-arm32.tar.gz -C ~/powershell

# Start PowerShell
~/powershell/pwsh

# Check version
$PSVersionTable

# Start PowerShell from bash with sudo to create a symbolic link
sudo ~/powershell/pwsh -c New-Item -ItemType SymbolicLink -Path "/usr/bin/pwsh" -Target "$PSHOME/pwsh" -Force

# Exit
exit

# Test link
pwsh

# Check version
$PSVersionTable

Now we have PowerShell installed we can get to the script, this will be run every hour via cron. You may have to update some of the paths. It assume that the remote server is mounted under /mnt/tv and the local drive is under /mnt/videos/tv. You will also have to update the IP address of the server that has the DVR files, replace 0.0.0.0 with your server IP.

For this to work make a file called test.txt in the remote mount share so the script can validate access. Then in the root folders you want to copy put a empty file called .rcopy. In the tv share I have a folder for every show and then folder under it for each season where the DRV has recorded. Emby has automation to make this so it is tidy and I can delete a season simply when I have watched it.

I use cron to schedule this to run once a hour, if the PI can see my home server then it will start the copy if not it will just skip the copy and exit. If the copy takes more than a hour having two instances running could be problematic so I drop a file int he pi profile directory. If it is there the process exits, if not it proceeds. Also handy to disable the copy process if you want to run manually without having to edit cron.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$serverIp = "0.0.0.0"
$remoteMount = "/mnt/tv"
$localMount = "/mnt/videos/tv"

Write-Host "Starting Test..."
if(Test-Path /home/pi/.mediacopyrunning) {return}
if ((Test-Connection -TargetName $serverIp -IPv4 -TimeoutSeconds 5).Status[0] -eq 'Success') {
"" | Set-Content /home/pi/.mediacopyrunning
Write-Host "Connected to network, checking share..."
umount $remoteMount
mount $remoteMount

if (Test-Path $remoteMount/test.txt) {
$tvFolders = (Get-ChildItem -Path $remoteMount -Recurse -Depth 1 -File -Force -Filter .rvcopy | ForEach-Object { Split-Path (Split-Path $_.FullName) -Leaf })
Remove-Item $remoteMount/rsynctv.log -Force
foreach ($folder in $tvFolders) {
Write-Host "Copying $folder"
rsync -rDvz --log-file=$remoteMount/rsynctv.log --partial --inplace --progress --no-perms --omit-dir-times --size-only "$remoteMount/$folder/" "$localMount/$folder"
}
}
Remove-Item /home/pi/.mediacopyrunning -Force
}

Loading message from the API: ...