Skip to content

Instantly share code, notes, and snippets.

@yuvve
Last active May 4, 2024 08:53
Show Gist options
  • Save yuvve/013977d93ddc3b0c4780de950ba53d89 to your computer and use it in GitHub Desktop.
Save yuvve/013977d93ddc3b0c4780de950ba53d89 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.

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...

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.
  11. Give tasker permission to "run both user-defined and its own tasks" and "run commands in Termux environment".

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

Tasker

Creating tasks

Create a task for every script: Tasker > tasks > create

Actions

Prompting and printing output (optional)

If you want your tasks to ask you for permission and / or print output messages, then configure your tasks like this (see the example below):

1. "pick input dialog" action of type YesOrNo with title "pull?" / "push?" / "reset?".
2. "If" action, If: %input ~ yes
    3. Termux action (the ones defined below)
    4. "If" action, If: %stdout NEQ \%stdout
        5. "Flash" action, Text: %stdout
    6. End if
    7. "If" action, If: %stderr NEQ \%stderr
        8. Flash action, Text: %stderr
    9. End if
10. End if
  • 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

Profile

When you have your tasks ready you can create your Tasker profile (see the example below):

  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.

Examples

Screenshot_2024-01-01-15-19-33-848_net dinglisch android taskerm Screenshot_2024-01-01-15-22-05-062_net dinglisch android taskerm

@aliwo
Copy link

aliwo commented Dec 12, 2023

Hi, Thanks for the guide! Where can I create a profile?

@yuvve
Copy link
Author

yuvve commented Dec 12, 2023

Hi, Thanks for the guide! Where can I create a profile?

Hey,
You need to create a profile inside Tasker. I have made the instructions clearer, thanks for pointing this out!

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