Automating Nextcloud Maintenance on unRAID with a Scheduled Script

Automating Nextcloud Maintenance on unRAID with a Scheduled Script
unRAID "Nextcloud Maintenance" User Script Log Output

As our video production process for YouTube channels ramped up, the required storage grew exponentially. We needed around 12TB of storage to back up final videos, raw recordings, and scripts. Google Workspace quickly became too expensive, costing over four digits annually just for three users. Adding another 5TB of storage on Google Drive costs $432 per year, which doesn't scale well for our needs.

To cut costs, we decided to self-host our own cloud storage solution using Nextcloud on an unRAID server. The one-time cost of adding an 18TB drive (~$300) was far cheaper than Google’s recurring fees, and we’ve gained faster file transfer speeds using our local 1Gbit Ethernet rather than being limited by our 50Mbit upload speed. However, self-hosting comes with its own set of responsibilities, such as system maintenance and data backups.

Here’s a step-by-step guide to automating daily maintenance tasks using a script scheduled through unRAID’s “User Scripts” plugin.

Don’t want to handle this on your own? If you need help or prefer assistance, you can schedule a remote call/meeting for personalized support.

The Script

#!/bin/bash

CONTAINER_NAME="Nextcloud"
ENABLE_APPDATA_SCAN=false
FORCE_APPDATA_SCAN_DAY="Sunday"
CURRENT_DAY=$(date +%A)

if docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME"; then
    echo "Performing Nextcloud maintenance tasks..."

    # Step 1.1: Update apps
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ app:update --all'

    # Step 1.2: Remove invalid dav shares
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ dav:remove-invalid-shares'

    # Step 1.3: Fix missing caldav changes
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ dav:fix-missing-caldav-changes'

    # Step 1.4: Synchronize birthday calendar
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ dav:sync-birthday-calendar'

    # Step 1.5: Sync system address book
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ dav:sync-system-addressbook'

    # Step 1.6: Add missing indexes
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ db:add-missing-indices'

    # Step 1.7: Clean up filecache
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ files:cleanup'

    # Step 1.8: Repair filesystem tree
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ files:repair-tree'

    # Step 1.9: Clean up remote storages
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ sharing:cleanup-remote-storages'

    # Step 1.10: Delete orphan shares
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ sharing:delete-orphan-shares'

    # Step 1.11: Rescan filesystem
    docker exec -i $CONTAINER_NAME /bin/bash -c './occ files:scan --all -n'

    # Step 1.12: Rescan AppData folder (conditional)
    if [[ "$ENABLE_APPDATA_SCAN" == true || "$CURRENT_DAY" == "$FORCE_APPDATA_SCAN_DAY" ]]; then
        docker exec -i $CONTAINER_NAME /bin/bash -c './occ files:scan-app-data'
    fi

    echo "Maintenance complete."
else
    echo "Error: Nextcloud container is not running."
fi

Script Explanation

  • Update Apps
    This command ensures that all installed Nextcloud apps are up to date with the latest versions, preventing security risks and improving performance.
  • Remove Invalid DAV Shares
    This task cleans up any broken or outdated WebDAV shares, ensuring only valid connections remain in the system.
  • Fix Missing CalDAV Changes
    A synchronization step for the CalDAV service, which ensures that calendar data is up-to-date, particularly when changes occur outside Nextcloud’s UI.
  • Synchronize Birthday Calendar
    This command ensures that birthdays stored in user profiles are reflected correctly in the calendar.
  • Synchronize System Address Book
    It updates the system address book to synchronize any new contacts or changes across your Nextcloud instance.
  • Add Missing Indexes
    Ensuring the database is optimized by adding any missing indices, which speeds up database queries.
  • Clean Up Filecache
    Removes any obsolete data from the file cache, reducing potential clutter in the system.
  • Repair Filesystem Tree
    Repairs any issues in the filesystem structure, helping to avoid file corruption or miscommunication between the filesystem and Nextcloud.
  • Clean Up Remote Storages
    Clears out any dead connections or irrelevant remote storage setups that are no longer needed.
  • Delete Orphan Shares
    Deletes any shares that are no longer attached to active files or users.
  • Rescan Filesystem
    This is one of the more time-consuming tasks, as it re-indexes the entire file structure, ensuring Nextcloud accurately reflects all files in its database.
  • Conditional AppData Scan
    This feature allows you to manage the frequency of AppData rescans, which can be resource-intensive. By only running this scan on a specified day or if enabled, you can optimize your maintenance routine while still keeping your data in check.

Script Settings

To adapt the script for your Nextcloud installation, update the following settings:

  • Container Name: Ensure CONTAINER_NAME matches the name of your Nextcloud Docker container.
  • ENABLE_APPDATA_SCAN: Set this to true if you want to perform daily AppData scans.
  • FORCE_APPDATA_SCAN_DAY: Specify the day you want the AppData scan to run if you have disabled daily scans.

How to Use It

  1. Install the "User Scripts" Plugin
    Go to the unRAID web interface, navigate to the Apps tab, and search for the "User Scripts" plugin. Install it to manage your scripts effectively.
  2. Copy the Script
    Copy the provided script into a new script within the "User Scripts" section. You can name it something like "Nextcloud Maintenance."
  3. Update Settings
    Modify the settings for CONTAINER_NAME, ENABLE_APPDATA_SCAN, and/or FORCE_APPDATA_SCAN_DAY to fit your needs.
  4. Set Schedule
    Schedule the script to run, for example, daily.

Conclusion

By scheduling this script daily, I’ve significantly reduced the manual effort needed to maintain our Nextcloud instance, allowing it to run optimally with minimal intervention. The time-saving benefits and regular cleanups ensure the system remains healthy. While Google Drive may offer superior convenience and fewer maintenance requirements, this self-hosted Nextcloud setup provides more control and long-term cost savings, especially when dealing with large amounts of data.

If you’ve found this content helpful and would like to support the blog, consider donating by clicking here. Your support is greatly appreciated!

Read more