Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active February 4, 2022 14:23

Revisions

  1. Utopiah renamed this gist Dec 6, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Utopiah created this gist Dec 6, 2021.
    51 changes: 51 additions & 0 deletions syncNC
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # script to (one-way) synchronize a NextCloud directory to a reMarkable device
    # use it manually as ssh remarkable /home/root/bin/syncNC or via Cron or systemd
    #
    # configure https://nextcloud.domain.tld/remote.php/dav/files/username/ with your own NextCloud Dav URL
    # add your username, password and path to /opt/etc/davfs2/secrets
    # install mount.davfs via opkg install davfs2
    # make the required directories e.g ~/nextcloudSync2 /media/nextcloudselfhosted/ on the reMarkable
    # user reMarkable2_sync/ on your NextCould or change its location here
    # do a symlink between ~/xochitl-data/ and ~/.local/share/remarkable/xochitl/

    # helpers adapted from https://gist.github.com/Utopiah/e2d5c944bbd632e3ae0530e602977f45
    addPdfWithMetadata(){
    cp "$1" ~/xochitl-data && echo "{'parent':'','type':'DocumentType','visibleName':'$1'}" | sed s/\'/\"/g > ~/xochitl-data/`echo $1 | sed "s/.pdf//"`.metadata;
    echo '{ "fileType": "pdf" }' > ~/xochitl-data/`echo $1 | sed "s/.pdf//"`.content;
    echo "$1" >> /tmp/newfilesaddedduringsync
    }
    addEpubWithMetadata(){
    cp "$1" ~/xochitl-data && echo "{'parent':'','type':'DocumentType','visibleName':'$1'}" | sed s/\'/\"/g > ~/xochitl-data/`echo $1 | sed "s/.epub//"`.metadata;
    echo '{ "fileType": "epub" }' > ~/xochitl-data/`echo $1 | sed "s/.epub//"`.content;
    echo "$1" >> /tmp/newfilesaddedduringsync
    }
    addWithMetadataIfNew(){
    if [ -f ~/xochitl-data/"$1" ];
    then
    echo "File already added, skipping"
    else
    echo "File is new, adding"
    echo "$1" | grep .epub && addEpubWithMetadata "$1"
    echo "$1" | grep .pdf && addPdfWithMetadata "$1"
    fi
    }
    syncFromNextcloudSelfHosted(){
    /opt/sbin/mount.davfs https://nextcloud.domain.tld/remote.php/dav/files/username/ /media/nextcloudselfhosted/
    rsync /media/nextcloudselfhosted/reMarkable2_sync/ ~/nextcloudSync2/ -r
    # note that this is tricky for file modification, might add -c or -a but requires the endpoint to show the update
    pushd $PWD
    cd ~/nextcloudSync2/
    rm -f /tmp/newfilesaddedduringsync
    for F in *pdf *epub; do
    addWithMetadataIfNew "$F";
    done
    popd
    if [ -f ~/nextcloudSync2/configuration.txt ]; then
    if [ -f /tmp/newfilesaddedduringsync ]; then
    grep "force restart" ~/nextcloudSync2/configuration.txt && systemctl restart xochitl
    # should have something like force restart xochtil in it, otherwise, do so manually
    fi
    fi
    }

    syncFromNextcloudSelfHosted