Setup git+ssh on Android (Termux)
Thinking of past
I have setup a great backup system for my Obsidian and Logseq, so …. How can I use my second brain (notes) from my smartphone ??? 🤔
Thats the question which ended up writing this post 😄
The process is done for Obsidian and tested…. In the future I’ll use it for Logseq too because I found this software really interesting and minimal.
I have to follow minimalist because I have some sort personality like ADHD (complex thing to point where I’ll locked up)
Lets start
Install packages
⚠️ You can use
pkg
instead ofapt
1
apt update && apt install git openssh openssl-tool netcat-openbsd
netcat-openbsd
listed here to use proxy for ssh 💡
If you run git
on ssh
without openssl-tool
you will face an error
Configure keys
Generate a key if you want to use different keys in your laptop and your smartphone
How to generate key? → https://blog.alerezaa.ir/posts/backup-obsidian-logseq/#generating-ssh-key-and-add-it-to-github (process is the same: generate the key without passphrase, select proper directory and copy FILE.pub
contents)
Move (or specify directory during generation) to /data/data/com.termux/files/home/.ssh/
or wherever suggested by generator during generating the key.
Clone the repository (with two tricks)
For the first time you’ve to run this line to clone the repo (replace KEY
and FOLDER
)
1
GIT_SSH_COMMAND='ssh -i /data/data/com.termux/files/home/.ssh/KEY' git clone git@github.com:USERNAME/REPO.git /storage/emulated/0/Documents/FOLDER
GIT_SSH_COMMAND='ssh -i /..../.ssh/id_res'
→ tells git
run ssh
using this options
/storage/emulated/0/Documents/FOLDER
→ specify final directory to clone the repo into
this directory maybe differ on other devices (mine is Huawei Mate 10), you can find paths by using Material Files application
⚠️🚧 If you face error “permission is too open”: chmod 600 /data/data/com.termux/files/home/.ssh/
Post-cloning
Follow the introduction here: https://blog.alerezaa.ir/posts/backup-obsidian-logseq/#local-configurations
Add configurations to .git/config
What else?
Because git
plugin doesnt work on smartphone, I think its better to wrte a function in bashrc
to control this cloning process with ease 🤔
First you have to run this
1
git config --global --add safe.directory /storage/emulated/0/Documents/FOLDER
After that, add below code to your ../usr/etc/bash.bashrc
1
2
3
function cloneobsidian () {
GIT_SSH_COMMAND='ssh -i /data/data/com.termux/files/home/.ssh/KEY' git -C /storage/emulated/0/Documents/FOLDER pull
}
Then, Every time use cloneobsidian
to pull the latest updates from github 😃
Note that future errors (due to changes in repo or anything else) should be solved by googling the error message!
We are all set here
∾ For ease of use, check below (”Configurations to be add to…”)
How about Logseq and push
option
I decided to use Logseq as daily logger and planner because its minimal and I can nest into details as long as I can (also, it is open source, free and privacy-friendly)
Most of use cases of Logseq for this purpose is based on Computer but I decided to be more potable.
I enabled Logseq backup (using git
and Github) as I previously described in a post, and now I tried to enable two-ways backing up for my smartphone (obsidian was one-way, only getting files from Github to my smartphone).
FIrst of all, install Termux and run this command to install required softwares:
1
apt update && apt install git openssh openssl-tool netcat-openbsd
How enable backup in smartphone for Logseq
Its same as above (Obsidian), after cloning repo:
1
GIT_SSH_COMMAND='ssh -i /data/data/com.termux/files/home/.ssh/LOGSEQKEY' git clone git@github.com:USERNAME/LOGSEQREPO.git /storage/emulated/0/Documents/MyLogseq
Go to cloned folder and edit .git/config
file
1
2
3
4
5
6
7
[user]
name = Name
email = name@Email.com
[core]
...
sshCommand = "ssh -i ~/.ssh/LOGSEQKEY"
add these lines to it (add [user]
part and add another line to [core]
part)
∾ For ease of use, I decided to put everything in bashrc
Configurations to be add to bashrc
open ../usr/etc/bash.bashrc
using nano
information below
1
2
3
4
5
6
7
8
9
10
11
12
function obsidianclone () {
GIT_SSH_COMMAND='ssh -i /data/data/com.termux/files/home/.ssh/obs_mob' git -C /storage/emulated/0/Documents/MyObsidian pull
}
function logseqpull () {
git -C /storage/emulated/0/Documents/MyLogseq pull --rebase
}
function logseqpush () {
git -C /storage/emulated/0/Documents/MyLogseq add .
git -C /storage/emulated/0/Documents/MyLogseq commit -m "mobile $(TZ=Asia/Tehran date +"%H%M")"
git -C /storage/emulated/0/Documents/MyLogseq push
}
- Then run
source ../usr/etc/bash.bashrc
and just type function names to run the codes: obsidianclone
Pull Obsidian changes from repositorylogseqpull
Pull Logseq changes from repositorylogseqpush
Push changes to repo
Make sure that you are working on only one device, Desktop or Mobile. Working on two devices simultaneously will lead to error
Set proxy in terminal
(These two command in useful for who lives in countries which has censorship mechanism)
1
2
export http_proxy="http://127.0.0.1:9999"
export https_proxy="http://127.0.0.1:9999"
OR add below part at the end of ../usr/etc/bash.bashrc
1
2
3
4
5
6
7
8
9
10
function setproxy () {
export https_proxy="http://127.0.0.1:9999"
export http_proxy="http://127.0.0.1:9999"
#export all_proxy=socks5://127.0.0.1:8888
}
function unsetproxy () {
export https_proxy=""
export http_proxy=""
#export all_proxy=""
}
then run source ../usr/etc/bash.bashrc
and use setproxy
/unsetproxy
to set/unset proxy.
- Just type function names to run the codes:
setproxy
to set proxy for terminal (Termux)unsetproxy
to remove proxy (These two command in useful for who lives in countries which has censorship