Bi-directional file synchronization with Unison
So for years I disliked using may laptop for any serious task because it didn't share my configuration and files. And I partially fixed that by writing a configuration sharing tool, but I also wanted to share my working directories.
rsync might come as an obvious solution for the job but the synchronization has to be bi-directional, and running the command twice results in favoring the changes of one direction.
rsync also has no way to handle deleted files. Luckily I stumbled upon unison.
Unison is a cross-platform file-synchronization tool. Check out the pictures on the wikipedia page if you want an estimate of how old it is. It allows you to sync two directories and manually intervene when file conflicts occur. It's like if Linus forgot to add commits to git. As with any half-decent tool it is covered by the ArchLinux wiki.
The way I run this is using these to systemd unit files:
Description=Sync directory %i with unison [Service] ExecStart=bash -c 'unison -sshargs "-i /home/rein/.ssh/xlab18" $HOME/%i ssh://x-lab18.local.boem/unison/%i -auto -batch -times' Restart=no
-auto automatically accept default nonconflicting actions
-batch ask no questions at all
-times preserve modification timestamps
[Unit] Description=Sync directory %i with unison every 5 minutes [Timer] OnUnitInactiveSec=5min OnBootSec=1min [Install] WantedBy=timers.target
I've stored these in my
If I were to execute
systemctl --user start unison@work, my directory
~/work will be synced with the directory
~/unison/work over at my server.
I can make this run every 5 minutes by starting and enabling the timer:
systemctl --user start firstname.lastname@example.org systemctl --user enable email@example.com