Sync Remote and Local Directories Using macFUSE, SSHFS, and rsync Across Platforms

Sync remote directory mcfuse, rsync, sshfs, square

Synchronizing directories across different platforms can be challenging, especially when dealing with macOS, Linux, Windows, and Android via Termux. Finding a reliable tool that can sync directories, resume interrupted transfers, run as a daemon or cron job, and be cross-compatible is essential. In this guide, we’ll explore how to achieve this using macFUSE, SSHFS, and rsync, and how to troubleshoot common issues, like when macOS suddenly fails to mount a remote directory.

Objective

Our goal is to set up a system that syncs a remote directory with a local directory, ensuring that the sync can resume after interruptions, can be automated, and is compatible across multiple platforms, including macOS, Linux, Windows, and Android via Termux.

Why macFUSE, SSHFS, and rsync?

  • macFUSE: Extends macOS’s filesystem capabilities, allowing you to mount filesystems that are not natively supported.
  • SSHFS: Enables mounting a remote directory over SSH, making it appear as if it were part of your local filesystem.
  • rsync: A powerful tool for syncing directories, rsync can resume transfers, copy only changed files, and be used in conjunction with cron for automation.

Step 1: Installing macFUSE

macFUSE is required on macOS to use SSHFS. Here’s how to install it:

Step 2: Mounting Remote Directory with SSHFS

  1. Download the latest version of macFUSE from the official website.
  2. Install the package by following the on-screen instructions.
  3. Once installed, verify it by running:
kextstat | grep osxfuse

You should see an entry for macFUSE if the installation was successful.

With macFUSE installed, you can now use SSHFS to mount the remote directory.

1. Install SSHFS:

$ brew install sshfs

2. Mount the remote directory:

$ sshfs user@remote_host:/remote/directory /local/mount/point

Replace user, remote_host, /remote/directory, and /local/mount/point with your specific details.

Step 3: Syncing with rsync

Now that the remote directory is mounted locally, you can use rsync to sync the files:

  1. Install rsync (if not already installed):
  • macOS/Linux: Already included.
  • Windows: Use Cygwin or WSL to install rsync.
  • Android: Install Termux and then run:
$ pkg install rsync
  • Sync the directories:
$ rsync -avz --progress /local/mount/point /local/destination

Step 4: Automating with cron (Optional)

To automate the sync process, you can set up a cron job:

  1. Edit the crontab:
$ crontab -e

Add a cron job to run rsync at regular intervals (e.g., every hour):

0 * * * * rsync -avz /local/mount/point /local/destination

Save and exit the editor.

*For a more nuanced instruction on cron, termux, and rsync, check this “How to Run a Bash Script via Cron in Termux: A Quick Guide to Termux Crontab

Step 5: Handling Interruptions

One of the strengths of rsync is its ability to resume interrupted transfers. If the sync is interrupted, simply rerun the rsync command, and it will pick up where it left off.

Challenge: When rsync Isn’t Available on the Server

In some cases, especially on shared hosting services, you may not have the option to install rsync on the server. This presents a challenge since rsync requires both the client and server to have rsync installed to function properly.

Solution: Use SSHFS as a Workaround

Instead of relying on rsync on the server side, you can mount the remote directory locally using SSHFS, and then use rsync to sync the files to a physical directory on your machine.

Here’s a step-by-step breakdown:

Mount the Remote Directory:

$ sshfs user@remote_host:/remote/directory /local/mount/point

Sync with rsync: Once the remote directory is mounted, use rsync to sync it with a local directory:

$ codersync -avz /local/mount/point /local/destination

This method circumvents the need to have rsync installed on the remote server by treating the remote directory as if it were a local one.

Rudimentary Diagram

+-----------------+       +-----------------+       +--------------------+
|  Remote Server  |       |  Local Machine  |       |  Physical Directory|
| (No rsync)      |       | (macOS/Linux)   |       | (macOS/Linux)      |
|                 |       |                 |       |                    |
|   /remote/dir   |<----->|  /local/mount   |<----->|   /local/dest       |
|                 | SSHFS |                 | rsync |                    |
+-----------------+       +-----------------+       +--------------------+

Troubleshooting: Fixing SSHFS Mount Issues on macOS

Sometimes, macOS may suddenly fail to mount a remote directory using SSHFS. Here’s how to troubleshoot and fix this issue:

1. Check Permissions

Ensure that macFUSE and SSHFS have the necessary permissions:

  • Go to System Settings > Privacy & Security > Full Disk Access.
  • Add Terminal (or the app you’re using) to the list and restart the application.
Sync directory security and privacy

2. Remount the Directory

Sometimes, remounting the directory resolves the issue:

$ umount /local/mount/point
$ sshfs user@remote_host:/remote/directory /local/mount/point
If the directory still doesn’t mount, try adding -o allow_other to the SSHFS command:
$ sshfs user@remote_host:/remote/directory /local/mount/point -o allow_other

3. Update macFUSE and SSHFS

Outdated software can cause mounting issues. Ensure that macFUSE and SSHFS are up to date:

For macFUSE, visit the official website to download the latest version.

For SSHFS, update via Homebrew:

$ brew upgrade sshfs

4. Reinstall macFUSE and SSHFS

If updates don’t resolve the issue, consider reinstalling both macFUSE and SSHFS:

$ brew uninstall sshfs
$ brew uninstall macfuse
$ brew install macfuse
$ brew install sshfs

5. Allow System Extensions

macOS may block system extensions after an update. Go to System Settings > Privacy & Security and allow any blocked system extensions for macFUSE. Restart your Mac after making these changes.

6. Reboot Your Mac

Sometimes, simply rebooting your Mac can resolve mounting issues.

Conclusion

By combining macFUSE, SSHFS, and rsync, you can create a robust, cross-platform directory synchronization system that works seamlessly across macOS, Linux, Windows, and Android via Termux. This setup ensures your files are always in sync, whether you’re working remotely or locally, and offers the flexibility to automate the process with tools like cron. And with the troubleshooting tips provided, you’ll be prepared to quickly resolve any issues that arise, ensuring smooth and uninterrupted file synchronization. Plus, the workaround using SSHFS allows you to overcome the limitations of servers that do not have rsync installed, expanding the versatility of your sync setup.

Leave a Reply

Your email address will not be published. Required fields are marked *