49 lines
1.6 KiB
Bash
49 lines
1.6 KiB
Bash
#!/bin/bash
|
|
|
|
chmod 600 /root/.ssh/id_rsa
|
|
# Parameter: Quelle, Zielserver, Zielpfad, Option
|
|
SOURCE_DIR="$1"
|
|
TARGET_SERVER="$2"
|
|
TARGET_PATH="$3"
|
|
OPTION="$4"
|
|
|
|
if [ -z "$SOURCE_DIR" ] || [ -z "$TARGET_SERVER" ] || [ -z "$TARGET_PATH" ]; then
|
|
echo "Nutzung: $0 <quelle> <serverziel> <zielpfad> [--timestamps]"
|
|
echo " Ohne '--timestamps': Vollständige Synchronisation"
|
|
echo " Mit '--timestamps': Nur Zeitstempel synchronisieren"
|
|
exit 1
|
|
fi
|
|
|
|
# Prüfen, ob der SSH-Zugang ohne Passwort funktioniert
|
|
echo "Prüfe SSH-Verbindung zu $TARGET_SERVER..."
|
|
ssh -o BatchMode=yes root@$TARGET_SERVER "exit"
|
|
if [ $? -ne 0 ]; then
|
|
echo "SSH-Zugang zu $TARGET_SERVER ist nicht konfiguriert. Führe zuerst zertifikat_install.sh aus."
|
|
exit 1
|
|
fi
|
|
|
|
# Berechtigungen auf der Quelle setzen
|
|
echo "Setze Berechtigungen auf der Quelle..."
|
|
chmod -R 755 $SOURCE_DIR
|
|
|
|
# Auswahl der Synchronisationsmethode
|
|
if [ "$OPTION" == "--timestamps" ]; then
|
|
echo "Synchronisiere nur Zeitstempel von $SOURCE_DIR nach $TARGET_SERVER:$TARGET_PATH..."
|
|
rsync -rt --existing --ignore-existing $SOURCE_DIR/ root@$TARGET_SERVER:$TARGET_PATH/
|
|
else
|
|
echo "Starte vollständige Synchronisation von $SOURCE_DIR nach $TARGET_SERVER:$TARGET_PATH..."
|
|
rsync -avz --delete $SOURCE_DIR/ root@$TARGET_SERVER:$TARGET_PATH/
|
|
fi
|
|
|
|
# Berechtigungen auf dem Ziel setzen
|
|
echo "Setze Berechtigungen auf dem Ziel..."
|
|
ssh root@$TARGET_SERVER "chmod -R 755 $TARGET_PATH"
|
|
|
|
# Prüfung auf Erfolg
|
|
if [ $? -eq 0 ]; then
|
|
echo "Synchronisation erfolgreich abgeschlossen und Berechtigungen korrekt gesetzt."
|
|
else
|
|
echo "Fehler bei der Synchronisation."
|
|
exit 1
|
|
fi
|