Easily sync your Obsidian vault between your desktop and mobile device without purchasing Obsidian Sync. This script leverages FTP for file transfer and ensures data integrity with backup capabilities.
- Two-Way Sync: Sync files and folders between desktop and mobile to keep your vault updated on both devices.
- Exclusion Rules: Customize which files and folders to exclude from the sync to avoid unnecessary data transfer.
- Automated Backup: Create local backups on your Android device before syncing to prevent data loss.
Important
This guide focuses on Windows and Android, but the steps can be adapted for other operating systems as well.
Caution
Test this script with a mock vault first to ensure the safety of your data. This precaution helps prevent any accidental loss or corruption of your actual vault. Once you have confirmed that everything works as intended, you can proceed to use your main vault.
Tip
Got a suggestion? Comment below!
-
FTP Server running on Desktop How to set up an FTP server on Windows?
Ensure the Obsidian folder on your desktop is accessible through FTP from your mobile beforehand.
-
A Terminal in Mobile (preferably Termux)
-
Install Required Packages in Termux:
pkg update && pkg install lftp rsync
-
Create the Sync Script:
nano obsidian-sync.sh
-
Add the following code to the script:
Don't forget to change the configurations in the
FTP server details
andPatterns to exclude
.#!/bin/bash # FTP server details FTP_HOST="ftp://192.168.1.100:21" # e.g., 192.168.1.100:21 FTP_USER="user" FTP_PASS="password" REMOTE_DIR="/Documents/Obsidian" # Obsidian location on Desktop LOCAL_DIR="/storage/1BC4-1763/Documents/Obsidian" # Obsidian location on Mobile BACKUP_DIR="/storage/1BC4-1763/Documents/Obsidian-Backup" # Backup location on Mobile # Patterns to exclude EXCLUDE_PATTERNS=( # "*.tmp" # Exclude all .tmp files # "*.bak" # Exclude all .bak files # "cache/" # Exclude cache directory # "logs/" # Exclude logs directory # "DoNotSync.md" # Exclude specific file ".obsidian/" # Exclude Obsidian configs ) # Convert EXCLUDE_PATTERNS to rsync format RSYNC_EXCLUDES=() for pattern in "${EXCLUDE_PATTERNS[@]}"; do RSYNC_EXCLUDES+=(--exclude "$pattern") done # Convert EXCLUDE_PATTERNS to lftp format LFTP_EXCLUDES="" for pattern in "${EXCLUDE_PATTERNS[@]}"; do LFTP_EXCLUDES+="--exclude $pattern " done # Function to create a backup using rsync backup_files() { rsync -av --delete "${RSYNC_EXCLUDES[@]}" "$LOCAL_DIR/" "$BACKUP_DIR/" } # Sync function using lftp sync_files() { lftp -u "$FTP_USER","$FTP_PASS" "$FTP_HOST" <<EOF set ftp:ssl-allow no mirror --verbose --delete --only-newer --no-perms $LFTP_EXCLUDES "$REMOTE_DIR" "$LOCAL_DIR" mirror --verbose --reverse --delete --only-newer --no-perms $LFTP_EXCLUDES "$LOCAL_DIR" "$REMOTE_DIR" quit EOF } # Create a backup before syncing backup_files # Call sync function sync_files
-
Make the Script Executable:
chmod +x obsidian-sync.sh
-
Run the Script:
./obsidian-sync.sh
Automating the sync process ensures that your Obsidian vault stays up-to-date without manual intervention. Here are two methods to automate the script:
You can set up a cron job in Termux to run the script at specified intervals. Follow these steps:
-
Install Cron Package:
pkg install cronie
-
Start the Cron Daemon:
crond
-
Edit the Cron Table:
crontab -e
-
Add a Cron Job: Add the following line to run the sync script every hour (adjust the schedule as needed):
0 * * * * /data/data/com.termux/files/home/obsidian-sync.sh
This line means "run the script at the start of every hour."
-
Save and Exit: Save the changes and exit the editor. Your script will now run automatically based on the specified schedule.
Make sure to keep Termux running in background.
For a more flexible approach, you can use automation apps like Automate or Tasker along with the Termux:Tasker plugin to run the script based on specific events or conditions.
-
Install Automate and Termux: Download and install Automate from the Play Store and the Termux:Tasker plugin from the Play Store.
-
Create a New Flow in Automate: Open Automate and create a new flow.
-
Add a Plug-in action Block:
- Add a "Plug-in action" block to the flow.
- In the "Pick plug-in" field, select Termux.
- This will open a new window.
- In executable field, place your
/path/to/obsidian-sync.sh
.- You can put your
obsidian-sync.sh
in/.termux/tasker
to avoid any issue.
- You can put your
- Save and exit, both tasker plugin and the flow.
-
Add a Trigger Block: Add a trigger block (like "Time" for scheduled sync or "Wi-Fi Connected" for event-based sync) and connect it to Plug-in action block.
I'll create a flow for this later, I'm tired now.
-
Save and Enable the Flow: Save your flow and enable it. The script will now run automatically based on your defined trigger.
-
Install Tasker and Termux:Tasker Plugin: Download and install Tasker and the Termux:Tasker plugin from the Play Store.
-
Create a New Profile in Tasker: Open Tasker and create a new profile.
-
Set a Trigger:
- Choose a trigger (like "Time" for scheduled sync or "Wi-Fi Connected" for event-based sync).
-
Add a Task:
- Create a new task and add an action.
- Select "Plugin" -> "Termux:Tasker".
- Tap the configuration icon and enter the path to your script:
/data/data/com.termux/files/home/obsidian-sync.sh
- You can put your
obsidian-sync.sh
in/.termux/tasker
to avoid any issue.
- You can put your
-
Save and Enable the Profile: Save your profile and enable it. The script will now run automatically based on the defined trigger.
By setting up automation, you ensure that your Obsidian vault syncs regularly without manual intervention. Choose the method that best fits your needs and enjoy seamless synchronization across your devices.
Hope this helps!
- Add some picture for better understanding.
- A Video Guide.
- A Automate flow for Obsync.
Also don't hesitate to comment below for potential improvements or any flaws in the code. Or improve the gist formatting anyway.
Thank you 💖