Save directory data to a local data folder ; see differences ; restore data.
Originally a tool to save Joplin data in case the sync broke/was disastrously told to empty itself.
Can be generalized to a data sync tool.
"Compile" with bash-builder
#!/usr/bin/env bash | |
set -euo pipefail | |
### Sync Target Folder to Local Folder Usage:help | |
# | |
# Put a directory's data into a local ./data directory - for example, in a git repo, or a backup folder | |
# | |
# sync-in.sh | |
# | |
# Actions: | |
# | |
# backup TARGETDIR | |
# take a backup of the target directory | |
# | |
# restore TARGETDIR | |
# restore the data to the target directory | |
# | |
# diff TARGETDIR | |
# show what changes are found in live target directory that are not in the local store | |
# | |
###/doc | |
#%include std/out.sh | |
#%include std/abspath.sh | |
#%include std/syntax-extensions.sh | |
#%include std/askuser.sh | |
#%include std/autohelp.sh | |
$%function joplin-sync(action) { | |
cd "$(dirname "$0")" | |
case "$action" in | |
backup) | |
backup-joplin-data "$@" | |
;; | |
restore) | |
restore-joplin-data "$@" | |
;; | |
diff) | |
joplin-diff "$@" | |
;; | |
esac | |
} | |
$%function set-joplin-abspath(*p_joplindir) { | |
p_joplindir="$(abspath:path "$p_joplindir")" | |
[[ -d "$p_joplindir" ]] || out:fail "$p_joplindir is not a directory" | |
} | |
$%function backup-joplin-data(joplindir) { | |
out:info "Pulling commits from git remote" | |
git pull | |
set-joplin-abspath joplindir | |
out:info "Pulling data from [$joplindir]" | |
rsync -av --delete "$joplindir/" ./data/ | |
check-git-changes || { | |
out:info "Nothing to add." | |
exit 0 | |
} | |
git status | |
} | |
prompt-quit-joplin() { | |
while ps aux | grep -v grep |grep -iq joplin; do | |
read -p "It looks like Joplin is still running - please quit it first and try again." | |
done | |
} | |
$%function restore-joplin-data(joplindir) { | |
set-joplin-abspath joplindir | |
if askuser:confirm "View restore diff ?"; then | |
diff --color=always -u -r "$joplindir/" ./data/ | less -R | |
fi | |
if askuser:confirm "Apply ? This will also remove the joplin-desktop/database.sqlite file."; then | |
prompt-quit-joplin | |
out:info "Removing Joplin database file" | |
rm ~/.config/joplin-desktop/database.sqlite | |
out:info "Synchronizing data" | |
rsync -av --delete ./data/ "$joplindir/" | |
fi | |
} | |
check-git-changes() { | |
if git status | grep -qiE "working (tree|directory) clean"; then | |
out:info "Nothing to commit." | |
return 1 | |
fi | |
return 0 | |
} | |
$%function joplin-diff(joplindir) { | |
set-joplin-abspath joplindir | |
diff --color=always -u -r ./data/ "$joplindir/" | less -R | |
} | |
autohelp:check-or-null "$@" | |
joplin-sync "$@" |