Automating some file history with git snapshots
My Note taking tool is part journal, part shopping list, part blog posts, part programs. Most notes get written once. Some get revised, some get posted or implemented. The tool is fully self-hosted and written by me, so I get to decide on its (lack of) features. For simplicity I avoided keeping a full note history. But then I realized that maybe some kind of history is actually nice, for the case when a wonky network failure erases or corrupts some note.
My solution for this feature is to simply snapshot the directory where I keep my notes every hour into a git repository. This
gives me some change history, but it does not record every keystroke. The cron job is the following shell script:
SOURCE=/home/corion/bin/App-notes-htmx/notes_corion
TARGET=/home/corion/backup/notes_corion_history
if git --git-dir=$TARGET --work-tree $SOURCE diff --quiet; then
git --git-dir=$TARGET --work-tree $SOURCE add --all
git --git-dir=$TARGET --work-tree $SOURCE commit -m "Hourly"
fi
I've "implemented" the tool as a shell script, but if I add more features, I will likely move it to Perl and use Git::Raw
to do the adding/filtering/pruning myself. But then I would lose the convenience of .gitignore for example.
I still have on my list some kind of git history-prune, that coalesces adjacent git commits if they are too close, just
to eliminate changes that don't matter anymore. Maybe
after a month, compress all commits older than one month and less than 24 hours apart into a single commit.
I think having all changes of a single day in one commit is a sensible granularity for my use case.