tar >rsync >git >

Mit rsync Ordner und Dateien synchronisieren

Installation in debian

apt update && apt install rsync

Mit rsync einen Lokalen Ordner synchronisieren.

  • " -r " => kopiere rekursiv, also auch Unterordner
  • " -l " => Symbolische Links werden kopiert
  • " -p " => Rechte der Quelldatei mit kopieren
  • " -t " => Zeiten (timestamp) der Quelldatei mit kopieren
  • " -v " => Zeigt etwas mehr Details
  • " -P " => Zeigt den Fortschritt an
  • " -n " => simuliere nur (dry run)
  • Um Inhalte von Ordnern zu synchronisieren immer auf das " / " am Ende des Quellordners achten!
# Order zum Testen erstellen
~ # mkdir test_rsync_quelle
~ # mkdir test_rsync_ziel
~ # touch test_rsync_quelle/test_datei_1.txt

# rsync mit Option "-n"
# Hier wird nur simuliert was er synchronisieren würde!

~ # rsync -rlptv -P -n test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
test_datei_1.txt

sent 80 bytes  received 22 bytes  204,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# rsync ohne Option "-n"

~ # rsync -rlptv -P test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
test_datei_1.txt
              0 100%    0,00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 116 bytes  received 38 bytes  308,00 bytes/sec
total size is 0  speedup is 0,00

~ # ls -l test_rsync_ziel/
insgesamt 0
-rw-r--r-- 1 root root 0 22. Sep 12:11 test_datei_1.txt

# Lässt man das "/" am Ende des Quellordners weg, passiert folgendes.

~ # rsync -rlptv -P test_rsync_quelle test_rsync_ziel/
sending incremental file list
test_rsync_quelle/
test_rsync_quelle/test_datei_1.txt
              0 100%    0,00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 143 bytes  received 39 bytes  364,00 bytes/sec
total size is 0  speedup is 0,00

# Der Ordner "test_rsync_quelle" wurde im Ziel angelegt.

~ # ls -l test_rsync_ziel/
insgesamt 4,0K
-rw-r--r-- 1 root root    0 22. Sep 12:11 test_datei_1.txt
drwxr-xr-x 2 root root 4,0K 22. Sep 12:11 test_rsync_quelle

Mit rsync einen Lokalen Ordner synchronisieren und alles im Zielordner löschen was im Quellordner nicht mehr vorhanden ist.

  • Option " --delete " zum Löschen im Ziel-Ordner benutzen.
  • Die rsync --delete Option ist sehr destruktiv. Am besten vorher immer mit "-n" gucken was gelöscht wird.
# Prüfen was im Ziel vorhanden ist

~ # ls -l test_rsync_ziel/
insgesamt 4,0K
-rw-r--r-- 1 root root    0 22. Sep 12:11 test_datei_1.txt
drwxr-xr-x 2 root root 4,0K 22. Sep 12:11 test_rsync_quelle

# rsync mit Option "-n"
# Hier wird der Ordner "test_rsync_quelle" samt Inhalt entfernt.

~ # rsync -rlptv -P -n --delete test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
deleting test_rsync_quelle/test_datei_1.txt
deleting test_rsync_quelle/
./

sent 73 bytes  received 75 bytes  296,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# rsync ohne Option "-n"

~ # rsync -rlptv -P --delete test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
deleting test_rsync_quelle/test_datei_1.txt
deleting test_rsync_quelle/
./

sent 73 bytes  received 75 bytes  296,00 bytes/sec
total size is 0  speedup is 0,00

# Nun ist der gewünschte Zustand hergestellt!

~ # ls -l test_rsync_ziel/
insgesamt 0
-rw-r--r-- 1 root root 0 22. Sep 12:11 test_datei_1.txt

rsync mit SSH verwenden um Dinge auf entfernten Servern auszuführen

  • rsync Option " -e "
  • Hier können die gewohnten Optionen für SSH verwendet werden.
  • Die SSH Optionen immer hinter einem " -e " in " " angeben.
  • Bei der Quelle oder Ziel auf einem anderen Server auf den vollständigen Pfad achten.
# Das Ziel ist auf einem anderen Server

~ # rsync -rlptv -P -n -e "ssh -p 22 -o StrictHostKeyChecking=no -i /pfad/zum/sshkey" test_rsync_quelle/ user@node:/root/test_rsync_ziel/

~ # rsync -rlptv -P -n --delete -e "ssh -p 22 -o StrictHostKeyChecking=no -i /pfad/zum/sshkey" test_rsync_quelle/ user@node:/root/test_rsync_ziel/

# Die Quelle ist auf einem anderen Server

~ # rsync -rlptv -P -n -e "ssh -p 22 -o StrictHostKeyChecking=no -i /pfad/zum/sshkey" user@node:/root/test_rsync_quelle/ test_rsync_ziel/

~ # rsync -rlptv -P -n --delete -e "ssh -p 22 -o StrictHostKeyChecking=no -i /pfad/zum/sshkey" user@node:/root/test_rsync_quelle/ test_rsync_ziel/

Mit rsync Ordner oder Dateien von einer Synchronisierung ausschließen.

  • Option " --exclude= "
  • Die rsync Option " --exclude= " kann beliebig wiederholt werden!
# Test Ordner anlegen

~ # mkdir test_rsync_quelle/cache

# Aktueller Stand im Quellordner

~ # ll test_rsync_quelle/
insgesamt 4,0K
drwxr-xr-x 2 root root 4,0K 22. Sep 12:51 cache
-rw-r--r-- 1 root root    0 22. Sep 12:11 test_datei_1.txt

# Mal gucken was er synchronisieren würde

~ # rsync -rlptv -P -n test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
cache/

sent 121 bytes  received 23 bytes  288,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# Der Ordner "cache" soll nicht mit synchronisiert werden
# Das ganze funktioniert folgendermaßen:

~ # rsync -rlptv -P -n --exclude="/cache" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./

sent 81 bytes  received 19 bytes  200,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# Alle Ordner mit der Bezeichnung "cache" ausschließen

# Test-Ordner stellen
~ # mkdir -p test_rsync_quelle/temp/cache

# exclude würde den /temp/cache nicht ausschließen, aber ...

~ # rsync -rlptv -P -n --exclude="/cache" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
temp/
temp/cache/

sent 145 bytes  received 27 bytes  344,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# ... in diesem Fall schon!
# Man achte auf die Schreibweise im exclude.

~ # rsync -rlptv -P -n --exclude="cache" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
temp/

sent 119 bytes  received 23 bytes  284,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# Dateien oder versteckte Dateien über rsync ausschließen

~ # touch test_rsync_quelle/.exclude_1

~ # rsync -rlptv -P -n --exclude="cache" --exclude="/.exclude_1" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
temp/

sent 118 bytes  received 23 bytes  282,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

~ # touch test_rsync_quelle/temp/.exclude_1

# Man achte wieder auf die Schreibweise im exclude.

# Mit "/" am Anfang

~ # rsync -rlptv -P -n --exclude="cache" --exclude="/.exclude_1" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
temp/
temp/.exclude_1

sent 148 bytes  received 26 bytes  348,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)

# Ohne "/" am Anfang

~ # rsync -rlptv -P -n --exclude="cache" --exclude=".exclude_1" test_rsync_quelle/ test_rsync_ziel/
sending incremental file list
./
temp/

sent 118 bytes  received 23 bytes  282,00 bytes/sec
total size is 0  speedup is 0,00 (DRY RUN)