DEV Community

sa3i0l
sa3i0l

Posted on • Updated on

Setting up Git syncing for Obsidian on Android

Setting up Git syncing for Obsidian on Android


Download the following apps from the Google Play Store:

Obsidian

Termux

  • install termux from fdroid !

On first launch run this command: yes | pkg upgrade
or apt update
apt upgrade && apt update

How to install packages
Termux uses apt package manager, just like Debian. However we highly recommend to use the pkg utility which is a wrapper for apt.

Install package: pkg install package-name

Uninstall package: pkg uninstall package-name

Upgrade system: pkg upgrade

List installed packages: pkg list-installed

Search for a package: pkg search query

Step 2: Configure Termux

Firstly, ensure everything is up to date on Termux by running:

apt update && apt upgrade
Enter fullscreen mode Exit fullscreen mode

Then, install ‘cronie’ and ‘termux-services’:

pkg install cronie termux-services
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Optional - If you are familiar with vim, you can install and enable it by running:

pkg install vim
export EDITOR=vim
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up Git

Install ‘git’ and ‘openssh’:

apt install git openssl-tool openssh 
Enter fullscreen mode Exit fullscreen mode
  • ‘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>"
Enter fullscreen mode Exit fullscreen mode

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>"
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To check if everything is set up correctly clone your repository:

git clone git@github.com:<user>/<repo> ~/storage/shared/documents/my-vault
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • ‘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
Enter fullscreen mode Exit fullscreen mode
  • for .shortcuts/ you should add, also this except for this shebang above HOME="/data/data/com.termux/files/home"

Set ‘ZK_PATH’ to the following:

ZK_PATH="/data/data/com.termux/files/storage/shared/documents/my-vault"
Enter fullscreen mode Exit fullscreen mode

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)"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Add the following:

*/30 * * * * bash ~/storage/shared/documents/zk_sync.sh
Enter fullscreen mode Exit fullscreen mode
  • 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’

Using termux-job-scheduler

Install Termux:API from F-Droid
Use termux-job-scheduler to schedule a script every 15 minutes

--job-i should be increased by +1 for each next job you have
to see which one should go next: termux-job-scheduler -p

termux-job-scheduler -s $HOME/obsidian.sh --persisted true --period-ms 900000 --job-id 1   
Enter fullscreen mode Exit fullscreen mode

It runs 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
Enter fullscreen mode Exit fullscreen mode

Note: make script executable (+x) before running termux-job-scheduler:
chmod +x /data/data/com.termux/files/home/notes.sh
termux-job-scheduler -s ~/obsidian_notes.sh --persisted true --period-ms 900000 --job-id 1


Widget

to have a shortcut widget, to manually update outside of sync on interval:

https://github.com/termux/termux-widget

Create ~/.shortcuts/ directory.
mkdir -p /data/data/com.termux/files/home/.shortcuts
chmod 700 -R /data/data/com.termux/files/home/.shortcuts


Create ~/.shortcuts/tasks directory.

mkdir -p /data/data/com.termux/files/home/.shortcuts/tasks
chmod 700 -R /data/data/com.termux/files/home/.shortcuts/tasks
Enter fullscreen mode Exit fullscreen mode

Script


#!/data/data/com.termux/files/usr/bin/bash
# ^^^^^^^^^^^^^^^ This says find the first instance of a sh (shell)
# binary and use that shell to execute these commands.
# There is little to no complexity here and no bashisms so it
# should work just fine on most systems and instances of shells
# (bash, zsh, sh, etc.)



ZK_PATH="storage/shared/documents/obsidian-notes"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ We are assigning the variable `ZK_PATH`
# with the (maybe) long string to our vault's location (mine is super
# long so this makes the final command look cleaner,
# it's unnecessary if you care)

cd "$ZK_PATH"
# ^^^^^^^^^^^ cd: Change Directory to your vault's location

git pull
# ^^^^^^ So if any changes occurred remotely or on another machine
# your local machine knows to pull those changes down instead of
# having to wait for a local change to run the script

CHANGES_EXIST="$(git status --porcelain | wc -l)"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ we are assigning
# a value to the variable `CHANGES_EXIST`, the value is the output
# of `git add --porcelain` which outputs a simple list of just the
# changed files and then the output is piped into the `wc` utility
# which is "word count" but with the `-l` flag it will count lines.
# basically, it says how many total files have been modified.
# if there are no changes the output is 0

if [ "$CHANGES_EXIST" -eq 0 ]; then
    exit 0
fi
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The whole if block is saying
# in plain english: if there are no changes (CHANGES_EXIST = 0)
# then exit with no error code `exit 0` if there are changes,
# then continue on with the script

git pull
# ^^^^^^ git pull: this will look at your repo and say "any changes?"
# if there are they will be brought down and applied to your local machine
# In the context of a team environment, a more robust approach is needed
# as this workflow doesnt factor in branches, merge conflicts, etc
# but if you leave your home machine, do work on the work machine,
# push to the remote repo before you return to the home machine, then
# you can just get the latest changes applied to the home machine and
# continue on like normal
git add .
# ^^^^^^^ git add. = add all current changes in the repo no
# matter the level of nested folders/files
git commit -q -m "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")(Mobile)"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# git commit -q -m: this says we are committing changes to
# our repo, -q says BE QUIET no output prints to terminal
# if ran manually, -m defines a message for the commit log
# the -m message is "Last Sync: $(date +"%Y-%m-%d %H:%M:%S")" this
# runs the command date with the formatting arguments for a
# date in YYYY-MM-DD HH-MM-SS format as your commit message
git push -q
# ^^^^^^^^^ git push -q: push the changes to github and
# BE QUIET about it The semicolons between commands are
# just saying run each command and then run the subsequent
# command, they're just separators


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
abdullhakim-sami profile image
sanabel-al-firdaws

there is now an easier way using the Obsideian git plugin

i was having problems using it at first but now i got it setup you only need to have a personal access token and fill out your info in the plugin settings then clone your repo using the plugin make changes then use the command commit all with specific message then push and thats it

it has some performance issues but for me it works great