Cron Backups to Github

Between my journal and workflow management systems I write a lot of notes.

I personally feel very uncomfortable entrusting all of my most personal thoughts and ideas to a third party like Evernote that has not made clear commitments to user privacy.

Instead I use Joplin, a free open source note taking frontend. Plug in a backed (Nextcloud or Webdav will do the trick. I use the latter.) and you’re good to go.

Because my webdav server just stores everything on my server’s filesystem, I needed a way to backup the files and store them remotely.

Git + Github was an easy solution, so I tossed together an hourly cron script to commit any changes to my files and push them up to a remote repository.

Specifically, I just created the following script at /home/meierj/bin/backup-journal:

#!/usr/bin/env bash

GIT_DIR='<webdav-git-directory, e.g. /home/var/www/journal>'

git -C $GIT_DIR add --all
git -C $GIT_DIR commit -m 'Backup' --allow-empty

git -C $GIT_DIR push origin >/home/meierj/tmp_error 2>&1
echo $status >> /home/meierj/tmp_log

Then simply add a cron entry with:

$ crontab -e

and add an entry to run it hourly:

0 */1 * * * /home/meierj/bin/backup-journal

You can find a great site for tweaking cron schedules here

If you connect to github over HTTPS you should be good to go from here.

If, however, like me you use an SSH key, read on.

I normally use ssh-agent to access my keys when trying to initiate a new session on a remote server.

ssh-agent allows you to add a key and input your password once. The key is then stored in the daemon’s memory and accessed whenever attempting a new ssh login.

However, cron cannot talk to ssh-agent. Instead of referencing the key via the daemon, we need to list it in ~/.ssh/config and associate it to the git user and github domain.

You can do this like so:

    User git
    IdentityFile ~/.ssh/<your-git-private-key>

Now the cron script’s ssh command will know where to look for the git private key and things should run without a hitch.

Happy backing up!

PS: if you would like to read more about the distinction between ssh and ssh-agent, check out the excellent wikipedia entry.