Feat: Fügt ein Skript hinzu, das Dateien synchronisiert.

This commit is contained in:
DragonSlayer_14 2025-03-08 14:32:24 +01:00
parent 65fc9d0c69
commit c9bf7c96ff

67
Files/Sync.sh Executable file
View File

@ -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 <SYNC_NAME> <DIR1> <DIR2>"
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 <<EOF >"$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"