seedbox_sync.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import subprocess
  2. import sys
  3. # Usage: sonarr_sync.py my-seedbox /seedbox/path/to/data /local/working /local/metadata /local/data
  4. # Get all file names in HOST:HOST_DATA_PATH
  5. # Get all previously processed file names in LOCAL_METADATA_PATH
  6. # Diff the above to get newly added files
  7. # For each new file:
  8. # Copy file to LOCAL_WORKING_PATH (used in case of transfer failure)
  9. # Add file name to LOCAL_METADATA_PATH
  10. # Move file to LOCAL_DATA_PATH
  11. if len(sys.argv) != 6:
  12. print("One or more args are undefined")
  13. sys.exit(1)
  14. host, host_data_path, local_working_path, local_metadata_path, local_data_path = sys.argv[1:6]
  15. r = subprocess.run(["ssh", host, "bash", "-c", f"IFS=$'\n'; ls {host_data_path}"], stdout=subprocess.PIPE, check=True)
  16. available = {f for f in r.stdout.decode().split('\n') if f}
  17. r = subprocess.run(["bash", "-c", f"IFS=$'\n'; ls {local_metadata_path}"], stdout=subprocess.PIPE, check=True)
  18. processed = {f for f in r.stdout.decode().split('\n') if f}
  19. new = available - processed
  20. for new_file in new:
  21. # Be super cautious about empty file names, wouldn't want to `rm -rf` a folder by accident
  22. if not new_file:
  23. continue
  24. print(f"Processing: {new_file}")
  25. subprocess.run(["rsync", "-rsvv", f'{host}:{host_data_path}/{new_file}', f'{local_working_path}'], check=True)
  26. r = subprocess.run(["touch", f'{local_metadata_path}/{new_file}'], check=True)
  27. print(f"Moving to ready: {new_file}")
  28. subprocess.run(["rsync", "-r", f'{local_working_path}/{new_file}', f'{local_data_path}'], check=True)
  29. subprocess.run(["rm", "-rf", f'{local_working_path}/{new_file}'], check=True)