Skip to content

Instantly share code, notes, and snippets.

@ScottJWalter
Forked from yuvve/Obsidian Git-Mobile.md
Last active January 22, 2024 11:14
Show Gist options
  • Save ScottJWalter/6bc31c2e48431e1f74f8eede17d78479 to your computer and use it in GitHub Desktop.
Save ScottJWalter/6bc31c2e48431e1f74f8eede17d78479 to your computer and use it in GitHub Desktop.
Automatically sync notes to and from git repository whenever you open and close Obsidian on mobile. #obsidian #github

How to automatically sync Obsidian between Github and Android with Termux and Tasker

I use Obsidian git on PC, and this workaround (since the plugin doesn't support mobile) to seamlessly* sync my notes to and from my phone. Using these instructions you create an automation for pulling every time you open Obsidian, and pushing every time you close it.

* you can still get merge conflicts...

Downsides of using this

Merge conflicts are not handled and will prevent pushing and pulling. Basically just make sure you only work on your repository from one device at a time.

Preparation

  1. Install Tasker.
  2. Install Termux.
  3. Install Termux tasker plugin.
  4. Open Termux and write termux-setup-storage, give it permissions.
  5. Generate a new ssh key and add it to your Github profile.
  6. pkg install git.
  7. cd ~/storage/shared and clone / create a repository in a folder of your choice. I use ~/storage/shared/Repos/Obsidian.
  8. mkdir ~/.termux/tasker/ and cd ~/.termux/tasker/.
  9. chmod 700 -R /data/data/com.termux/files/home/.termux
  10. Create the scripts below inside the folder from step 7. You need two scripts pull.sh and backup.sh. I added another script - reset.sh, which I use for resetting in case of merge conflict.

Scripts

pull.sh

#!/data/data/com.termux/files/usr/bin/bash
git pull
exit 0

backup.sh

#!/data/data/com.termux/files/usr/bin/bash
git add .
git commit -m "mobile backup"
git push
exit 0

reset.sh

#!/data/data/com.termux/files/usr/bin/bash
git reset --hard
exit 0

Creating tasks

Prompting and printing output (optional)

If you want your tasks to ask you for permission and / or print output messages, then you can build your tasks like this:

1. Pick input dialog YesOrNo "Do this?" (this = pull / push / reset).
2. If %input ~ yes
    3. Termux action (the ones defined below)
    4. If (%stdout NEQ \%stdout)
        5. Flash Text %stdout
    6. End if
    7. If (%stderr NEQ \%stderr)
        8. Flash Text %stderr
    9. End if
10. End if

Creating your Termux actions

For every task create the following Termux action

  • path:
    • script.sh (i.e. backup.sh, pull.sh or reset.sh).
  • arguments:
    • None
  • working directory path:
    • ~/storage/shared/Repos/Obsidian (or whatever you use).
  • execute in a terminal session:
    • unchecked

Creating a profile

When you have your tasks ready you can create your profile:

  1. Choose application -> Obsidian.
  2. Choose your pull task.
  3. Long press the green arrow next to your pull task, and choose "add exit task".
  4. Choose your backup task.

Using reset

I only use the reset task via a shortcut, as automating it is too dangerous and it is not required very often.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment