From 8495bb0f1df88540b9d48d9a63dc74c2feb38f56 Mon Sep 17 00:00:00 2001 From: martin <1+martin@noreply.localhost> Date: Sun, 10 May 2026 10:42:41 +0200 Subject: [PATCH] import_keys.sh aktualisiert --- import_keys.sh | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/import_keys.sh b/import_keys.sh index eae38be..59ff89f 100644 --- a/import_keys.sh +++ b/import_keys.sh @@ -3,28 +3,45 @@ # Configuration URL="https://git.1337733.xyz/martin/sshconfig/raw/branch/main/authorized_keys" LOCAL_FILE="$HOME/.ssh/authorized_keys" +BACKUP_FILE="$HOME/.ssh/authorized_keys.bak" -# Ensure .ssh exists +# 1. Vorbereitung: Verzeichnis erstellen und lokale Datei sicherstellen mkdir -p ~/.ssh && chmod 700 ~/.ssh touch "$LOCAL_FILE" -echo "Checking for new keys..." +# 2. Backup erstellen (Sicherheit geht vor!) +cp "$LOCAL_FILE" "$BACKUP_FILE" -# 1. Download keys to a temporary file +# 3. Remote-Keys in temporäre Datei laden temp_keys=$(mktemp) curl -s "$URL" > "$temp_keys" -# 2. Add a newline to the local file to prevent "glued" keys -echo "" >> "$LOCAL_FILE" +# Falls der Download leer war, abbrechen um nichts zu beschädigen +if [ ! -s "$temp_keys" ]; then + echo "Fehler: Konnte Keys nicht von Gitea laden oder Datei ist leer." + rm "$temp_keys" + exit 1 +fi -# 3. Combine files and remove duplicates while keeping order -# This 'awk' magic keeps the first occurrence of every line and ignores the rest. -# It preserves your comments and their relationship to the keys. -cp "$LOCAL_FILE" "${LOCAL_FILE}.bak" -cat "$LOCAL_FILE" "$temp_keys" | awk '!visited[$0]++' > "${LOCAL_FILE}.tmp" && mv "${LOCAL_FILE}.tmp" "$LOCAL_FILE" +# 4. Der Smart-Merge +# Wir nehmen die lokale Datei ZUERST, dann die neuen Keys. +# awk '!visited[$0]++' behält nur das erste Vorkommen jeder Zeile. +# Bestehende Keys (auch fremde) bleiben also unberührt an ihrem Platz. +awk '!visited[$0]++' "$LOCAL_FILE" "$temp_keys" > "${LOCAL_FILE}.tmp" -# 4. Final Cleanup +# 5. Statistik berechnen +old_count=$(wc -l < "$LOCAL_FILE") +new_count=$(wc -l < "${LOCAL_FILE}.tmp") +diff=$((new_count - old_count)) + +# 6. Datei ersetzen und aufräumen +mv "${LOCAL_FILE}.tmp" "$LOCAL_FILE" chmod 600 "$LOCAL_FILE" rm "$temp_keys" -echo "Done! New keys (if any) have been added. A backup was created at authorized_keys.bak." \ No newline at end of file +if [ $diff -gt 0 ]; then + echo "✔ Fertig! $diff neue Zeile(n) wurden hinzugefügt." + echo "Bestehende Keys wurden nicht verändert." +else + echo "✔ Alles aktuell. Keine neuen Keys gefunden." +fi \ No newline at end of file