Skip to content

Instantly share code, notes, and snippets.

@mgsloan
Created June 22, 2020 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgsloan/2f67ddc3e6061ea8546e48f62120fee7 to your computer and use it in GitHub Desktop.
Save mgsloan/2f67ddc3e6061ea8546e48f62120fee7 to your computer and use it in GitHub Desktop.
Roam backup shell script
#!/bin/sh -e
# Script I wrote before I realized
# https://github.com/MatthieuBizien/roam-to-git exists
if [ "$#" -eq 1 ]; then
LAST_ROAM_EXPORT=$1
elif [ "$#" -eq 0 ]; then
echo "Attempting to use XDG info to find roam export."
# Load XDG dir configuration into variables
DOWNLOAD_DIR=$(xdg-user-dir DOWNLOAD)
# Finds the last modified Roam-Export-*.zip file in downloads.
LAST_ROAM_EXPORT=$(find $DOWNLOAD_DIR -type f -name Roam-Export-*.zip -printf "%T@\t%p\n" | sort -n | cut -f 2- | tail -1)
else
echo "Expected only one argument indicating the Roam-Export-*.zip to extract"
exit 1
fi
echo "Extracting $LAST_ROAM_EXPORT"
unzip -u $LAST_ROAM_EXPORT
ZIP_INFO=$(zipinfo -1 $LAST_ROAM_EXPORT)
ZIP_INFO_LINE_COUNT=$(echo $ZIP_INFO | wc -l)
if [ $ZIP_INFO_LINE_COUNT != '1' ]; then
echo "Only one file expected in $LAST_ROAM_EXPORT. Instead got:\n$ZIP_INFO"
exit 1
fi
JSON_FILE=$(echo $ZIP_INFO | tail -1)
PRETTY_JSON_FILE="pretty-$JSON_FILE"
echo "Storing pretty-printed version of $JSON_FILE in $PRETTY_JSON_FILE"
$(cat $JSON_FILE | python -m json.tool > $PRETTY_JSON_FILE)
echo "Deleting $JSON_FILE"
rm $JSON_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment