Skip to content

Instantly share code, notes, and snippets.

@JCGoran
Created May 4, 2020 17:09
Show Gist options
  • Save JCGoran/da1308892ca323ab289a43b8fb278acf to your computer and use it in GitHub Desktop.
Save JCGoran/da1308892ca323ab289a43b8fb278acf to your computer and use it in GitHub Desktop.
Backing up edited Joplin resources

This is a small shell script which can be used to backup edited Joplin resources, since this is, as of time of writing, still an open issue. Should work on any GNU/Linux or MacOS machine since it just uses POSIX utils.

Note that you should manually set the env variables JOPLIN_CURRENT_DIR (where Joplin currently saves everything) and JOPLIN_BACKUP_DIR (where the backup directory is); usually the former is ${HOME}/.config/joplin-desktop (the default value), and the latter is determined by the user (default value is ${HOME}/.joplin-backup).

Usage:

sh editable-resources.sh

If you don't want to be prompted whether the directory info is correct (which you should check!), you can just do:

yes | sh editable-resources.sh

This can of course be coupled with something like cron to make backups on a regular basis; for instance, to backup every hour at 00 minutes, you can do something like this:

(crontab -l ; echo "0 * * * * sh /path/to/editable-resources.sh")| crontab -
#!/usr/bin/env sh
# abort on error
set -e
# path to resources of current Joplin session
JOPLIN_CURRENT_DIR="${JOPLIN_CURRENT_DIR:-${HOME}/.config/joplin-desktop}"
# path to resources of Joplin backup
JOPLIN_BACKUP_DIR="${JOPLIN_BACKUP_DIR:-${HOME}/.joplin-backup}"
# append resources subdir name to current and backup dir
JOPLIN_CURRENT_DIR="${JOPLIN_CURRENT_DIR}/resources"
JOPLIN_BACKUP_DIR="${JOPLIN_BACKUP_DIR}/.resource"
# check the current Joplin directory exists
if [ ! -d "${JOPLIN_CURRENT_DIR}" ]
then
printf "ERROR: current Joplin dir '%s' does not exist!\n" "${JOPLIN_CURRENT_DIR}"
exit 1
fi
# check the backup Joplin directory exits
if [ ! -d "${JOPLIN_BACKUP_DIR}" ]
then
printf "ERROR: backup Joplin dir '%s' does not exist!\n" "${JOPLIN_BACKUP_DIR}"
exit 2
fi
# make sure it's correct by asking the user
printf "Current Joplin resource directory is: %s\n" "${JOPLIN_CURRENT_DIR}"
printf "Backup Joplin resource directory is: %s\n" "${JOPLIN_BACKUP_DIR}"
printf "Do you wish to proceed with backup?\n"
read -r prompt
if [ "${prompt}" = "y" ] || [ "${prompt}" = "Y" ]
then
for entry in "${JOPLIN_CURRENT_DIR}"/*
do
# get the name of a file
filename="$(basename "${entry}")"
# get file with stripped name (POSIX style)
stripped_filename="${filename%.*}"
# check if backup dir contains the current entry
if [ -e "${entry}" ]
then
# check if there are differences between the files
if ! diff "${entry}" "${JOPLIN_BACKUP_DIR}/${stripped_filename}"
then
# if there are, move the file to backup
cp -a "${entry}" "${JOPLIN_BACKUP_DIR}/${stripped_filename}"
fi
# otherwise, copy the file to backup
else
cp -a "${entry}" "${JOPLIN_BACKUP_DIR}/${stripped_filename}"
fi
done
else
printf "Aborting...\n"
fi
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment