2024-05-13    2024-05-13    2846 字  6 分钟

logseq 多端同步 :logseq:同步

仅使用电脑端和 android 手机

推荐使用 坚果云 或者 dropbox , 安卓手机上需要使用的软件:

  • pc 端的同步

    pc 端仅需下载对应软件,开启软件后,使用软件自带的同步盘功能。将对应的目录纳入同步管理中

  • android 端的同步

    使用 坚果云 为例, 图文教程: 如何使用 FolderSync 在安卓手机上同步文件夹到坚果云, 具体步骤:

    1. 开启坚果云的 webdav 功能, 参考: 坚果云第三方应用授权 WebDAV 开启方法
    2. 下载并安装好 FolderSync
    3. 打开 FolderSync ,点击 账号 –> 添加账号 –> WebDAV
    4. 填写服务:
    参数作用
    名称自定义,webdav 的名称 ,步骤 6 需要的名称
    登录名坚果云注册账号
    密码应用密码,步骤 1 生成的应用授权码
    服务器一定是: dav.jianguoyun.com
    端口一定是: 443
    路径/dav/xxx , xxx 为你需要挂载的路径,默认可以访问全部内容
    1. 点击测试,如果测试通过了 点击保存,如果测试没有通过,请 检查步骤 4 中填写的服务参数 是否正确。
    2. 返回 FolderSync 主页, 点击 “+“号 选择刚刚新建服务的 名称
    3. 选择同步类型: 我们选择 双向同步 ,各个同步选项的含意: 到远程(仅上传到云端), 到本地 (仅下载到本地), 双向 (同步文件夹功能)
    4. 设置同步的文件夹,一个是本地文件夹(后面打开 logseq app 是选择的路径),一个是远程文件夹(logesq 云端存放路径)
    5. 点击 保存,设置完成后 点击 FolderSync 的 同步文件夹 按钮,你会看到之前窗口的同步名称,点击同步
    6. 使用 Tasker 其他自动化工具在按照你的需求自动同步

