Syncing my notes folder between laptop and phone
Syncing my notes folder between laptop and phone
I wanted to sync my notes folder that was on my laptop with a folder on my phone, so I could access and edit it in both places.
For this I tried 2 approaches:
syncing with rclone on laptop and DriveSync on phone
I dropped this approach since it had some core issues.. I'll explain this at the endsyncing with git
Setting up a git repo on Android
1) Install Termux
Install Termux from the F-Droid app store.
(The version on the Google Play Store is outdated.)
2) Allow access to phone storage
Run this once:
termux-setup-storage
Inside the termux home folder there should now be a folder called storage.
This folder contains references to different parts of the phone filesystem.
Important links:
~/storage/shared -> main internal storage (/storage/emulated/0)
~/storage/downloads
~/storage/pictures
if you want to navigate phone's internal storage folder:
cd ~/storage/shared
3) Install required packages
pkg update
pkg install git openssh
git→ version controlopenssh→ required for SSH authentication
4) Navigate to the folder where you want to set up the git repo
Example:
cd ~/storage/shared/notes
(in my case this is my notes vault)
Git Identity Configuration
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
You can verify it with:
git config --list
SSH authentication setup
Instead of typing passwords every time, we authenticate using SSH keys with GitHub.
6) Generate SSH key pair in Termux
ssh-keygen -t ed25519 -C "your_email@example.com"
You can technically put anything in the comment field.
Email is just the usual convention. I actually used my device name.
Press Enter when it asks for the save location.
This generates public keys for ssh.
7) Display the public key
cat ~/.ssh/id_ed25519.pub
It will look something like this:
ssh-ed25519 AAAAC3Nz.... your_email@example.com
Copy the entire line.
8) Add the key to GitHub
Go to:
GitHub → Profile → Settings → SSH and GPG Keys
Add a new SSH key:
- Click New SSH Key
- Paste the copied key
- Give it a title (like
phone-termux)
9) Test the SSH connection
Run:
ssh -T git@github.com
If everything worked you should see something like:
Hi username! You've successfully authenticated.
Connecting the folder to the GitHub repo
Go to your repo on GitHub and copy the SSH URL.
it should look like this:
git@github.com:username/repo-name.git
10) Initialize git repo
Inside your folder type:
git init
This creates the .git folder which stores the repository metadata.
11) Add the remote repository
paste the copied SSH link here
git remote add origin git@github.com:username/repo-name.git
verify:
git remote -v
First commit
Add all files:
git add .
Create the commit:
git commit -m "initial commit"
Push to GitHub
Rename branch to main (optional but standard):
git branch -M main
Push.
git push -u origin main
Now your repo is connected to GitHub.
Syncing notes between laptop and phone
On phone
Before editing:
git pull
After editing:
git add .
git commit -m "update notes"
git push
On laptop
Here, I had thought that SSH wouldn't be needed since I had never needed to configure it before. But I was wrong.
Because my repo was private repo, all permissions - cloning, editing, adding were restricted.
In public repos, cloning is freely allowed, however, adding and editing needs authorization. I had never required it since I had used VS Code while using git. I have investigated this issue in detail in this post: 🔍How VSCode Secretly Handles Git Authentication (And I Reverse Engineered It)
Steps:
Perform the same process to link repo to folder using SSH.
In order to push, I created this script
#!/bin/bash
set -e # Exit if any command fails
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
printf "Script started...."
# -------- RCLONE BACKUP --------
rclone sync \
/home/anu/anu/notes \
gdrive:My_Notes \
--log-file="$HOME/.my_notes_rclone.log" \
--log-level INFO
printf "\nSuccessfully cloned to gdrive"
# -------- GIT BACKUP --------
DATE=$(date +"%Y-%m-%d %H:%M:%S")
cd /home/anu/anu/notes || exit 1
printf "\nPulling files from git...\n"
git pull --rebase --autostash
#printf "\nCommit all changes?(y/n): "
#read answer
#if "$answer" "n" || "$answer" "N" ; then
# printf "\nNo changes committed.\n"
#else
git add .
printf "\nChanges staged to commit...\n"
# Only commit if there are changes
if ! git diff --cached --quiet; then
git commit -m "$DATE laptop"
if ! git push; then
printf "\nPush failed. Rebasing and retrying...\n"
git pull --rebase --autostash
git push
fi
printf "\nPushed all files to to git successfully.\n"
else
printf "No changes commited.\n"
fi
#fi
printf "\nFinish\n"
Why was this script needed? Why not simply git pull and git commit?
This is due to Branch divergence.
Branch divergence happens when both devices make commits independently before syncing.
Imagine a scenario like this:
Laptop commits something
Phone commits something else
Neither device has the other's commit yet
Now, the history looks like this:
C (Laptop commit)
/
A ---- B
\
D (Phone commit)
Until B, the history was common, but this changed after B,
the phone's history was different from laptop's.
This is branch divergence.
git pull contains the command git merge that basically merges both histories together into a new commit like this:
Laptop commit
|
A --- B --- C
\
D - Phone commit
\
M
|
merge commit
But, what if the same file was edited on both devices? Git can't automatically merge in this case. This produces a merge conflict.
This can be resolved using git pull --rebase
git pull --rebase
temporalily removes local commits, storing them in a seperate file,
A --- B
updates branch from remote
A --- B --- C
then re-apply the local commit
A --- B --- C --- D'
D' is not the same as D.
Unlike D who had B as parent, D' has C (laptop commit) as a parent
The commit history is linear now, instead of a graph. Thus, issue solved.
how does this impact my phone?
Since my phone isn't rooted, Termux has limited filesystem permissions.
operations that are needed for git pull --rebase such as mkstemp() ,
rename() , overwrite file may be blocked by Android storage layer.
And that is why, this operation of resolving merge conflicts has to be done in the laptop that has no restrictions.
Why I dropped the rclone + DriveSync approach
The rclone + DriveSync setup had a few problems:
- syncing delays
- occasional file conflicts led to loss of data
- no version history
- difficult to debug when things break
Git solves most of these problems since:
- every change is tracked
- conflicts are visible
- full version history exists
- it's deterministic
Result
Now my notes are synced between:
- laptop
- phone
- GitHub (as backup)
And I get proper version control on top of it.
Extra: How to do the rclone - Drivesync approach?
This is how it'll work:
Laptop folder ⇄ rclone ⇄ Google Drive ⇄ DriveSync ⇄ Phone folder
Steps:
1. Setting up rclone on laptop
Install rclone for your OS from the official website
https://rclone.org/downloads/
check installation using rclone version
2. Configure Google Drive in rclone
rclone config
Follow this:
n→ new remote- name →
gdrive - storage → choose Google Drive
- client id → press Enter
- client secret → Enter
- scope → choose full access (option 1)
- root folder → Enter
- advanced config → n
- auto config → y
Browser opens → login to Google → allow access.
Test using rclone ls gdrive:. It should list files.
3. Create a Sync folder on Google Drive, or determine which folder to sync
you can create a folder using rclone using
rclone mkdir gdrive:SyncFolder
and then link it using
rclone sync ~/SyncFolder gdrive:SyncFolder
For an already existing folder, you can download updates usiong:
rclone sync gdrive:SyncFolder ~/SyncFolder
4. On the phone
Install Drivesync from Google Play Store.
Sign in to google drive from drivesync
Test a sample sync
Create new sync > Choose the gdrive folder, local folder and then enable 2 way sync
And done!
Comments
Post a Comment