Automation for sync'ing stuff.

I believe my setup is incredibly unusual and self-inflicted, so the below is unlikely help anyone else. However, it’s interesting nonetheless.

My remote Canadian server subscribes to a list of the most popualar Linux ISOs for the day, grabs them from the internet, unpacks and organizes them in a Linux_ISO folder, then Linux ISO software streams them to my family’s personal devices. The Linux ISOs exist in a large folder full of other Linux distributions on the remote server. I don’t want system memory getting out of control with indexing tasks, so I periodically move the files from Linux_ISO to Linux_ISO_sync. Syncthing indexes the sync folder and slowly drips the files over to my home server, through an encrypted connection, as to not saturate my home internet. It’s a great way to keep the ISOs coming without disrupting family Roblox or YouTube streaming.

The problem is that every morning I need to check the status of the current sync, and manually move the files over to a folder that my Linux ISO streaming software monitors. Automating this out was today’s task to save me the 10 minutes that’s needed.

The below script uses Syncthing’s API to determine the overall size of the remining sync size across a few folders, and move a random number of ISOs to keep the flow movin’. This way, I could die and my server will keep on chugging until it’s completely full or we have a nuclear winter as a result of the numerous wars our awesome country unnecessarily shoves its nose in.

#!/bin/bash

# Your local (home) server variables
apiKey="API_KEY"
address="http://localhost:8384"
folders=("folder1" "folder2" "folder3")

# Remote server SSH details
remoteUser="USER"
remoteHost="HOST"
remoteSrc="/downloads/folder"
remoteDest="/sync/folder"

# Initialize total size variable
totalSize=0

# Loop through each folder and sum up the 'needBytes'
for folder in "${folders[@]}"; do
  response=$(curl -s -k -X GET -H "X-API-Key: $apiKey" "$address/rest/db/status?folder=$folder")
  if [ $? -eq 0 ]; then # Check if curl command was successful
    result=$(echo "$response" | jq '.needBytes')
    totalSize=$((totalSize + result))
  else
    echo "Failed to fetch data for folder $folder"
  fi
done

# Convert totalSize to gigabytes
totalSizeGB=$(echo "$totalSize" | awk '{print $1/1024/1024/1024}')

# Determine linux_iso_qty based on totalSizeGB threshold
if (( $(echo "$totalSizeGB > 30" |bc -l) )); then
  linux_iso_qty=0
else
  linux_iso_qty=5
fi

# SSH command to execute on the remote server
sshCommand="find $remoteSrc -type f \\( -name \"*.iso\" -o -name \"*.img\" \\) | shuf -n $linux_iso_qty | xargs -I {} mv {} $remoteDest"

# Execute the command over SSH
ssh $remoteUser@$remoteHost "$sshCommand"

Note: For remote authentication without needing a password, setup ssh-key

ssh-keygen -t rsa -b 4096;
ssh-copy-id username@remote_host;
Previous: Generation Kill Next: Ricky Stanicky