Logseq DB (v2) and git sync

By chimo on (updated on )

Just jotting down notes on a naive way of syncing my Logseq (v2) graph to a remote git repository using the Markdown Mirror Logseq feature.

  1. Initialize a git repository somewhere (ex: ~/logseq-git).
  2. Enable Markdown Mirror in your Logseq graph.
  3. Create a `logseq` alias/script which
    1. Starts an auto-commit loop in the background.
    2. Launches Logseq.
    3. Auto-commits one last time once Logseq terminates (ex: when Quitting).
~/.local/bin/logseq
#!/bin/sh -eu

git_dir="~/logseq-git/.git"
work_tree="~/logseq/graphs/Demo/mirror/markdown"

auto_commit() (
    # Add all files
    git --git-dir="${git_dir}" --work-tree="${work_tree}" add -A

    # Commit
    git --git-dir="${git_dir}" --work-tree="${work_tree}" commit -m 'Auto-commit.' || true

    # Push
    git --git-dir="${git_dir}" --work-tree="${work_tree}" push || true 
)

main() (
    # Auto-commit loop
    (while true
    do
        auto_commit
        sleep 60
    done)&

    loop_pid=${!}

    # Launch logseq
    logseq

    kill "${loop_pid}"

    # Commit on close
    auto_commit
)

main

Notes:

  1. This would probably be better as an actual Logseq plugin.
  2. The script above only auto-commits a single graph (I only have one).
  3. From limited testing, it seems to be working okay but time will tell.
  4. Some weird things may or may not happen if multiple Logseq instances are launched via this script (I only ever have one instance running).