Skip to main content
  1. Posts/

How to Sync a Headless Server with Your Nextcloud Files

Table of Contents

When it comes to keeping your headless server synchronized with your Nextcloud server, setting up a reliable automated process is crucial. This guide will walk you through syncing top-level directories on an Ubuntu server. While this explanation uses Ubuntu, be aware that some commands, such as package installation, may vary on other Linux distributions.

Step 1: Install the Nextcloud Command-Line Client
#

First, you’ll need to install the Nextcloud command-line client to manage the synchronization:

sudo apt install nextcloud-desktop-cmd

Ensure the package installs successfully so that you can proceed with setting up automated sync tasks.

Configure Credentials
#

To enable authentication for nextcloudcmd, add your credentials to the ~/.netrc file with the following format:

machine your-next-cloud-domain.org
login your-username
password your-password

Replace your-username and your-password with your actual Nextcloud login details. This file ensures that the command-line client can authenticate without manual input.

Step 2: Choose Directories to Sync
#

For simplicity, I prefer syncing only the top-level directories that are important to me, such as src and similar folders. This approach keeps the sync process efficient and focused.

Step 3: Create the systemd Service File
#

To automate the syncing process, create a systemd service file that defines how the sync should be executed. Save the following content as ~/.config/systemd/user/nextcloud-sync-src.service:

[Unit]
Description=Nextcloud Sync Src

[Service]
Type=simple
ExecStart=/usr/bin/nextcloudcmd -n --non-interactive --path /src /home/mauro/src https://your-next-cloud-domain.org
TimeoutStartSec=300
Slice=nextcloud-sync.slice

[Install]
WantedBy=default.target

Step 4: Create the systemd Timer File
#

The systemd timer will trigger the service at regular intervals. Save the following as ~/.config/systemd/user/nextcloud-sync-src.timer:

[Unit]
Description=Run Nextcloud Sync Src every 5 minutes
Requires=nextcloud-sync-src.service

[Timer]
Unit=nextcloud-sync-src.service
OnUnitInactiveSec=5m
RandomizedDelaySec=5m
AccuracySec=1s

[Install]
WantedBy=timers.target

This configuration ensures that the service runs every 5 minutes with a randomized delay for slight variability.

Step 5: Create a systemd Slice File
#

To limit resource usage, create a slice file to manage CPU and memory allocation. Save this content as ~/.config/systemd/user/nextcloud-sync.slice:

[Unit]
Description=Nextcloud Sync Slice
DefaultDependencies=no
Before=slices.target

[Slice]
CPUQuota=30%
MemoryMax=2G

This configuration will limit the sync process to 30% of the CPU and a maximum of 2 GB of memory.

Step 6: Load and Enable the Timer
#

To activate the service and timer, reload the systemd user daemon and enable the timer:

systemctl --user daemon-reload
systemctl --user enable nextcloud-sync-src.timer
systemctl --user start nextcloud-sync-src.timer

Step 7: Monitor the Service and Timer
#

To check the logs for the service, use:

journalctl --user -u nextcloud-sync-src

To list active timers and see when they last ran or are scheduled to run next, use:

systemctl --user list-timers

Conclusion
#

With this setup, your headless server will automatically sync the specified directories with your Nextcloud server every 5 minutes. This approach keeps your essential files up-to-date without manual intervention, optimizing the workflow for your headless environment.

Reply by Email

Related

Running Multiple Instances of a Service
·679 words·4 mins
Why I Chose This Stack for my K8s Cluster
·857 words·5 mins
How Does a Raspberry Pi 5 Boot an image?
A New Dawn for Secure Linux in Untrusted Environments
Remote Setup with EdgeVPN