Rclone

| Comments

Contents:

Install (macOS) #

This is required for getting mount to work properly, see:

Run:

brew install --cask macfuse

brew install gromgit/fuse/rclone-mac

Mount (OneDrive) #

#!/usr/bin/env bash

LOG_PATH="$HOME/Library/Logs/rclone-onedrive-mirror.log"
MOUNT_DIR="$HOME/Library/CloudStorage/OneDrive"
mkdir -p "$MOUNT_DIR"

/usr/local/opt/rclone-mac/libexec/rclone/rclone mount \
    onedrive: "$MOUNT_DIR" \
    --volname "OneDrive" \
    --buffer-size 64M \
    --vfs-read-ahead 512M \
    --vfs-cache-mode full \
    --vfs-cache-max-age 8760h \
    --vfs-cache-max-size 100G \
    --vfs-cache-poll-interval 30s \
    --vfs-write-back 5s \
    --attr-timeout 8700h \
    --dir-cache-time 8760h \
    --poll-interval 30s \
    --log-level INFO \
    --log-file "$LOG_PATH"
    # Optional:
    # --rc \
    # --rc-web-gui \
    # --rc-web-gui-no-open-browser \
    # --rc-htpasswd "$HOME/.config/rclone/htpasswd"

# Optionally, inform the user when the process stops.
# Requires: brew install terminal-notifier
PROC_EXIT_CODE="$?"
terminal-notifier -title "OneDrive mount stopped!" -message "$LOG_PATH"
exit $PROC_EXIT_CODE

Notes:

For serving the remote control web UI, you need credentials:

touch ~/.config/rclone/htpasswd
htpasswd -B ~/.config/rclone/htpasswd alex

macOS service #

The above command supports a --daemon option, but I’d like automatic launch at startup, maybe even restarts. Create a script in ~/bin/mount-onedrive:

Create ~/Library/LaunchAgents/my.rclone-onedrive-mount.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>my.rclone-onedrive-mount</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/myname/bin/mount-onedrive</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>UserName</key>
    <string>myname</string>
  </dict>
</plist>

The service can then be loaded via:

launchctl load -w ~/Library/LaunchAgents/my.rclone-onedrive-mount.plist

Or unloaded via:

launchctl unload ~/Library/LaunchAgents/my.rclone-onedrive-mount.plist

Bisync #

Alternatively there’s a bisync command.

On first run, the database needs to be initialized, via --resync, but as a warning, this can override the files of “path2” with those of “path1”.

This is the onedrive-sync-init script:

#!/usr/bin/env bash

function notifyIfError() {
    PROC_EXIT_CODE="$1"
    if [ "$PROC_EXIT_CODE" -ne 0 ]; then
        terminal-notifier -title "OneDrive sync failed!" -message "Check the logs"
        echo "OneDrive sync failed"
        exit "$PROC_EXIT_CODE"
    fi
}

mkdir -p ~/OneDrive/Bisync
mkdir -p ~/OneDrive/.db

rclone bisync onedrive: ~/OneDrive/Bisync \
    --resync \
    --filters-file ~/.config/rclone/onedrive-filter.conf \
    --workdir ~/OneDrive/.db \
    -v "$@"
notifyIfError "$?"

# Adds .rclone-check files in the top sub-directories
find ~/OneDrive/Bisync -type d -d 1 -exec touch "{}/.rclone-check" \;
notifyIfError "$?"

# Syncs those .rclone-check files
rclone bisync onedrive: ~/OneDrive/Bisync \
    --filters-file ~/.config/rclone/onedrive-filter.conf \
    --workdir ~/OneDrive/.db \
    -v "$@"
notifyIfError "$?"

This is the onedrive-sync-periodic script, which could be installed in crontab:

#!/usr/bin/env bash

rclone bisync onedrive: ~/OneDrive/Bisync \
    --check-access \
    --check-filename ".rclone-check" \
    --filters-file ~/.config/rclone/onedrive-filter.conf \
    --workdir ~/OneDrive/.db \
    -v "$@"

PROC_EXIT_CODE="$1"
if [ "$PROC_EXIT_CODE" -ne 0 ]; then
    terminal-notifier -title "OneDrive sync failed!" -message "Check the logs"
    echo "OneDrive sync failed"
    exit "$PROC_EXIT_CODE"
fi

And then there’s the ~/.config/rclone/onedrive-filter.conf file, which dictates what to sync (aka selective sync):

- **/.DS_Store
- **/.localized
- **/*.swp

+ /Documents/**
+ /Scanned/**
+ /Screenshots/**

# Exclude everything by default
- **