Simple automated backup for markdown notes

, 4 min read

Read first I assume you are using Linux or Mac. You can probably easily modify this to work for Windows, or ask ChatGPT.

Open your terminal and create a file named backup-notes.sh in ~/scripts directory:

touch ~/scripts/backup-notes.sh

Open the file using nano ~/scripts/backup-notes.sh (or any other text editor) and add the following:

#!/bin/bash

# Path to your notes
SOURCE_DIR="$HOME/Documents/notes/"
# Path to save backups
DEST_DIR="$HOME/Nextcloud/Backups/note-backups"

# The actual filename to use for current backup e.g. notes-2023-07-03.tar.gz
BACKUP_FILE="${DEST_DIR}/notes-$(date +'%Y-%m-%d').tar.gz"

# Compress all the files in SOURCE_DIR and save them to BACKUP_FILE
tar -czf "${BACKUP_FILE}" "${SOURCE_DIR}"

echo "Finished running $0."
echo "If all went smoothly, backup should be at ${BACKUP_FILE}"

Make the file executable:

chmod +x ~/scripts/backup-notes.sh

Run the script to make sure that it works:

. ~/scripts/backup-notes.sh

Check that the backup file is created properly. If everything worked fine, it is now time to schedule the script run automatically. Open cron in your terminal by running:

crontab -e

And add this line to the crontab file:

0 0 * * 1 $HOME/scripts/backup-notes.sh

Now your notes will be automatically backed up every Monday at 00:00 (assuming your computer/laptop is powered on, standby is also fine).

If you have any sort of cloud mounted on your device1 and you use that as DEST_DIR, your backups will automatically be stored at your cloud and will be available to all your devices.

Script available also as a Github gist.


Intended audience: People with limited to medium experience with terminal who take notes in the style of Zettelkasten, Evergreen notes, PKM, Personal knowledge management, Obsidian, Roam Research, Foam, Logseq, etc. and want a trivially easy way of having their work automatically backed up.

For the curious:

  1. What is #!/bin/bash? See Stack Overflow
  2. What is chmod +x? See my earlier post, chmod explained in 5 minutes.
  3. What is tar -czf?
    • c: create a new archive.
    • z: use gzip for compression (widely supported format).
    • f: filename to save the archive.
  4. What is cron? A simple utility for defining periodic jobs, see Wikipedia. Schedule (0 0 * * 1) explanation: https://crontab.guru
  5. The more technical people reading this will notice that I am using just tar for backing up, instead of rsync, borg etc. I prefer to have full backups rather than incremental/diff only, and since my notebase is still rather small (<50mb), I can afford it.
  1. If you noticed my DEST_DIR, I save my backups on Nextcloud

#tech-blog, #note-taking, #automation