One-way syncing for Linux

I take it that it is a settled fact that NextCloud devs. cannot be persuaded to add a “one-way syncing” feature to the official NextCloud desktop syncing app (“NextCloud Files”), and this thread is not attempting to wake up any sleeping dogs. What can those of us who need this feature use though? Again, what I need is some app that has, by-and-large, the same features as the official NextCloud app PLUS one-way syncing (desktop to NextCloud cloud). As far as I can tell, rclone ain’t it. Note that it should preferably be open source, and ideally already packaged as Flatpak, or Docker image or the like.

Many thanks for any suggestions!

There is no such thing like “one-way-sync”
 depending on the direction what should happen with new/deleted files? If you speak about scheduled snapshots/copies it is possible to do such task using webdav and curl (or GitHub - j4m3z0r/davsync: DavSync: like rsync, for WebDAV)

Thanks, looks nice for P2P, only I already have a personal cloud that I went to great lengths to build. Also, they seem to have deprecated the Docker image, which is a shame. Finally, while I may be able to cram the RPM onto my system (ClearLinux), I am not sure if TrueNAS takes RPMs.

One-way sync scenario–Folder F of computer A is one-way synced with folder FC of computer C (the “cloud”):

  1. All changes in folder F are automatically replicated in FC, including deleted files. (Nice-to-have option at this point: delete file from F without deleting from FC. (Deleted F files to be renamed to something else in FC.))
  2. All changes to files in FC that originate from F and have the same names as their F counterparts to be automatically replicated in F.
  3. Any files in FC that do not come from F (or originate from F files but were deleted from F–see “nice-to-have option” above), to be excluded from syncing.

Apparently Syncthing might be able to do what I need–or something in that vein.

What about Intel Unison?

Not a Linux app. At least not for now.

And what about rsync + cron?

The crucial requirement is that syncing happens in (almost) real time, hence the “automatic(ally)” attribute. Can Cron jobs be triggered by changes to the F folder? Any change to the content of F needs to be immediately propagated to FC; periodic backups ain’t cuttin’ it, as often as they may be.

Whenever people here ask about one-way sync, I always wonder what their actual use case is, and whether there might be a better way to do what they are trying to achieve, or whether Nextcloud is even the right tool for the job :wink:

And no, I don’t mean the technical description of the process that you described here, I mean the actual use case. What actual real-world problem are you trying to solve with this rather complex process?

Is it about normal files that you work with day-to-day? is this meant to be some backup concept? is it about a kind of autouload of files that are read by a scanner, camera, auto-generated by some tool
?

1 Like

I have asked ChatGPT:

/read https://help.nextcloud.com/t/one-way-syncing-for-linux/218202/10

Make a suggestion on how best to solve this with a Python script

Based on the discussion in the Nextcloud forum regarding one-way syncing on Linux, a Python script can be created to handle real-time one-way sync from a local folder to Nextcloud, avoiding traditional scheduled sync via cron.


Best Approach: Using Python + Watchdog + WebDAV

Since the user requires real-time sync (not periodic), a solution can be built using:

  1. Watchdog – to detect file changes in the source directory.
  2. WebDAV (Nextcloud’s API) – to upload, update, or delete files on the Nextcloud server.

Python Script Plan

  1. Monitor a local folder (F) for changes – Detect file creations, modifications, and deletions.
  2. Sync changes to Nextcloud (FC) – Using WebDAV:
    • Upload new files.
    • Update existing files.
    • Remove files from Nextcloud when deleted locally.
  3. Ignore files that originated from FC but were modified on the server.
  4. Handle errors and log sync activity.

Implementation: Python Script for One-Way Sync

Here’s a Python script that continuously syncs a folder to Nextcloud in real-time:

import os
import time
import requests
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# Nextcloud WebDAV Credentials
NEXTCLOUD_URL = "https://your-nextcloud.com/remote.php/dav/files/your-username/"
USERNAME = "your-username"
PASSWORD = "your-password"

# Local folder to sync
LOCAL_FOLDER = "/path/to/local/folder/"

class SyncHandler(FileSystemEventHandler):
    """Handles file system events and syncs with Nextcloud via WebDAV."""

    def __init__(self):
        super().__init__()

    def upload_file(self, file_path):
        """Uploads or updates a file in Nextcloud."""
        relative_path = os.path.relpath(file_path, LOCAL_FOLDER)
        nextcloud_path = f"{NEXTCLOUD_URL}{relative_path}"
        
        with open(file_path, "rb") as file:
            response = requests.put(nextcloud_path, data=file, auth=(USERNAME, PASSWORD))
        
        if response.status_code in [201, 204]:
            print(f"Uploaded: {relative_path}")
        else:
            print(f"Failed to upload {relative_path}: {response.status_code}")

    def delete_file(self, file_path):
        """Deletes a file from Nextcloud."""
        relative_path = os.path.relpath(file_path, LOCAL_FOLDER)
        nextcloud_path = f"{NEXTCLOUD_URL}{relative_path}"

        response = requests.delete(nextcloud_path, auth=(USERNAME, PASSWORD))
        
        if response.status_code == 204:
            print(f"Deleted: {relative_path}")
        else:
            print(f"Failed to delete {relative_path}: {response.status_code}")

    def on_created(self, event):
        """Handles file creation."""
        if not event.is_directory:
            self.upload_file(event.src_path)

    def on_modified(self, event):
        """Handles file modification."""
        if not event.is_directory:
            self.upload_file(event.src_path)

    def on_deleted(self, event):
        """Handles file deletion."""
        if not event.is_directory:
            self.delete_file(event.src_path)

if __name__ == "__main__":
    event_handler = SyncHandler()
    observer = Observer()
    observer.schedule(event_handler, LOCAL_FOLDER, recursive=True)

    print(f"Watching {LOCAL_FOLDER} for changes...")
    observer.start()

    try:
        while True:
            time.sleep(10)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

How It Works

  1. Listens for file changes in LOCAL_FOLDER using watchdog.
  2. Uploads new or modified files to Nextcloud via WebDAV.
  3. Deletes files from Nextcloud if they are deleted locally.
  4. Runs continuously in the background.

Requirements

Install dependencies:

pip install watchdog requests

Run the script:

python nextcloud_sync.py
2 Likes

The real-world problem is precisely the problem that one tries to solve using two-way sync 
 except one very important difference: my Nextcloud cloud is 16T, while my local HDD is 512G in size. The Nextcloud box culls files from 4 other LAN boxes.

The solution to this is called Virtual Files.

Unfortunately, if you are on Linux, this is still ‘experimental’, which means that only the ‘barbones’ variant using placeholder files is implemented. It does work though.

There is also some discussion going on about how to implement a similar experience on Linux as on Windows and MacOS, but nothing has been decided or implemented yet: VFS on Linux · Issue #3668 · nextcloud/desktop · GitHub

1 Like

Are you aware of git-annex?

Docker is adopting a pay-to-play model now that they have so many folks committed to so much Docker infrastructure. At least, that’s my read on it.

Could there be a solution whereby you set permissions on some local folders such that the sync down to the desktop can only write into certain areas and not others?

Even without virtual files, you can choose which folders you’d like to sync. I use like this a few working folders, where I have files that I currently work on. Other stuff I only sync temporarily: