Synchronisieren zwischen PC und Laptop mit rsync
Aus aredn.dg5mgj.de
Version vom 17. Januar 2026, 02:58 Uhr von DG5MGJ (Diskussion | Beiträge)
Dies ist ein Script zum synchronisieren eines Verzeichnispfades zwischen zwei Rechnern mit rsync
#!/bin/bash
###############################################################################
#** Mit diesem Script kann eine Ordnerstruktur zwischen zwei Rechnern mit rsync
#** in beiden Richtungen synchronisiert werden.
#** Es werden keine Dateien überschrieben oder gelöscht, sondern nur neue Pfade
#** und Dateien übertragen. Und das in beide Richtungen!
#** Im Anschluss kann eine Liste der veränderten Dateien zwischen Laptop und PC
#** angezeigt werden und diese Liste auch als Aufgabendatei gespeichert werden.
#** Es wird auch eine Logdatei des Sicherungsvorgangs erstellt in dem alle not-
#** wendigen Informationen zu einer Analyse enthalten sind.
###############################################################################
##* History
##* Datum Name Beschreibung
##* -----------+-------+-------------------------------------------------------
##* 2026-01-18 DG5MGJ Hostname mit Großbuchstabenmachen Probleme => Translate
##* 2026-01-17 DG5MGJ Fehlermeldungen bei rsync kommen nun auch ins $Log_File
##* 2026-01-16 DG5MGJ Skript aktiviert auf beiden Rechnern und getestet => OK
##* 2026-01-15 DG5MGJ Skript erzeugt zur Syncronisierung zwischen Laptop + PC
##* *********************************************************************** *##
##* V="Variablen";sed -n "0,/${V}-sind-definiert/p" ~/Videos/Script/rsync-Projekte.sh > ~/x.y;. ~/x.y
###############################################################################
This_Name="`basename -- $0|cut -d. -f1`"
if [ "${This_Name: -4}" = "bash" ]; then This_Name="rsync-Projekte"; fi
if [ -t 1 ]; then echo -ne "\033]0;${This_Name}\007"; fi
Day_Stamp="`date +'%F'`"
Mon_Stamp="`date +'%Y-%m'`"
DayStpVal="`date +'%Y%m%d'`"
TimeStpVl="`date +'%H%M%S'`"
Verbose=0; if [ "$1" = "-v" ]; then Verbose=1; fi
#*****************************************************************************
#** Erzeugung der benötigten Variablen und Arrays
ThisHost="`uname -n|tr '[:upper:]' '[:lower:]'`"
ParamPth="~/.${This_Name}"
declare -a DomainNm
declare -a HostName
declare -a UserName
declare -a HostPath
declare -a HostStat
declare -a CopyPath
#** Hostnamen und Pfade zuweisen
DomainNm[0]="dahoam"
HostName[0]="mycomputer"
UserName[0]="johndoe"
HostPath[0]="/home/johndoe/Dokumente/Projekte/"
DomainNm[1]="dahoam"
HostName[1]="mylaptop"
UserName[1]="johndoe"
HostPath[1]="/home/johndoe/Dokumente/Projekte/"
#* Log-File zur Protokollierung im Home-Verzeichnis des Users
Log_File="${HOME}/${This_Name}_${DayStpVal}${TimeStpVl}.log"
if [ -s ${ParamPth}/${This_Name}.param ]; then . ${ParamPth}/${This_Name}.param; fi
#*****************************************************************************
#********alle-Variablen-sind-definiert****************************************
#*****************************************************************************
echo -e "\n******** Programmstart on ${ThisHost} at `date` ********" | tee -a ${Log_File}
echo -e "******** Logfile is ${Log_File} ********"
#*****************************************************************************
# Verbose-Ausgabe der Grundparameter
if [ $Verbose -gt 0 ]
then
for idx in {0..1}
do echo "$idx = ${HostName[$idx]}.${DomainNm[$idx]}:${HostPath[$idx]}" | tee -a ${Log_File}
done
fi
# set -x
#*****************************************************************************
#* Entscheidung zum Ausführungsstandort und der Kopierrichtung remote/local
for idx in {0..1}
do xid=$(( ($idx+1)%2 ))
if [ "${ThisHost}" = "${HostName[$idx]}" ]
then
CopyPath[0]="${HostPath[$idx]}"
CopyPath[1]="${UserName[$xid]}@${HostName[$xid]}.${DomainNm[$xid]}:${HostPath[$xid]}"
fi
done
if [ "${CopyPath[0]}" = "" ]
then echo "## unknown Hostname ${ThisHost} ##" | tee -a ${Log_File}
echo -e "\n** Ermittle Onlinestatus of Hosts ${HostName[@]} **" | tee -a ${Log_File}
for idx in {0..1}
do ping -qc 2 ${HostName[$idx]}.${DomainNm[$idx]} > /dev/null
HostStat[$idx]="$?"
if [ ${HostStat[$idx]} -eq 0 ]
then echo "******** ${HostName[$idx]} is online ********" | tee -a ${Log_File}
fi
done
echo -e "\n=============================================================================\n" | tee -a ${Log_File}
sleep 21; exit 7
fi
#*****************************************************************************
#* Überprüfung der Erreichbarkeit des Zielrechners
DestHost="`echo "${CopyPath[1]}"|cut -d':' -f1|cut -d'@' -f2`"
echo -e "\n** Ermittle Onlinestatus of Host ${DestHost} **" | tee -a ${Log_File}
ping -qc 2 ${DestHost}
HostStat[1]=$?
if [ ${HostStat[1]} -eq 0 ]
then
echo "** Der Zielhost ${DestHost} ist erreichbar, Synchronisierung startet **" | tee -a ${Log_File}
echo rsync -av --ignore-existing ${CopyPath[1]} ${CopyPath[0]} | tee -a ${Log_File}
rsync -av --ignore-existing ${CopyPath[1]} ${CopyPath[0]} 2&>1 | tee -a ${Log_File}
echo -e "\n=============================================================================\n" | tee -a ${Log_File}
echo rsync -av --ignore-existing ${CopyPath[0]} ${CopyPath[1]} | tee -a ${Log_File}
rsync -av --ignore-existing ${CopyPath[0]} ${CopyPath[1]} 2&>1 | tee -a ${Log_File}
else
echo "## Der Zielhost `echo ${CopyPath[1]}|cut -d ':' -f1` ist NICHT erreichbar, Abbruch ##" | tee -a ${Log_File}
fi
echo -e "******** Logfile is ${Log_File} ********"
sleep 42
#** [ E-O-F ] **#
Wie im Programm selbst beschrieben werden nur neu erstellte Dateien und Ordner repliziert. Es werden keine Dateien überschrieben...