Aller au contenu

En bref

Comme nous l'avons vu précédemment, la synchronisation à sens unique utilise rsync + inotify-tools. Dans certains scénarios d'utilisation spéciaux, la synchronisation bidirectionnelle peut être requise, ce qui nécessite inotify-tools + unison.

Préparation de l'environnement

  • Rocky Linux 8 et Fedora 34 nécessitent la compilation et l'installation du code source inotify-tools, qui n'est pas spécifiquement développé ici.
  • Les deux machines doivent utiliser l'authentification sans mot de passe, ici nous utilisons le protocole SSH pour
  • ocaml uses v4.12.0, unison uses v2.51.4.

Une fois l'environnement prêt, il peut être vérifié ainsi :

[root@Rocky ~]# inotifywa
inotifywait inotifywatch
[root@Rocky ~]# ssh -p 22 testrsync@192.168.100.5
Last login: Thu Nov 4 13:13:42 2021 from 192.168.100.4
[testrsync@fedora ~]$
[root@fedora ~]# inotifywa
inotifywait inotifywatch
[root@fedora ~]# ssh -p 22 testrsync@192.168.100.4
Last login: Wed Nov 3 22:07:18 2021 from 192.168.100.5
[testrsync@Rocky ~]$

Astuce

Les fichiers de configuration des deux machines /etc/ssh/sshd_config doivent être ouverts PubkeyAuthentication yes

Rocky Linux 8 install unison

Ocaml est un langage de programmation ainsi qu'une dépendance de l'utilitaire unison.

[root@Rocky ~]# wget -c https://github.com/ocaml/ocaml/archive/refs/tags/4.12.0.tar.gz
[root@Rocky ~]# tar -zvxf 4.12.0.tar.gz -C /usr/local/src/
[root@Rocky ~]# cd /usr/local/src/ocaml-4.12.0
[root@Rocky /usr/local/src/ocaml-4.12.0]# ./configure --prefix=/usr/local/ocaml && make world opt && make install
...
[root@Rocky ~]# ls /usr/local/ocaml/
bin lib man
[root@Rocky ~]# echo PATH=$PATH:/usr/local/ocaml/bin >> /etc/profile
[root@Rocky ~]# . /etc/profile
[root@Rocky ~]# wget -c https://github.com/bcpierce00/unison/archive/refs/tags/v2.51.4.tar.gz
[root@Rocky ~]# tar -zvxf v2.51.4.tar.gz -C /usr/local/src/
[root@Rocky ~]# cd /usr/local/src/unison-2.51.4/
[root@Rocky /usr/local/src/unison-2.51.4]# make UISTYLE=txt
...
[root@Rocky /usr/local/src/unison-2.51.4]# ls src/unison
src/unison
[root@Rocky /usr/local/src/unison-2.51.4] cp -p src/unison /usr/local/bin

Fedora 34 unison installieren

La même opération.

[root@fedora ~]# wget -c https://github.com/ocaml/ocaml/archive/refs/tags/4.12.0.tar.gz
[root@feodora ~]# tar -zvxf 4.12.0.tar.gz -C /usr/local/src/
[root@fedora ~]# cd /usr/local/src/ocaml-4.12.0
[root@fedora /usr/local/src/ocaml-4.12.0]# ./configure --prefix=/usr/local/ocaml && make world opt && make install
...
[root@fedora ~]# ls /usr/local/ocaml/
bin lib man
[root@fedora ~]# echo PATH=$PATH:/usr/local/ocaml/bin >> /etc/profile
[root@fedora ~]#. /etc/profile
[root@fedora ~]# wget -c https://github.com/bcpierce00/unison/archive/refs/tags/v2.51.4.tar.gz
[root@fedora ~]# tar -zvxf v2.51.4.tar.gz -C /usr/local/src/
[root@fedora ~]# cd /usr/local/src/unison-2.51.4/
[root@fedora /usr/local/src/unison-2.51.4]# make UISTYLE=txt
...
[root@fedora /usr/local/src/unison-2.51.4]# ls src/unison
src/unison
[root@fedora /usr/local/src/unison-2.51.4]# cp -p src/unison /usr/local/bin

Démo

Notre exigence : le répertoire /dir1/ de Rocky Linux 8 est automatiquement synchronisé avec le répertoire /dir2/ Fedora 34, tandis que le répertoire /dir2/ de Fedora 34 est automatiquement synchronisé avec le répertoire /dir1/ de Rocky Linux 8.

Configurer Rocky Linux 8

[root@Rocky ~]# mkdir /dir1
[root@Rocky ~]# setfacl -m u:testrsync:rwx /dir1/
[root@Rocky ~]# vim /root/unison1.sh
#!/bin/bash
a="/usr/local/inotify-tools/bin/inotifywait -mrq -e create,delete,modify,move /dir1/"
b="/usr/local/bin/unison -batch /dir1/ ssh://testrsync@192.168.100.5//dir2"
$a | while read directory event file
do
    $b &>> /tmp/unison1.log
done
[root@Rocky ~]# chmod +x /root/unison1.sh
[root@Rocky ~]# bash /root/unison1.sh &
[root@Rocky ~]# jobs -l

Configurer Fedora 34

[root@fedora ~]# mkdir /dir2
[root@fedora ~]# setfacl -m u:testrsync:rwx /dir2/
[root@fedora ~]# vim /root/unison2.sh
#!/bin/bash
a="/usr/local/inotify-tools/bin/inotifywait -mrq -e create,delete,modify,move /dir2/"
b="/usr/local/bin/unison -batch /dir2/ ssh://testrsync@192.168.100.4//dir1"
$a | while read directory event file
do
    $b &>> /tmp/unison2.log
done
[root@fedora ~]# chmod +x /root/unison2.sh
[root@fedora ~]# bash /root/unison2.sh &
[root@fedora ~]# jobs -l

Astuce

Pour la synchronisation bidirectionnelle, les scripts des deux machines doivent être démarrés, sinon une erreur sera signalée.

Astuce

If you want to start this script at boot [root@Rocky ~]# echo "bash /root/unison1.sh &" >> /etc/rc.local [root@Rocky ~]# chmod +x /etc/rc.local

Astuce

Si vous voulez arrêter le processus correspondant à ce script, vous pouvez le trouver dans la commande htop puis kill

Author: tianci li

Contributors: Steven Spencer, Ganna Zhyrnova