feat(torrent_qbittorrent): move torrent scripts to docker backup

change torrent scp to mediahost to cp to nfs directory
This commit is contained in:
lucas committed 2026-11-09 20:40:46 +01:00
1 parent 927b650e86
commit f10157792c
2 files changed
+20 -37

No files matched your search

@@ -0,0 +1,56 @@
#!/config/.venv/bin/python3
"""
torrent_mqtt.py
Veröffentlicht die übergebenen Kommandozeilenargumente als MQTT-Nachricht.
Verwendung:
torrent_mqtt.py "Download abgeschlossen: Film XYZ"
Topic:
serverhost_torrent_sensor/topic
Exit Codes:
0 = Erfolgreich
1 = Keine Nachricht übergeben
2 = MQTT-Fehler
"""
import sys
import paho.mqtt.client as mqtt
MQTT_BROKER_HOST = "192.168.0.4"
MQTT_BROKER_PORT = 1883
MQTT_USERNAME = "mqttbrokeruser"
MQTT_PASSWORD = "TKtWrtWGLSouf6"
MQTT_TOPIC = "serverhost_torrent_sensor/topic"
def main():
message = " ".join(sys.argv[1:]).strip()
if not message:
print("No message supplied.")
sys.exit(1)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
try:
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 60)
info = client.publish(MQTT_TOPIC, message)
info.wait_for_publish()
print(f"Published to '{MQTT_TOPIC}': {message}")
client.disconnect()
sys.exit(0)
except Exception as e:
print(f"MQTT error: {e}")
sys.exit(2)
if __name__ == "__main__":
main()
@@ -0,0 +1,176 @@
#!/bin/bash
###############################################################################
# Description:
# qBittorrent external script that:
# 1. Receives torrent name, root path and category from qBittorrent.
# 2. Sends the torrent name to torrent_mqtt.py.
# 3. Copies the torrent contents to mediahost via SCP.
# 4. Logs all output to a timestamped logfile.
#
# Usage:
# torrent_complete.sh <torrent_name> <root_path> <category>
#
# qBittorrent configuration:
# /home/lucas/scripts/serverhost/torrent_complete.sh "%N" "%R" "%L"
#
# Arguments:
# $1 Torrent name
# $2 Root path
# $3 Torrent category
#
# Log files:
# /home/lucas/.LOG/log_torrent_name_YYYY-MM-DD_HH-MM-SS.log
#
# Requirements:
# - bash
# - scp
# - SSH access to "mediahost"
# - /home/lucas/scripts/serverhost/torrent_mqtt.py
###############################################################################
set -u
SCRIPT_NAME="$(basename "$0")"
###############################################################################
# Usage
###############################################################################
usage() {
cat <<EOF
Usage:
$SCRIPT_NAME <torrent_name> <root_path> <category>
Description:
Sends the torrent name to torrent_mqtt.py and copies the torrent
contents to mediahost.
Arguments:
torrent_name Torrent name
root_path Torrent root path
category Torrent category
Example:
$SCRIPT_NAME "Ubuntu 24.04" "/downloads/Ubuntu 24.04" "Linux"
qBittorrent:
$SCRIPT_NAME "%N" "%R" "%L"
EOF
}
###############################################################################
# Check arguments
###############################################################################
if [[ $# -ne 3 ]]; then
usage >&2
exit 1
fi
TORRENT_NAME="$1"
ROOT_PATH="$2"
CATEGORY="$3"
if [[ "$CATEGORY" != *arr* && "$CATEGORY" != "mytorrents" ]]; then
###############################################################################
# Start
###############################################################################
echo "=================================================="
echo "Torrent transfer started"
echo "Date: $(date)"
echo "Script: $SCRIPT_NAME"
echo "Torrent name: $TORRENT_NAME"
echo "Root path: $ROOT_PATH"
echo "Category: $CATEGORY"
echo "=================================================="
# ###############################################################################
# # Check for scp command
# ###############################################################################
#
# if ! command -v scp >/dev/null 2>&1; then
# echo "Install scp"
# sed -i 's|http://|https://|g' /etc/apk/repositories
# for i in {1..12}; do
# if apk add --no-cache openssh-client; then
# break
# fi
#
# echo "Retry $i/12"
# sleep 5
# done
# fi
###############################################################################
# Send torrent name to MQTT script
###############################################################################
echo
echo "--- Sending torrent name to torrent_mqtt.py ---"
/config/.venv/bin/python3 /config/torrent_mqtt.py "$TORRENT_NAME"
MQTT_STATUS=$?
echo "torrent_mqtt.py exit status: $MQTT_STATUS"
###############################################################################
# Check root path
###############################################################################
echo
echo "--- Checking root path ---"
if [[ ! -d "$ROOT_PATH" ]]; then
echo "ERROR: Root path does not exist or is not a directory:"
echo " $ROOT_PATH"
exit 1
fi
echo "Root path exists."
###############################################################################
# Copy torrent to mediahost
###############################################################################
echo
echo "--- Copying torrent to mediahost ---"
echo "Source: $ROOT_PATH"
echo "Destination: mediahost:/torrent/qbittorrent/mediahost/"
cp -r "$ROOT_PATH" /mediahost/
CP_STATUS=$?
echo "scp exit status: $CP_STATUS"
###############################################################################
# Result
###############################################################################
echo
echo "=================================================="
if [[ $MQTT_STATUS -ne 0 ]]; then
echo "WARNING: torrent_mqtt.py failed."
fi
if [[ $CP_STATUS -eq 0 ]]; then
echo "Torrent transfer completed successfully."
else
echo "ERROR: Torrent transfer failed."
fi
echo "Finished: $(date +'%y%m%d %H%M%Sr')"
echo "=================================================="
else
echo "ARR handled torrent: $(date +'%y%m%d %H%M%S'): $TORRENT_NAME:$CATEGORY"
fi
# Return SCP's exit status to qBittorrent
exit 0