seedbox_sync.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. if not new_file:
  22. continue
  23. print(f"Processing: {new_file}")
  24. subprocess.run(["bash", "-c", f"rsync -rsvv {host}:{host_data_path}/{new_file} {local_working_path}"], check=True)
  25. r = subprocess.run(["bash", "-c", f"touch {local_metadata_path}/{new_file}"], check=True)
  26. print(f"Moving to ready: {new_file}")
  27. subprocess.run(["bash", "-c", f"rsync -r {local_working_path}/{new_file} {local_data_path}"], check=True)
  28. subprocess.run(["bash", "-c", f"rm -rf {local_working_path}/{new_file}"], check=True)