Zum Inhalt

ApoAlly Insights Einrichtung#

Debian/Ubuntu#

Bash Upload-Script
apoally-insights-upload.sh
#!/bin/bash
# upload_csv.sh
#
# This script now automatically searches for an optional schema file (.json or .csvs)
# with the same base name as the CSV/TSV file. JSON has priority over CSVS if both exist.
# If no schema file is found, only the CSV/TSV file is uploaded. If a directory is provided,
# the script processes all .csv and .tsv files within it (recursively), each with its optional schema file.
#
# Usage:
#   ./upload_csv.sh <path_to_csv_or_tsv_file_or_directory>
#
# Environment Variables:
#   API_URL - The URL of the CSV upload endpoint (default: https://api.apoally.de/insights/upload)
#   API_KEY - The API key used for authentication (default: YOUR-API-KEY)

API_URL="https://api.apoally.de/insights/upload"
API_KEY="YOUR-API-KEY"

# Check if exactly one argument is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <path_to_csv_or_tsv_file_or_directory>"
    exit 1
fi

TARGET="$1"

function upload_single_file() {
    local file_path="$1"
    local base="${file_path%.*}"

    # Detect optional schema file with the same base name
    local schema_file=""
    if [ -f "${base}.json" ]; then
        schema_file="${base}.json"
    elif [ -f "${base}.csvs" ]; then
        schema_file="${base}.csvs"
    fi

    echo "Uploading file: $file_path"
    if [ -n "$schema_file" ]; then
        echo "Found optional schema file: $schema_file"
        curl -X POST \
             -H "X-API-Key: $API_KEY" \
             -F "files=@${file_path}" \
             -F "files=@${schema_file}" \
             "$API_URL"
    else
        # No schema file, upload only the CSV/TSV
        curl -X POST \
             -H "X-API-Key: $API_KEY" \
             -F "files=@${file_path}" \
             "$API_URL"
    fi
    echo
}

# If TARGET is a file, upload it and its optional schema
if [ -f "$TARGET" ]; then

    # Extract the extension and allow both .csv and .tsv
    extension="${TARGET##*.}"
    if [ "$extension" != "csv" ] && [ "$extension" != "tsv" ]; then
        echo "Error: '$TARGET' is neither a .csv nor a .tsv file."
        exit 1
    fi

    upload_single_file "$TARGET"

# If TARGET is a directory, process all .csv and .tsv files within it recursively
elif [ -d "$TARGET" ]; then

    echo "Processing directory: $TARGET"
    while IFS= read -r -d '' file; do
        upload_single_file "$file"
    done < <(find "$TARGET" -type f \( -iname '*.csv' -o -iname '*.tsv' \) -print0)

else
    echo "Error: '$TARGET' is not a valid file or directory"
    exit 1
fi

Anleitung zur Installation und Konfiguration#

  1. Script erstellen und ablegen
    Speichern Sie das Bash-Script apoally-insights-upload.sh in einem gewünschten Verzeichnis, z. B. /usr/local/bin/.
sudo nano /usr/local/bin/apoally-insights-upload.sh

Fügen Sie den Inhalt des Scripts ein und speichern Sie die Datei.

  1. Ausführbar machen
    Stellen Sie sicher, dass das Script ausführbar ist:
sudo chmod +x /usr/local/bin/apoally-insights-upload.sh
  1. API-Schlüssel konfigurieren
    Öffnen Sie das Script und ersetzen Sie YOUR-API-KEY durch Ihren tatsächlichen API-Schlüssel.
sudo nano /usr/local/bin/apoally-insights-upload.sh

Ändern Sie die Zeile:

API_KEY="YOUR-API-KEY"

Speichern und schließen Sie die Datei.

  1. Cronjob einrichten
    Um das Script regelmäßig auszuführen (nicht öfter als alle 15 Minuten), erstellen Sie einen Cronjob:
crontab -e

Fügen Sie die folgende Zeile hinzu, um das Script alle 15 Minuten auszuführen:

*/15 * * * * /usr/local/bin/apoally-insights-upload.sh /path/to/your/csv_or_tsv_files >> /var/log/apoally-upload.log 2>&1

Ersetzen Sie /path/to/your/csv_or_tsv_files durch den Pfad zu Ihrer Datei oder Ihrem Verzeichnis.

  1. Logdatei überprüfen
    Die Ausgaben des Scripts werden in /var/log/apoally-upload.log gespeichert. Überprüfen Sie die Logdatei regelmäßig, um sicherzustellen, dass das Script korrekt funktioniert:
tail -f /var/log/apoally-upload.log
  1. Testlauf
    Führen Sie das Script manuell aus, um sicherzustellen, dass alles korrekt eingerichtet ist:
/usr/local/bin/apoally-insights-upload.sh /path/to/your/csv_or_tsv_files

Überprüfen Sie die Ausgabe und die Logdatei auf Fehler.

Hinweis#

  • Stellen Sie sicher, dass Ihr System über eine stabile Internetverbindung verfügt, um die Daten erfolgreich hochladen zu können.
  • Überprüfen Sie regelmäßig die Cronjob-Ausführung und die Logdateien, um sicherzustellen, dass keine Probleme auftreten.