Two-Way Directory Synchronization at Specific Night Hours (Unison + systemd)
Problem
On my server, I wanted certain directories to synchronize with each other only at night between 00:00–06:00.
Doing this manually is tedious, and tools like rsync work one-way only. I needed something like syncthing but completely local, bidirectional, and time-controlled.
Solution
To meet this need, I used the Unison tool. Unison is a tool that can synchronize two directories bidirectionally, in sync, and can work locally or over the network.
- Thanks to the
ignoremechanism, I can define the parent directory as root and exclude subdirectories I don't want. - Using
systemd service + timer, I limited this synchronization to run only at night.
This way:
- Files start syncing at 00:00
- At 06:00 in the morning, the synchronization service stops
- During the day, source/target directories are left untouched
Code / Configuration
1. Installing Unison
# Debian/Ubuntu
sudo apt install unison
# Arch Linux
sudo pacman -S unison
2. Unison profile file
~/.unison/local.prf
root = /data
root = /backup/data
auto = true
batch = true
prefer = newer
confirmbigdeletes = true
fastcheck = true
log = true
logfile = /var/log/unison.log
# Ignore mechanism
ignore = Name {.git}
ignore = Name {node_modules}
ignore = Name {*.tmp}
ignore = Path {cache}
ignore = BelowPath {var/tmp}
ignore = Regex (?i)\.DS_Store$
3. Unison service file
/etc/systemd/system/unison-local.service
[Unit]
Description=Unison two-way synchronization (local)
After=network.target
[Service]
ExecStart=/usr/bin/unison local -repeat watch
Restart=always
User=USERNAME
Group=USERNAME
Nice=10
IOSchedulingClass=best-effort
IOSchedulingPriority=7
4. Timer files
Start (00:00):
/etc/systemd/system/unison-local-start.timer
[Unit]
Description=Unison night sync start (00:00)
[Timer]
OnCalendar=*-*-* 00:00:00
Persistent=true
[Install]
WantedBy=timers.target
Stop (06:00):
/etc/systemd/system/unison-local-stop.timer
[Unit]
Description=Unison morning sync stop (06:00)
[Timer]
OnCalendar=*-*-* 06:00:00
Persistent=true
[Install]
WantedBy=timers.target
5. Wrapper services
Start:
/etc/systemd/system/unison-local-start.service
[Unit]
Description=Start Unison service
[Service]
Type=oneshot
ExecStart=/bin/systemctl start unison-local.service
Stop:
/etc/systemd/system/unison-local-stop.service
[Unit]
Description=Stop Unison service
[Service]
Type=oneshot
ExecStart=/bin/systemctl stop unison-local.service
6. Enabling
sudo systemctl daemon-reload
sudo systemctl enable --now unison-local-start.timer
sudo systemctl enable --now unison-local-stop.timer
7. Verification
systemctl list-timers | grep unison
journalctl -u unison-local.service -e
Result
With this setup, two directories automatically synchronize between 00:00–06:00 every night.
- I achieved bidirectional safe synchronization with Unison.
- With
ignorerules, I excluded directories I didn't want. - Thanks to systemd timer, the process automatically starts and ends at exactly the times I wanted.
Now without manual intervention, my files are automatically updated every night.MARKDOWN_EOF