# TrueNAS two-way file sync controller This prototype configures **Syncthing** to continuously sync the contents under `stor0` in both directions. A small controller discovers the ZFS datasets on both TrueNAS SCALE systems over SSH and validates that eligible filesystem datasets are mounted beneath the `stor0` root. Syncthing syncs the root tree, so newly created directories and datasets are included on a scan without adding a folder for each dataset. The default exclusions are `stor0/containers` and `stor0/vm_stor`. Zvols are reported but not included. Permissions are ignored by Syncthing for now; ACLs can be configured after the content sync is tested. New folders are configured with staggered Syncthing file versioning (180 days by default; configurable in TOML). > **Important:** the Syncthing app/container on each NAS must be able to see the entire `stor0` mount tree, including nested ZFS dataset mounts created after the app starts. Test that before relying on this. If the app only sees the root dataset and not child mounts, dynamic discovery will not work until its host-path/mount configuration is corrected. ## Safety notes - `--dry-run` is the default. It performs read-only SSH and Syncthing API requests and makes no changes. - Before the first `--apply`, take fresh ZFS snapshots on both NASes. Initial two-way sync can find different versions of the same path; Syncthing may create conflict copies. - Syncthing propagates changes and deletions. The controller enables staggered file versioning for newly created sync folders, but also rely on independently retained ZFS snapshots before production use. Syncthing versioning stores replaced/deleted remote copies in `.stversions` and uses additional disk space. - Do not sync active container databases/mail stores or live VM disks as ordinary files. This controller excludes the named `containers` and `vm_stor` subtrees and all zvols. Plan app-aware/VM failover separately. - Both NASes can accept changes, but simultaneous edits to the same file while disconnected can create conflict copies; they cannot be merged automatically in every format. - Dataset mountpoints outside the root mount are rejected instead of silently omitted. `canmount=off` datasets and zvols are reported as skipped. - The controller never runs `rm`, `zfs destroy`, or snapshot deletion commands. Syncthing itself does propagate file deletions. If a child dataset is unmounted or destroyed, its files can appear to have been deleted from the sync root and those deletions may propagate; only do that intentionally, with snapshots/versioning available. The ZFS dataset object itself is not recreated or deleted on the other NAS. ## Requirements - Python 3.11+ - SSH access from the controller host to both NASes, with permission to run read-only `zfs list` - Syncthing installed/running on both NASes, with the two devices already paired - Syncthing REST API access and API keys for both instances - A Syncthing folder path on each NAS that points to that NAS's `stor0` root **inside the Syncthing app/container** Use HTTPS for the Syncthing API when it is reachable over a network. Restrict API access with firewall rules; the API key grants control over Syncthing. ## Configure 1. Copy `truenas-sync.example.toml` to `truenas-sync.toml` and protect it (`chmod 600 truenas-sync.toml` on Linux). Fill in SSH targets, Syncthing API URLs/keys, each device ID, and each path as seen inside the Syncthing app. 2. Confirm both Syncthing devices are paired in the Syncthing UI. The controller will not pair devices automatically. 3. Confirm the configured Syncthing paths resolve to each `stor0` root and that the app can see the child dataset mountpoints. The host mountpoint returned by ZFS is shown separately in the dry-run. 4. Run a plan: ```sh python3 truenas_sync.py --config truenas-sync.toml --dry-run ``` 5. Review every eligible/excluded/skipped dataset and fix any errors. Take fresh snapshots on both NASes, then configure the folder: ```sh python3 truenas_sync.py --config truenas-sync.toml --apply ``` `--dry-run` checks the discovered datasets and plans Syncthing configuration; it does **not** calculate a file-by-file transfer/deletion preview. `--apply` activates bidirectional sync for the entire configured root, so do not use it for the first time against production data until the inventory and snapshots are reviewed. For a small pilot, set `dataset_root = "stor0/sync_test"`, use `sync_path = "/data/stor0/sync_test"` on both peers, and give it a distinct `folder_id`. Remove the pilot folder configuration on both peers (keep the files) before expanding to `stor0`; Syncthing does not support nested shared folders safely. The controller creates the same Syncthing folder ID on each NAS in paused state, adds exclusions, then activates the folders. It is safe to run again once configured. It refuses to rewrite an existing folder whose path/peer differs, and refuses to change missing exclusion rules on an already-active folder. ## Dynamic dataset behavior Run the controller periodically (for example every 5 minutes) to re-inventory datasets and catch mount/path issues. The controller does not create a per-dataset Syncthing folder: the single Syncthing root covers the directory tree. Syncthing's filesystem watcher plus periodic rescans discovers new content. For a newly created ZFS dataset, verify that its mount is visible inside the Syncthing app and that the app's scanner sees a test file before trusting it. The script currently assumes the matching relative path exists under each side's sync root. If a dataset exists only on one side, Syncthing can create the corresponding directory on the other side; it does not create a ZFS dataset there. `dataset_root` may be a pool root such as `stor0` or a deeper ZFS dataset path such as `stor0/sync_test`. ## Tests Run the offline unit tests (no NAS or Syncthing connection required): ```sh python3 -m unittest -v ```