Guest User

seedbox sync

a guest
Mar 4th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.52 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # -------------------------------
  4. # Seedbox
  5. # -------------------------------
  6. REMOTE_HOST=""                      # Remote FTP host
  7. REMOTE_USER=""                           # Remote FTP username
  8. REMOTE_PASSWORD=""              # Remote FTP password
  9.  
  10. # -------------------------------
  11. # Variables
  12. # -------------------------------
  13. REMOTE_DIR=""                     # Remote directory or file to sync
  14. LOCAL_DIR=""                   # Local directory for syncing
  15.  
  16. # -------------------------------
  17. # qBittorrent
  18. # -------------------------------
  19. QB_API_URL="/api/v2" # qBittorrent Web API URL
  20. QB_USERNAME=""                           # qBittorrent username
  21. QB_PASSWORD=""                  # qBittorrent password
  22. QB_CATEGORY=""              # qBittorrent category for filtering torrents
  23.  
  24. # Check if lftp is already running
  25. if ps aux | grep -v grep | grep -q "lftp"; then
  26.     echo "lftp is already running. Exiting."
  27.     exit 1
  28. fi
  29.  
  30. # -------------------------------
  31. # Login to qBittorrent API
  32. # -------------------------------
  33. echo "Logging in to qBittorrent..."
  34. QB_COOKIE=$(curl -si -X POST "$QB_API_URL/auth/login" \
  35.     --data-urlencode "username=$QB_USERNAME" \
  36.     --data-urlencode "password=$QB_PASSWORD" | grep -oP 'set-cookie: \K.*SID=.*' | tr -d '\r')
  37.  
  38. if [ -z "$QB_COOKIE" ]; then
  39.     echo "Failed to log in to qBittorrent."
  40.     exit 1
  41. fi
  42. echo "Login successful."
  43.  
  44. # -------------------------------
  45. # Check for active downloads in qBittorrent
  46. # -------------------------------
  47. echo "Checking for active downloads in qBittorrent..."
  48. RESPONSE=$(curl -s -b "$QB_COOKIE" "$QB_API_URL/torrents/info")
  49. if [ $? -ne 0 ] || [ -z "$RESPONSE" ]; then
  50.     echo "Failed to fetch torrent info. Exiting."
  51.     exit 1
  52. fi
  53.  
  54. ACTIVE_PATHS=$(echo "$RESPONSE" | jq -r \
  55.     ".[] | select(.state | test(\"downloading|stalledDL\")) | select(.category == \"$QB_CATEGORY\") | .content_path")
  56.  
  57. if [ -n "$ACTIVE_PATHS" ]; then
  58.     echo "Active downloads detected. Skipping sync."
  59.     exit 0
  60. fi
  61.  
  62. echo "No active downloads detected. Proceeding with sync."
  63.  
  64. # -------------------------------
  65. # Run lftp to list files and download them if modified since the last sync
  66. # -------------------------------
  67. echo "Starting lftp sync for new/updated files..."
  68.  
  69. lftp -u "$REMOTE_USER","$REMOTE_PASSWORD" "$REMOTE_HOST" -e "
  70. set file:use-lock true;
  71. set xfer:use-temp-file true;
  72. set xfer:temp-file-name *.lftp
  73. mirror --continue \
  74.       --parallel=3 \
  75.       $REMOTE_DIR $LOCAL_DIR;
  76. bye;
  77. "
  78.  
  79. echo "Sync and transfer completed."
Add Comment
Please, Sign In to add comment