Free automated backups of Obsidian notes to GDrive for Mac, Windows and Linux
Tags: obsidian, backup, automation, shell
Published: 2023-12-18 14:34:55

No one wants to lose all their years of note-taking to a spilled cup of coffee on the keyboard.
 
Obsidian has a robust paid [Obsidian Sync](https://obsidian.md/sync) proprietary service that enables two-way sync between devices both on and offline, version history and end-to-end encryption. If these features are important to you, I recommend this service. 

However, if you just want simple free, automated backups of your local notes to GDrive you have options.  

These solutions are scalable in that they only sync file changes and do not copy your entire vault every time.

## Free backups for Mac/Windows
Luckily this one is easy. Google has a free [Google Drive for Desktop](https://support.google.com/drive/answer/10838124?hl=en#zippy=%2Copen-drive-for-desktop%2Cdownload-drive-for-desktop) client available for these operating systems that enables automated real-time, two-way syncing of local directories on your computer.

Once installed, follow [these instructions](https://support.google.com/drive/answer/10838124?hl=en#zippy=%2Csync-a-folder-with-google-drive-or-google-photos) to sync your vault folder to GDrive.


## Free backups for Linux 
Unfortunately there is no Google Drive for Desktop client for Linux today. Here is something I hacked together using [FreeFileSync](https://freefilesync.org/) and a shell script run on a 5 minute cron job. 

_**Note** FreeFileSync does not encrypt your files. If that is important to you, check out [this thread](https://freefilesync.org/forum/viewtopic.php?t=9682) for some solutions._

### 1. Install [FreeFileSync](https://freefilesync.org/download.php)
This tool can be used 100% free for the purposes of this article. 

Consider downloading the [Donation version](https://freefilesync.org/download.php) with some extra thank-you features if you appreciate their service.

Don't be fooled by the malwaresque look of their website. The service is great, community is friendly, and I haven't been frauded yet since making a donation.


### 2. Create a new sync configuration
Open FreeFileSync and create a "New" sync configuration from the upper left. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fih0rda5ynxjue79wjz.png)


### 3. Configure source and destination directories
On the left, select the directory of your Obsidian vault.

On the right, click the cloud icon to configure a connection to your GDrive 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg4xo9n7pdc6pnetpqst.png)

When you click the cloud icon, a connection menu will appear. Authorize a connection to your GDrive, select which drive you will save to, and select a destination directory. 

By default, it will sync to the root of your drive - this is messy! I recommend specifying a destination directory for your vault files, (you can create one if it doesn't exist yet). I named mine the same as my local vault, "claire-thinkpad"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t9crtb9d1gzpygihauf.png)


Once set up, your configure screen should look something like below

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ei4oz0xsbjw4lwz135zb.png)

_FreeFileSync lets you set the sync mode to "mirror" (one-way, left to right), or [other sync modes](https://freefilesync.org/manual.php?topic=synchronization-settings) such as "two-way", which in theory would enable you to pull changes from GDrive and share work across multiple devices. I didn't try two-way sync yet - comment if you do and find it useful!_

### 4. Save configuration as a batch job
Once you have your source and destination directories selected, click "Save as" in the upper left

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwwv9xprp2wmy5oo6p1r.png)

Accept the defaults and click Save as


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fls58d87uluovkt5r3l.png)

Save the `.ffs_batch` file inside your vault. For me that was  `~/Documents/claire-thinkpad`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nod9hgw475y9xz4qq71.png)


### 5. Create the backup script 

First create a shell script file that will execute the sync.

`touch ~/cron/backup.sh`

You can call it something other than `backup.sh`. 

Paste the below script in the file: 

```sh
#!/bin/sh

if ping -c 1 google.com >/dev/null 2>&1; then
  # Internet is up, run batch job via FreeFileSync
  env DISPLAY=:1.0 /usr/local/bin/FreeFileSync /home/claire/Documents/claire-thinkpad/SyncSettings.ffs_batch
else
  # Internet is down, do not run cron job
  exit 1
fi
```

**Replace `/home/claire/Documents/claire-thinkpad/SyncSettings.ffs_batch` with the path to your `.ffs_batch` file**. 

The script checks whether you are connected to the internet by pinging google.com. If true, it will execute a headless run of your batch job via FreeFileSync. 

_I was saved here by Joe Lotz's [post](https://www.joelotz.com/blog/2020/running-freefilesync-backup-from-crontab-on-ubuntu.html) about running background jobs with FreeFileSync. The `DISPLAY=:1.0` is the trick Joe found to run this headlessly, without opening the GUI._

#### Test your script 
Run `~/cron/backup.sh` and then check your GDrive destination folder and see that your files are there. Make sure your local vault is not empty first.

### 6. Create a cron job to execute your script

In your terminal, run `crontab -e` to enter the cron editor.

Add this cron job at the bottom of the file :

```sh
*/5 * * * * /home/claire/cron/backup.sh
```

**Replace `home/claire/cron/backup.sh` with the path to your shell script from step 5**

This will run your backup script every 5 minutes. Feel free to change to a desired interval, with the help of a [cron editor tool](https://crontab.guru/) or ChatGPT. 

## There you have it
Your changed files should now sync every 5 minutes, so long as you have internet connection.

Technically, instead of FreeFileSync you could code up your own diff calculator and Google Drive API calls to post changes directly to Google Drive. The benefit of using FreeFileSync is that it will calculate diffs for you out of the box and handle all the Google Drive API logic. Google APIs are not fun.