Obsidian Sync script

Obsidian series - part 2
Posted by Dev Matters on Saturday, November 5, 2022
This article will setup vault repository in github. Then we show to write a small but effective script to add. commit and push to remote repository.

This article is part of a series.

Obsidian

Table Of Contents

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

cd ~
mkdir -p .obsidian

create .obsidian/obsidian-sync.sh file

touch ~/.obsidian/obsidian-sync.sh

Add script to push changes to remote repo

bash -c "cat >~/.obsidian/obsidian-sync.sh" <<EOF
#!/bin/bash
obsidian_path=\$1
echo "changing to obsidian vault located at \$obsidian_path"
pushd \$obsidian_path > /dev/null
git add .
git commit -m "auto push by obsidian-sync.service"
git push
if [ \$? -eq 0 ]; then
    echo "successfully push at `date`"
else
    echo "failed push at `date`"
fi
popd > /dev/null
EOF

Test execute the script

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

Output

changing to obsidian value located at /home/munmeh/Documents/gitrepo/obsidian-personal-vault/
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Everything up-to-date
successfully 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

This article is part of a series.


comments powered by Disqus