Automating Bear notes backup

Jun 5, 2021

All note apps suck in some way or another. Apple Notes is too basic, Notion is too slow, Obsidian is too intimidating. For me, Bear currently strikes a nice balance between usability (well designed apps) and openness (first-class Markdown support, easy export). Bear is not perfect; it’s Apple-only, and the editor lacks some Markdown features, but I use it daily and like it a lot (and they’re apparently working on both issues).

As my collection of notes grows, it should naturally become part of my backup process, which consists of a homegrown shell script that backs up a number of things, including databases, cloud storage and personal files. While Bear has a handy backup/restore feature in both the macOS and iOS app, it has no CLI I can use from my script. The only supported way to make a backup is through its GUI.

Sure, you could easily make a copy of the underlying SQLite database, but there’s no guarantee Bear can open it a year down the line after updates have migrated the database schema, and it’s pretty unintuitive to work with directly should Bear no longer be around. Bear’s own backups, meanwhile, are just zips of TextBundle files, which makes recovery easy both in- and outside of Bear.

So here’s an AppleScript for macOS that automates the clicks and keystrokes needed to back up notes through Bear. It can be run directly from a terminal or shell script and waits around for the backup process to complete, so the file will be ready when the script exits. The whole thing does rely on some pretty shaky assumptions about the Bear interface, but GUI automation is, as always, notoriously brittle.

Important: Make sure the backup directory exists. It will not be created for you.