diff --git a/Files/Sync.sh b/Files/Sync.sh new file mode 100755 index 0000000..e52dbd6 --- /dev/null +++ b/Files/Sync.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# === IMPORTS === +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/../Utility/Lock.sh" +source "$SCRIPT_DIR/../Utility/Log.sh" + +# === Hilfe-Seite === +if [[ "$*" == *"-h"* ]] || [[ "$#" -ne 3 ]]; then + echo "Benutzung: $0 " + echo + echo "Optionen:" + echo " -h Zeigt diese Hilfenachricht an" + echo " SYNC_NAME Der Name dieser Synchronisation" + echo " DIR1 Der absolute Dateipfad eines Synchronisationsordners" + echo " DIR2 Der absolute Dateipfad eines Synchronisationsordners" + echo + echo "Folgende Pakete müssen installiert sein: unison" + echo + echo "Dieses Skript synchronisiert zwei Ordner miteinander." + exit 0 +fi + +# === Variablen === +SYNC_NAME="$1" +DIR1="$2" +DIR2="$3" + +# === TRAP EINRICHTEN === +# Signalbehandlung einrichten, um Lock-Datei bei Abbruch zu entfernen +trap "trap_remove_lock '$SYNC_NAME'" SIGINT SIGTERM + +# === LOCK-FILE ERSTELLEN === +# Lock-Datei erstellen, um parallele Ausführungen zu verhindern +create_lock "$SYNC_NAME" + +# Prüfen, ob Unison installiert ist, und es bei Bedarf installieren +if ! command -v unison &>/dev/null; then + echo "Unison ist nicht installiert. Installiere..." + sudo apt update && sudo apt install -y unison |& while IFS= read -r line; do log "$line"; done +fi + +# === Hauptskript === + +# Synchronisationsdatei nur erstellen, wenn sie noch nicht existiert +SYNC_FILE="$HOME/.unison/$SYNC_NAME.prf" +if [[ ! -f "$SYNC_FILE" ]]; then + echo "Erstelle Standard-Unison-Profil..." + mkdir -p "$(dirname "$SYNC_FILE")" + cat <"$SYNC_FILE" +root = $DIR1 +root = $DIR2 + +auto = true +batch = true +EOF +else + echo "Unison-Profil existiert bereits: $SYNC_FILE" +fi + +# Unison ausführen, um die Synchronisation durchzuführen +echo "Starte Synchronisation mit Unison..." +unison $SYNC_NAME |& while IFS= read -r line; do log "$line"; done + +# === LOCK-FILE ENTFERNEN === +# Lock-Datei entfernen, um die Ausführung freizugeben +remove_lock "$SYNC_NAME"