Skip to content

Instantly share code, notes, and snippets.

@viniciusnevescosta
Last active May 24, 2023 03:49
Show Gist options
  • Save viniciusnevescosta/82f44179f6ec074a4df6fdf097873460 to your computer and use it in GitHub Desktop.
Save viniciusnevescosta/82f44179f6ec074a4df6fdf097873460 to your computer and use it in GitHub Desktop.
This Gist ains to help you use obsidian, sync the files in all your devices and auto-backup them.

Obsidian + Syncthing + Gitwatch

This Gist ains to help you use obsidian, sync the files in all your devices and auto-backup them.

Acknowledgements

Installation

1. Let's Install the Tools

Obsidian

Obsidian can be installed in various ways, the easiest method is searching in your software manager (Ex: Gnome Software.)

Syncthing

Installs in all the devices you want to sync.

[sudo] dnf install syncthing

If you want, install in your mobile device too. Just go to your app store and install Syncthing from there.

Gitwatch

[sudo] dnf install git
[sudo] dnf install inotify-tools
git clone https://github.com/gitwatch/gitwatch.git

cd gitwatch

[sudo] install -b gitwatch.sh /usr/local/bin/gitwatch

Configuring

2. Second Steps

Obsidian

I suggest you create a folder called Obsidian under documents directory. Now, create a subfolder to be your Obsidian vault and store all of your anotations (Ex: Sync). We'll sync this subfolder later.

Syncthing

In the terminal run the following command

syncthing

Now open a browser and enter the Syncthing web gui. By default the address is: 127.0.0.1:8384.

Link your remote devices by clicking in Add Remote Device option. Add the device id and the name you want to identify this device.

Let's sync the Obsidian folder! Click in Add Folder, in the general section add a name and the folder path (Ex: /home/user/Documents/obsidian). Now go to Sharing section and select the devices you want to sync. In your devices accept the sync invite.

Congratulations you are now syncing the annotations in all your devices!

But we got a problem... What if we make a mistake or an annotation get corrupt? Well Syncthing will sync the error in all your devices. We don't want that, right? For this reason we need to backup the files, if we look at the Syncthing FAQ, they say:

"Syncthing is not a great backup application because all changes to your files (modifications, deletions, etc.) will be propagated to all your devices. You can enable versioning, but we encourage you to use other tools to keep your data safe from your (or our) mistakes."

We can use Git for versioning only the changes we made and push to a web plataform for a batter control.

Before we use the Gitwatch script, if you want the backup to be done to a repository of another account on Github, you need to set up another account on your computer. Personally, I recommend using an SSH. Here is a Gist showing how to configure it step-by-step.

Gitwatch

Here we'll be using Gitwatch for automatically commit and push the changes. And Github for host all the files and commits.

Go to your subfolder containg your obsidian files. In the terminal run git init.

Now run the following command, remember to remove the '[]'.

cd /
gitwatch -s [seconds until committing] -r [remote repo link] -b main [folder path, ex: /home/user/Documents/obsidian/Sync]

Congrats, now you're versioning your changes and push them automatically to Github!

Automating the process

3. A Step Further

We can go a step further automating all this process by start all this stuff in the boot load.

cd /
mkdir -p "$HOME/.config/systemd/user && cd $HOME/.config/systemd/user"

Syncthing

Create a file named 'syncthing@.service' and add the following content:

[Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %I
Documentation=man:syncthing(1)
After=network.target
StartLimitIntervalSec=60
StartLimitBurst=4

[Service]
User=%i
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0
Restart=on-failure
RestartSec=1
SuccessExitStatus=3 4
RestartForceExitStatus=3 4

# Hardening
ProtectSystem=full
PrivateTmp=true
SystemCallArchitectures=native
MemoryDenyWriteExecute=true
NoNewPrivileges=true

# Elevated permissions to sync ownership (disabled by default),
# see https://docs.syncthing.net/advanced/folder-sync-ownership
#AmbientCapabilities=CAP_CHOWN CAP_FOWNER

[Install]
WantedBy=multi-user.target

Now start the service, replace 'myuser' with the system user name:

systemctl enable syncthing@myuser.service
systemctl start syncthing@myuser.service

Gitwatch

In the same folder, create a file named 'gitwatch@.service' and add the following content, don't forget to replace the ExecStart config with your own args:

[Unit]
Description=Watch file or directory and git commit all changes. run with: systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service

[Service]
Environment="SCRIPT_ARGS=%I"
ExecStart=/usr/local/bin/gitwatch -s [seconds until committing] -r [remote repo link] -b main [folder path, ex: /home/user/Documents/obsidian/Sync]

[Install]
WantedBy=default.target

Again, start the service:

systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service

Bibligraphy

Well that's it, you now can create notes, sync and backup them automatically for free.

For more informations:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment