Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active February 4, 2022 14:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Utopiah/d26e178be3840d0d68cead166eb45a18 to your computer and use it in GitHub Desktop.
Save Utopiah/d26e178be3840d0d68cead166eb45a18 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment