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#
- Script erstellen und ablegen
Speichern Sie das Bash-Scriptapoally-insights-upload.shin einem gewünschten Verzeichnis, z. B./usr/local/bin/.
- Ausführbar machen
Stellen Sie sicher, dass das Script ausführbar ist:
- API-Schlüssel konfigurieren
Öffnen Sie das Script und ersetzen SieYOUR-API-KEYdurch Ihren tatsächlichen API-Schlüssel.
Ändern Sie die Zeile:
Speichern und schließen Sie die Datei.
- Cronjob einrichten
Um das Script regelmäßig auszuführen (nicht öfter als alle 15 Minuten), erstellen Sie einen Cronjob:
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.
- Logdatei überprüfen
Die Ausgaben des Scripts werden in/var/log/apoally-upload.loggespeichert. Überprüfen Sie die Logdatei regelmäßig, um sicherzustellen, dass das Script korrekt funktioniert:
- Testlauf
Führen Sie das Script manuell aus, um sicherzustellen, dass alles korrekt eingerichtet ist:
Ü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.