Obsidian Sync script

Posted by Munish Mehta on Sat, Nov 5, 2022

This is the second part of three part post series. Be sure to checkout part-1 before continuing here.

In this post I am going to create a script which does only one thing: Take all uncommitted changes and pushes them to remote repository. In next part of the series we will create a systemd service and systemd timer to timely execute this script.

High level plan

There are three parts to achieve successful state.

  1. A script which pushes the latest changes to remote
  2. Create a systemd service which executes the script
  3. Create a systemd timer which schedules when to run the script

This part i am going to cover first point which is, to create a script which takes all changes from your obsidian vault and push to remote repository. Three steps for this:

  • git add changed files
  • git commit with some default message
  • git push to remote git repository

The next and the final part of series will cover creating systemd service and timer

Script for push to remote repository

The setup and the script below executes a local script which does a git add, commit and push to git remote obsidian valut.

Local configuration and create script

create .obsidian directory in home

1cd ~
2mkdir -p .obsidian

create .obsidian/obsidian-sync.sh file

1touch ~/.obsidian/obsidian-sync.sh

Add script to push changes to remote repo

 1bash -c "cat >~/.obsidian/obsidian-sync.sh" <<EOF
 2#!/bin/bash
 3obsidian_path=\$1
 4echo "changing to obsidian vault located at \$obsidian_path"
 5pushd \$obsidian_path > /dev/null
 6git add .
 7git commit -m "auto push by obsidian-sync.service"
 8git push
 9if [ \$? -eq 0 ]; then
10    echo "successfully push at `date`"
11else
12    echo "failed push at `date`"
13fi
14popd > /dev/null
15EOF

Test execute the script

1./.obsidian/obsidian-sync.sh ~/Documents/gitrepo/obsidian-personal-vault/

Output

1changing to obsidian value located at /home/munmeh/Documents/gitrepo/obsidian-personal-vault/
2On branch main
3Your branch is up to date with 'origin/main'.
4
5nothing to commit, working tree clean
6Everything up-to-date
7successfully push at Thu 08 Sep 2022 11:24:57 AEST

Continue to last and final of the series where, as promised, I would be creating a systemd user service and a timer which will execute script created here in a timely manner everyday, keeping you worry free loosing your unsaved work.

Reference



comments powered by Disqus