How to Set Up Automatic Syncing for Obsidian Using GitHub for Free

Jun Takemura · November 27, 2024

How to Set Up Automatic Syncing for Obsidian Using GitHub for Free

This article explains how to automatically back up and sync your Obsidian Vault to GitHub for free. It also covers setting up GitHub on Kali Linux as a prerequisite and making your notes accessible via GitHub Wiki.

Note: If you work with a client, uploading data to a cloud service may not be allowed. Make sure to check the data handling policy.


Prerequisites

Install Git on Kali Linux

sudo apt update
sudo apt install git

Configure GitHub SSH Access

  1. Generate an SSH key:
    ssh-keygen -t ed25519 -C "[email protected]"
    

    Press Enter to accept the default file location and add a passphrase.

  2. Add your SSH key to GitHub: Copy the public key:
    cat ~/.ssh/id_ed25519.pub
    

    Go to GitHub > Settings > SSH and GPG Keys > New SSH Key, and paste the key.

  3. Test the connection:
  4. Add the SSH agent settings to ~/.zshrc:
    if ! pgrep -u "$USER" ssh-agent > /dev/null 2>&1; then
      eval "$(ssh-agent -s)" > /dev/null
    fi
    ssh-add -l > /dev/null 2>&1 || ssh-add ~/.ssh/id_ed25519 > /dev/null 2>&1
    

Apply it:

source ~/.zshrc

Create a Private GitHub Repository

  1. Log into GitHub.
  2. Create a new repository and set it to Private.

Set Up Obsidian Vault Backup

Initialize Git in Your Vault

  1. Navigate to your Obsidian Vault:
    cd /path/to/ObsidianVault
    
  2. Initialize a Git repository:
    git init
    
  3. Add the GitHub remote:
    git remote add origin [email protected]:username/repo-name.git
    
  4. Create a .gitignore file to exclude unnecessary files:
    echo ".obsidian/cache/" >> .gitignore
    echo ".obsidian/workspace" >> .gitignore
    echo ".obsidian/plugins" >> .gitignore
    

First Commit and Push

  1. Add all files:
    git add .
    
  2. Commit the changes:
    git commit -m "Initial commit of Obsidian Vault"
    
  3. Push to GitHub:
    git branch -M main
    git push -u origin main
    

Automate Syncing

Create a Sync Script

  1. Create a file named sync_obsidian.sh:
    vim ~/sync_obsidian.sh
    
  2. Add the following script:
    #!/bin/bash
    VAULT_DIR="/path/to/ObsidianVault"
    cd $VAULT_DIR
    git pull --rebase
    git add .
    git commit -m "Auto-sync: $(date)"
    git push
    

You can download the refined version of this script from my GitHub: sync_obsidian

  1. Make the script executable:
    chmod +x ~/sync_obsidian.sh
    

Automate with Cron

  1. Open the crontab editor:
    crontab -e
    
  2. Add a cron job to sync every hour:
    0 * * * * ~/sync_obsidian.sh
    

Add Notes to GitHub Wiki

Clone the Wiki Repository

  1. Clone the GitHub Wiki repository for your project:
    git clone https://github.com/username/repo-name.wiki.git ~/ObsidianWiki
    

Sync Markdown Files to the Wiki

  1. Use rsync to copy Markdown files from your Obsidian Vault to the Wiki repository (excluding unnecessary files):
    rsync -av --exclude=".obsidian" --exclude="*.jpg" --exclude="*.png" /path/to/ObsidianVault/ ~/ObsidianWiki/
    
  2. Commit and push the changes to the Wiki:
    cd ~/ObsidianWiki
    git add .
    git commit -m "Sync notes to GitHub Wiki: $(date)"
    git push
    

Access Notes via GitHub Wiki

Navigate to the Wiki tab on your GitHub repository to view and read your synced notes.


Verify Syncing

Make changes to your vault in Obsidian.
Wait for the cron job to run or execute the script manually:

~/sync_obsidian.sh

Check your GitHub repository and Wiki to confirm changes are backed up and visible.

Twitter, Facebook