使用 Git 同步 logseq logseq同步gitpcandroidios

  • DONE pc 端 logseq同步pc

    • 创建一个 github 仓库,参考 Quickstart for repositories
    • 使用单独的 ssh-key 控制该 github 仓库.
    • 克隆该仓库为文件夹 <logseq>
    • 将之前创建的 logseq 数据复制到 <logseq> 文件中
    • 添加两个新的 git-hooks 文件到 <logseq>/.git/hooks 目录下,文件名分别为 pre-commit, 和 post-commit
    • 使用 chmod +x .git/hooks/pre-commit && chmod +x .git/hooks/post-commit 授予 pre-commit 和 post-commit 执行权限
    • 打开 logseq 将 <logseq> 文件作为新的 graph。
    • (可选) 打开 logseq -> settings -> Version Control -> Toggle on “Enable Git auto commit” . 来在使用 logseq 时,自动 commit 并提交到 github 仓库
    • (可选) 使用插件logseq-git-plugin 来手动提交 commit 到 github 仓库
    • pre-commit 和 post-commit 内容和作用

      代码来自:Logseq-Git-Sync-101 的 hooks 目录下的 pre-commit 和 post-commit 文件

      • pre-commit

        作用:每次 commit 前同步远程仓库数据,避免覆盖提交,内容如下

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        
        #!/bin/sh
        #
        #
        # Pull before committing
        # Credential handling options:
        #  - hardcode credentials in URL
        #  - use ssh with key auth
        #  - https://git-scm.com/docs/git-credential-store
        #  - git credential helper on windows
        
        # Redirect output to stderr, uncomment for more output for debugging
        # exec 1>&2
        
        output=$(git pull --no-rebase)
        
        # Handle non error output as otherwise it gets shown with any exit code by logseq
        if [ "$output" = "Already up to date." ]; then
            # no output
            :
        else
            # probably error print it to screen
            echo "${output}"
        fi
        
        git add -A
        
      • post-commit

        作用: 每次提交后,执行自动推送到远程仓库。 内容如下:

        1
        2
        3
        
        #!/bin/sh
        
        git push origin main
        
    • git 仓库的一些优化选项

      • 不提交 logseq 目录下的 bak 目录和 .recycle 和一些常见的忽视文件
      1
      2
      3
      4
      5
      6
      
      logseq/bak/
      logseq/.recycle
      
      .DS_Store
      .idea
      *.log
      
  • DONE 安卓端 logseq 同步 logseqandroid

    已知可以管理 git 仓库的工具有: MGitTermux , 这里使用 Termux,因为 Termux 可以借用其他工具实现自动化操作,而 Mgit 目前没有不进入 Mgit 来完成 git 的操作。

    • termux 操作步骤

      1. termux 配置 git 环境
      1
      2
      3
      
      pkg install openssh git
      git config --global user.name "username"
      git config --global user.email "example@xxx.com" 
      
      1. 执行 termux-setup-storage 授权 termux 可以访问 SD 目录,

      2. 复制你在 pc 端上创建的 ssh-key 到 android 上,或在 termux 中重新构建 ssh-key 并给它可以访问远程仓库的权限。

      3. 克隆远程仓库到本地

      1
      2
      
      cd ~/store/shared/Documents && git clone --depth 1 git@github.com:<username>/<repos>.git
      git config --global --add safe.directory ~/storage/shared/Documents
      
      1. 安装 Termux:Widgets 建议 Termux 和 Termux:Widgets 都使用 F-Droid 进行安装,避免一些意外错误
      2. 在 Termux 中创建 ~/.shortcuts 文件,并添加两个可执行脚本,一个用来远程同步,一个用来推送到仓库
      3. 将脚本复制到 ~/.shortcuts 目录后执行,termux-fix-shebang xx.sh 修复,因为 shellbang 不对出现的错误
      4. 执行脚本时,需要 termux 在后台运行
      • pull 脚本

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        
        #!/usr/bin/env bash
        eval $(ssh-agent)
        source ~/../usr/bin/source-ssh-agent # path might varied based on your system and brand
        function send-notify() {
            if type termux-notification > /dev/null 2>&1; then
                termux-notification -t "logseq pull" -c "$*"
            fi
        }
        if [[!  -d $HOME/storage/shared/Documents/logseq ]]; then
           cd $HOME/storage/shared/Documents
           git clone --depth 1 git@github.logseq:<username>/<repo-name>.git
           cd logseq  || {
               send-notify "git clone 执行出问题"
               exit 1
           }
           git config --global --add safe.directory $PWD
        else
            cd $HOME/storage/shared/Documents/logseq || {
                send-notify "没有进入指定目录" "$HOME/storage/shared/Documents/logseq"
                exit 1
            }
        fi
        
        git pull
        
        if [[ $? == 0 ]]; then
            send-notify "拉取成功"
        else
            send-notify "拉取失败"
        fi
        
      • push 脚本

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        
        #!/usr/bin/env bash
        eval $(ssh-agent)
        source ~/../usr/bin/source-ssh-agent # path might varied based on your system and brand
        function send-notify() {
            if type termux-notification > /dev/null 2>&1; then
                termux-notification -t "logseq push" -c "$*"
            fi
        }
        if [[ !  -d $HOME/storage/shared/Documents/logseq ]]; then
            send-notify "目录 $HOME/storage/shared/Documents/logseq 不存在"
            exit 1
        else
            cd $HOME/storage/shared/Documents/logseq || {
                send-notify "没有进入指定目录" "$HOME/storage/shared/Documents/logseq"
                exit 1
            }
        fi
        
        git add -A && git commit -m "Android edit on $(data +"%Y-%m-%d %H:%M:%S %z")" && git push
        
        
        if [[ $? == 0 ]]; then
            send-notify "推送成功"
        else
            send-notify "推送失败"
        fi
        
    • 使用 Termux:Tasker 配合 Tasker 同步 logseq taskertermuxlogseq

      1. 安装 termux:tasker ,建议 termux 和 termux:tasker 都使用 F-Droid 安装
      2. 在 Termux 中创建 ~/.termux/tasker 文件,将该内容下 的 pull,push 脚本复制到 ~/.termux/tasker 目录下,并赋予执行权限, 并修复 shellbang 的错误,和测试脚本是否可用
      3. 打开 Tasker -> 任务 –> “+” –> “logseq pull” –> “加号” –> 插件 –> Termux:Tasker –> 配置 –> Executable: 你命名的 pull 脚本名 –> “save” – 运行
      4. 按照步骤三创建新的任务 logseq push , executable 为 你的 push 脚本名
      5. 高阶用法,创建一个任务 logseq pull 后在打开 logseq 文件, 关闭 logseq 程序后自动执行 logseq push 命令
  • logseq ios 端的同步 logseqiosgit

    可选工具:

    1. Working Copy 免费版仅支持 git pull 操作, 不支持 git push 操作, 如果你在 ios 端只读使用,非常推荐。不在乎价格,也推荐
    2. iSH 一个 Linux shell 工具,可以安装 git ,但不支持 捷径 操作。 所以不推荐
    3. a-shell 一个免费开源的终端工具,可以对远程仓库进行拉取,推送,无法完全自动化拉取,备份
    • a-shell 同步 logseq 方法 :logseq:ios:a-shell:

      • 复制之前创建的 ssh-key 密钥和公钥到 iso 端。(你也可以在 a-shell 中创建新的)
      • (可选) 如何克隆时提示你需要 ssh key
      1
      
      lg2 config user.identityFile "~/Documents/.ssh/id_ed25519"
      
      • 克隆远程仓库到本地
      1
      
      lg2 clone --depth 1 git@github.com:<username>/<repo-name>.git
      
      • 设置 username 和 email
      1
      2
      
      lg2 config user.name "John Doe"
      lg2 config user.email "Johndoe@example.com"
      
      • 创建快捷方式 打开 logseq 时,拉取远程仓库,关闭 logseq 时,自动推送到远程仓库.
      • 存在问题,logseq 仅对自己文件用于权限。
    • working copy 同步流程 :logseq:ios:working-copy:

      • 打开文件 app 进入 -> 我的 Iphone -> 在 Logseq 图标下新建一个目录(该图标出现的前提,需要打开一次 logseq app)
      • 打开 Wokring Copy -> Settings –> Hosting Providers -> Click Test -> Sing-in your Github account
      • open Working Copy -> Click “+” -> Clone repository -> Choose your repo and hit Clone
      • 下载仓库后:
        • 点击该仓库名, -> 点击 Repository -> 点击 标题上的 down carrot (下箭头)
        • 选择 Link Repository to Directory
        • 此时进入 文件 app 的操作界面,选择 Logseq 图标下的目录(步骤一新建的),打开
      • 打开 Logseq 将步骤一新建的目录添加到新的图表(graph)
      • ShortCuts 操作
        • 打开 ShortCUts -> Automation(自动化) -> Create Personal Automation:
          1. 创建 pull 快捷方式:
            1. 点击 在 App 打开时 –> 选择程序 Logseq -> 下一步
            2. 新的快捷方式,搜索操作 -> Pull Repository , 选择刚才 repo
            3. 构建完成(可以选择打开 app 后手动确认还是立即执行)
          2. 创建 push 操作:
            1. app 选择在 logseq 关闭时
            2. 新的快捷方法
            3. 动作 stage for Commit , Path “*” , Repo: your Logseq repo
            4. if 条件 input: 选择变量 Filenames 。条件: 有任意值,(has any value)
            5. if 下的操作: commit Pepository, Push Repository
            6. commit Repository: Repo: your Logseq repo 。 Message: “Auto-commit form ios”
            7. push Repository: Repo: Your Logseq repo .
      • 测试是否同步

参考

  1. CharlesChiuGit–Logseq-Git-Sync-101
  2. Alternative way of git syncing on iOS using a-shell for free - Customization / Look what I built - Logseq
  3. Failed to authenticate SSH session: Unable to open public key file · Issue #547 · holzschu/a-shell
  4. logseq笔记软件实现多终端(window,MAC,IOS,Android)自动同步的操作步骤【完结】_哔哩哔哩_bilibili
  5. [[https://zhuanlan.zhihu.com/p/553238392][Logseq 系列之 Git 同步 - 知乎]]
  6. [[https://zhuanlan.zhihu.com/p/565028534][42号笔记:iOS上使用iSH的git同步Obsidian - 知乎]]
  7. [[https://utgd.net/article/20315][Obsidian 的手机端同步方案,iOS + Git + Shortcuts 实现自动同步 | #UNTAG]]
  8. [[https://forum-zh.obsidian.md/t/topic/10083/10][ios上使用iSH的git同步obsidian - 经验分享 - Obsidian 中文论坛]]