|
| 1 | +# Backup an Obsidian vault to a GitHub repo |
| 2 | + |
| 3 | +I leverage iCloud to sync my vaults between my Mac, iPhone, and iPad. Unfortunately, iCloud doesn't offer you the following: |
| 4 | + |
| 5 | +- sync status |
| 6 | +- sync activity |
| 7 | +- file version history |
| 8 | +- file restore capabilities |
| 9 | + |
| 10 | +In the early time the servies was flacky, but in recent years has grown a lot, an now it sync pretty reliably. Still, the lack of the aforementioned feature make me feel anxious as the size of my vault increases over time. |
| 11 | + |
| 12 | +So I decided to add an offsite backup solution to the mix. |
| 13 | + |
| 14 | +Periodically commit and push vault file changes to a private GitHub repo. |
| 15 | + |
| 16 | + |
| 17 | +## Create a private GitHub repository |
| 18 | + |
| 19 | +```shell |
| 20 | +# Create a brand new private repository to host the files from the vault |
| 21 | +$ gh repo create zmoog/obsidian-vault-maintenance --private |
| 22 | +✓ Created repository zmoog/obsidian-vault-maintenance on GitHub |
| 23 | + |
| 24 | +# Walk into the vault directory |
| 25 | +cd /Users/${USER}/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/Maintenance |
| 26 | + |
| 27 | +# Initialize a new Git repository |
| 28 | +git init |
| 29 | + |
| 30 | +# Set the GitHub repository as remote |
| 31 | +git remote add origin [email protected]:zmoog/obsidian-vault-maintenance.git |
| 32 | + |
| 33 | +# set git options (they apply to this repository only) |
| 34 | +git config user.name "Automated" |
| 35 | +git config user.email "[email protected]" |
| 36 | + |
| 37 | +# add everything changed to the stage |
| 38 | +git add -A |
| 39 | + |
| 40 | +# create the initial commit |
| 41 | +git commit -m "Latest data: ${timestamp}" |
| 42 | + |
| 43 | +# push and set the GitHub repository as upstream |
| 44 | +git push -u origin main |
| 45 | +``` |
| 46 | + |
| 47 | +## Create a sync script |
| 48 | + |
| 49 | +```shell |
| 50 | +#!/bin/bash |
| 51 | + |
| 52 | +# make the script exit immediately if a command returns a non-zero exit status |
| 53 | +set -e |
| 54 | + |
| 55 | +# list of Obsidian vaults we want to sync |
| 56 | +vaults=("Elastic" "Knowledge") |
| 57 | + |
| 58 | +for vault in ${vaults[@]} |
| 59 | +do |
| 60 | + # walk into the vault directory |
| 61 | + cd /Users/${USER}/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/${vault} |
| 62 | + |
| 63 | + # TODO: check this is a Git repository |
| 64 | + # |
| 65 | + |
| 66 | + # set git options for this repository only |
| 67 | + git config user.name "Automated" |
| 68 | + git config user.email "[email protected]" |
| 69 | + |
| 70 | + # add everything changed to the stage |
| 71 | + git add -A |
| 72 | + |
| 73 | + # commit everything changed |
| 74 | + timestamp=$(date -u) |
| 75 | + git commit -m "Latest data: ${timestamp}" || continue |
| 76 | + |
| 77 | + # push the commit to the remote repo |
| 78 | + git push |
| 79 | +done |
| 80 | +``` |
| 81 | + |
| 82 | +## Schedule the sync every few hours |
| 83 | + |
| 84 | +Open cron |
| 85 | + |
| 86 | +```shell |
| 87 | +crontab -e |
| 88 | +``` |
| 89 | + |
| 90 | +Schedule the script execution every four hours: |
| 91 | + |
| 92 | +```shell |
| 93 | +0 */4 * * * /Users/zmoog/bin/sync-obsidian.sh |
| 94 | +``` |
| 95 | + |
0 commit comments