sonarr_sync.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash -e
  2. # Usage: sonarr_sync.sh my-seedbox /seedbox/path/to/data /local/working /local/metadata /local/data
  3. # Get all file names in HOST:HOST_DATA_PATH
  4. # Get all previously processed file names in LOCAL_METADATA_PATH
  5. # Diff the above to get newly added files
  6. # For each new file:
  7. # Copy file to LOCAL_WORKING_PATH (used in case of transfer failure)
  8. # Add file name to LOCAL_METADATA_PATH
  9. # Move file to LOCAL_DATA_PATH
  10. HOST=$1
  11. HOST_DATA_PATH=$2
  12. LOCAL_WORKING_PATH=$3
  13. LOCAL_METADATA_PATH=$4
  14. LOCAL_DATA_PATH=$5
  15. if [[ -z $HOST || -z $HOST_DATA_PATH || -z $LOCAL_WORKING_PATH || -z $LOCAL_METADATA_PATH || -z $LOCAL_DATA_PATH ]]; then
  16. echo 'one or more args are undefined'
  17. exit 1
  18. fi
  19. ssh $HOST bash -c 'OIFS=$IFS; IFS=$'"'\n'"'; available=($(ls '$HOST_DATA_PATH')); IFS=$OIFS; declare -p available' > /tmp/available
  20. source /tmp/available
  21. rm /tmp/available
  22. #declare -p available
  23. bash -c 'OIFS=$IFS; IFS=$'"'\n'"'; processed=($(ls '$LOCAL_METADATA_PATH')); IFS=$OIFS; declare -p processed' > /tmp/processed
  24. source /tmp/processed
  25. rm /tmp/processed
  26. #declare -p processed
  27. OIFS=$IFS
  28. IFS=$'\n'
  29. new=($(comm --nocheck-order -13 <(printf '%s\n' "${processed[@]}" | LC_ALL=C sort) <(printf '%s\n' "${available[@]}" | LC_ALL=C sort)))
  30. IFS=$OIFS
  31. #declare -p new
  32. for i in "${new[@]}"
  33. do
  34. if [ ! -z "$i" ]; then
  35. echo "Processing: $i"
  36. rsync -rsvv "$HOST:$HOST_DATA_PATH/$i" $LOCAL_WORKING_PATH
  37. touch "$LOCAL_METADATA_PATH/$i"
  38. echo "Moving to ready: $i"
  39. rsync -r "$LOCAL_WORKING_PATH/$i" "$LOCAL_DATA_PATH"
  40. rm -rf "$LOCAL_WORKING_PATH/$i"
  41. fi
  42. done