For those that find it useful
so i keep my vault on icloud for storage but on other machines like my fire walled work machine i can only access github so i also sync my vault with github. to keep all my configs and workspace state with exactly what my editor was when i left it on my personal machine here is what i did:
- You need git installed already, and this works seamlessly if you have your credentials cached so you dont need to enter your password on
git push
commands - make an empty repo on github
- make my vault a git repo:
git init
git add .
git commit -m “init”
- Ignore files that are uneccesary and could cause errors on new workspace
- make a
.gitignore
file with the contents:
.obsidian/cache
.trash/
.DS_Store
- Now push everything to the remote repo:
git remote add origin https://github.com/USER/REPONAME.git
git push -u origin master
that will put your files on github ONCE now any updates need to be manually added with git commands etc. but why not automate the crap out it
- I make a shell script in my
.local/bin/
directory (mac and linux, windows IDK if this works the same with WSL/Gitbash):
touch zk_sync
chmod +x zk_sync
- Shell script contents:
#!/usr/bin/env sh
ZK_PATH=”PATH TO YOUR VAULT”
cd “$ZK_PATH”
git pull
CHANGES_EXIST=”$(git status — porcelain | wc -l)”
if [ “$CHANGES_EXIST” -eq 0 ]; then
exit 0
fi
git add .; git commit -q -m “$(date +”%Y-%m-%d %H:%M:%S”)”; git push -q
- WHAT THE SCRIPT DOES:
you should never run a script without knowing what it does as it could easily be malicious and nuke your machine so line by line:
#!/usr/bin/env sh
# ^^^^^^^^^^^^^^^ This says find the first instance of a sh (shell)
# binary and use that shell to execute these commands.
# There is little to no complexity here and no bashisms so it
# should work just fine on most systems and instances of shells
# (bash, zsh, sh, etc.)
ZK_PATH=”PATH TO YOUR VAULT”
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ We are assigning the variable `ZK_PATH`
# with the (maybe) long string to our vault’s location (mine is super
# long so this makes the final command look cleaner,
# it's unnecessary if you care)
cd “$ZK_PATH”
# ^^^^^^^^^^^ cd: Change Directory to your vault’s location
git pull
# ^^^^^^ So if any changes occurred remotely or on another machine
# your local machine knows to pull those changes down instead of
# having to wait for a local change to run the script
CHANGES_EXIST=”$(git status — porcelain | wc -l)”
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ we are assigning
# a value to the variable `CHANGES_EXIST`, the value is the output
# of `git add — porcelain` which outputs a simple list of just the
# changed files and then the output is piped into the `wc` utility
# which is “word count” but with the `-l` flag it will count lines.
# basically, it says how many total files have been modified.
# if there are no changes the output is 0
if [ “$CHANGES_EXIST” -eq 0 ]; then
exit 0
fi
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The whole if block is saying
# in plain english: if there are no changes (CHANGES_EXIST = 0)
# then exit with no error code `exit 0` if there are changes,
# then continue on with the script
git add .; git commit -q -m “$(date +”%Y-%m-%d %H:%M:%S”)”; git push -q
# git add. = add all current changes in the repo no matter the
# level of nested folders/files
# git commit -q -m: this saus we are committing changes to
# our repo, -q says BE QUIET no output prints to terminal
# if ran manually, -m defines a message for the commit log
## the -m message is “$(date +”%Y-%m-%d %H:%M:%S”)” this
# runs the command date with the formatting arguments for a
# date in YYYY-MM-DD HH-MM-SS format as your commit message
# git push -q: push the changes to github and BE QUIET about it
# The semicolons between commands are just saying run each
# command and then run the subsequent command, they’re just
# separators
So now that we have the script ready we enter CRON!
- Set up cron job (More on cron: https://ostechnix.com/a-beginners-guide-to-cron-jobs/)
on linux i used cronie, mac comes with cron installed already, to get into your cron jobs:
crontab -e
# ^^^^^^^^ -e: edit your crontab file i.e. your list of cronjobs
my cronjob looks like this:
*/30 * * * * /Users/bryanjenks/.local/bin/zk_sync >/dev/null 2>&1
*/30 * * * *
the first section is the timers
/Users/bryanjenks/.local/bin/zk_sync
the second is what file is it executing (absolute path)
and then to make sure its quiet its sending any script output (shouldnt be any we used -q
a lot) to >/dev/null 2>&1
so now any changes i make to anything in my vault is pushed to github every 30 minutes with an ISO time stamp for the files with changes.
i keep the repo private, and this way i always have my work available where ever i go and with git i have version control. plus if there are no changes, the command doesnt do anything.
hope someone else enjoys the workflow