With Obsidian just recently becoming available for mobile, I wanted to find a way to get all the notes from my desktop onto the new blazingly fast app.
Although there is an option to pay for Obsidian Sync, the built-in feature for encrypted syncing, this functionality can be available to everyone for free with a bit of tinkering.
This guide assumes you have already set up Git with your desktop notes. If you haven’t already, here is a great guide by Bryan Jenks.
Step 1: Download apps from Google Play
Download the following apps from the Google Play Store:
Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required.
Step 2: Configure Termux
Firstly, ensure everything is up to date on Termux by running:
apt update && apt upgrade
Then, install ‘cronie’ and ‘termux-services’:
pkg install cronie termux-services
‘cronie’ contains the daemon ‘crond’ which helps us run specified programs at scheduled times
‘termux-services’ allows us to enable the ‘crond’ service.
Next, restart Termux.
Finally, enable ‘crond’:
sv-enable crond
Optional - If you are familiar with vim, you can install and enable it by running:
pkg install vim
export EDITOR=vim
Step 3: Set Up Git
Install ‘git’ and ‘openssh’:
apt install git openssh
‘git’ is version control software
‘openssh’ is a suite of secure networking utilities based on the Secure Shell (SSH) protocol
Set your email and name.
git config --global user.name "<name>"
git config --global user.email "<email>"
Now we can create the SSH keys that we’ll be using as our authentication method to access our Git repository:
ssh-keygen -t rsa -C "<email>"
This creates the folder ‘~/.ssh’ containing our private key (id_rsa), our public key (id_rsa.pub) and some other files which we won’t be using.
Finally we’ll need to add our public key to our Git repository hosting service. In this case I’m using GitHub and the details of how this is done can be found here.
The step above will be easier to do on our desktop so we need to find a way to get our public key there. To make things easier, we’ll set up symlinks to access ‘/storage/emulated/0’ on our device from within Termux via ‘~/storage’:
termux-setup-storage
An easy method would be copying the file to our ‘documents’ folder using a file explorer to share it to our desktop or plugging our phone into our desktop and manually moving it over.
mv ~/.ssh/id_rsa.pub ~/storage/shared/documents/id_rsa.pub
To check if everything is set up correctly clone your repository:
git clone git@github.com:<user>/<repo> ~/storage/shared/documents/my-vault
Step 4: Set Up Git Syncing
This step is based on Bryan Jenks’ Git sync workflow with a few minor adjustments.
I’ll be referring to the source code he has provided here.
Download the ‘zk_sync.sh’ file to your device:
pkg install wget
cd ~/storage/shared/documents
wget https://gist.githubusercontent.com/lucidhacker/0d6ea6308997921a5f810be10a48a498/raw/386fd5c640282daeaa3c9c5b7f4e875511c2946c/zk_sync.sh
‘wget’ is program that retrieves content from web servers
Modify the shebang to the following by replacing line 1 with:
#!/data/data/com.termux/files/usr/bin/bash
Set ‘ZK_PATH’ to the following:
ZK_PATH="/data/data/com.termux/files/storage/shared/documents/my-vault"
Optional - Add source of the git commit to the commit message by modifying the ‘git commit’ command with:
git commit -q -m "Last Sync: $(date +"%Y-%m-%d %H:%M:%S") (Mobile)"
Step 5: Automate Git Syncing
Now that we have our Git syncing working we can automate it to run on a schedule. I like to do it every 30 minutes.
crontab -e
Add the following:
*/30 * * * * bash ~/storage/shared/documents/zk_sync.sh
If you’d like to customise how frequent the script runs use crontab.guru, a great tool to help you create the correct expression
Considerations
The automated syncing will stop if Termux is not running
You can lock apps in multitask on Android by holding the app on the recent apps screen and pressing ‘Lock’
Here’s an alternative method, thanks to /u/kenlin for sharing:
FYI, I just setup a different way of scheduling that I think works better
Installed Termux:API from F-Droid
Used termux-job-scheduler to schedule a script every 15 minutes
termux-job-scheduler -s $HOME/obsidian.sh --persisted true --period-ms 900000 --job-id 1
It seems to run even if termux is not running
my script:
cd /storage/emulated/0/Code/notes git pull --rebase git add --all git commit -m "mobile update $(date)" git push origin main
You should be all set up now to make the most of Obsidian together on desktop and Android!
Subscribe now for more tech, life and ideas.
I think the path in the zk_sync script is incorrect, it missed the home folder, the correct path is :
"/data/data/com.termux/files/home/storage/shared/path-to-vault"
> change path-to-vault to your vault path
Does this still work with the heavily restricted scoped storage on Android 12?