import_keys.sh aktualisiert
This commit is contained in:
+29
-12
@@ -3,28 +3,45 @@
|
|||||||
# Configuration
|
# Configuration
|
||||||
URL="https://git.1337733.xyz/martin/sshconfig/raw/branch/main/authorized_keys"
|
URL="https://git.1337733.xyz/martin/sshconfig/raw/branch/main/authorized_keys"
|
||||||
LOCAL_FILE="$HOME/.ssh/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
|
mkdir -p ~/.ssh && chmod 700 ~/.ssh
|
||||||
touch "$LOCAL_FILE"
|
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)
|
temp_keys=$(mktemp)
|
||||||
curl -s "$URL" > "$temp_keys"
|
curl -s "$URL" > "$temp_keys"
|
||||||
|
|
||||||
# 2. Add a newline to the local file to prevent "glued" keys
|
# Falls der Download leer war, abbrechen um nichts zu beschädigen
|
||||||
echo "" >> "$LOCAL_FILE"
|
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
|
# 4. Der Smart-Merge
|
||||||
# This 'awk' magic keeps the first occurrence of every line and ignores the rest.
|
# Wir nehmen die lokale Datei ZUERST, dann die neuen Keys.
|
||||||
# It preserves your comments and their relationship to the keys.
|
# awk '!visited[$0]++' behält nur das erste Vorkommen jeder Zeile.
|
||||||
cp "$LOCAL_FILE" "${LOCAL_FILE}.bak"
|
# Bestehende Keys (auch fremde) bleiben also unberührt an ihrem Platz.
|
||||||
cat "$LOCAL_FILE" "$temp_keys" | awk '!visited[$0]++' > "${LOCAL_FILE}.tmp" && mv "${LOCAL_FILE}.tmp" "$LOCAL_FILE"
|
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"
|
chmod 600 "$LOCAL_FILE"
|
||||||
rm "$temp_keys"
|
rm "$temp_keys"
|
||||||
|
|
||||||
echo "Done! New keys (if any) have been added. A backup was created at authorized_keys.bak."
|
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
|
||||||
Reference in New Issue
Block a user