note to self: Automatically sending NTS notes to a server

Server Setup

What I'm going to send is basically a couple HTML files. So any file transfer method should work pretty well. I recently heard about something called sshfs and that looked interesting, so let's try it out.

$ mkdir ~/srv
$ sshfs -o allow_other "$HOME/srv"

Two commands later, I was looking at my server's root from my own filesystem. That's kinda neat!

Now let's try exporting to somewhere in the server.

$ mkdir ~/srv/home/admicos/notes
$ nts -w ~/srv/home/admicos/notes
Loading template /home/admicos/.local/share/nts/export/template.html
Saving index
Saving automatically-sending-nts-notes-to-a-server
$ ls  ~/srv/home/admicos/notes
automatically-sending-nts-notes-to-a-server.html  index.html

This is turning out to be easier than I expected. Now that the notes are on the server, we just need to move them to the appropriate location so nginx can pick them up.

Now, we can do this by hand, but we can also automate it to make our life a little easier. This is where we need to write a couple lines of bash.

Server Automation

Just searching bash detect when folder updates gives us exactly what we need:

inotifywait -m "$HOME/notes" -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"

Now let's run it... And it didn't work. Why? We're not checking for modifications. Just adding a -e modify would work, but I won't need that for my script.

So, if we empty the folder, and then try again, it works!

$ ./
Setting up watches.
Watches established.
The file 'index.html' appeared in directory '/home/admicos/notes/' via 'CREATE'
The file 'automatically-sending-nts-notes-to-a-server.html' appeared in directory '/home/admicos/notes/' via 'CREATE'

Now, all we need to do is move everything into where we need them to be.

rm "$HOME/notes/"*

inotifywait -m "$HOME/notes" -e create -e moved_to |
        while read path action file; do
                rm "/srv/html/$file"
                mv "$path/$file" "/srv/html/$file"

Let's test it out. I run the script, and put a file to that folder via sshfs and... it worked! (It would've been a great idea to put a print or something though, thought sshfs was bugged and not saving my test file before remembering that I move it basically instantly)

Now that we got everything working, let's make the script run in the background. I'm using an Ubuntu 18.04 server, and I really don't want to deal with systemd right now, so I just add it to my crontab

@reboot /home/admicos/

and run the script in the background.

$ ./ &

After exiting and re-entering the SSH session to confirm the script will stay in the background, I'm going to Test it again just to make sure.

(The script reacts so quickly, touch errors out with touch: setting times of '/home/admicos/srv/home/admicos/notes/test': No such file or directory)

Client Setup

Now that everything's working on the server, let's focus on the client.

First of all, the sshfs command I ran isn't permanent. Let's put it on our fstab to make the mount permanent. /home/admicos/srv fuse defaults,allow_other 0 0

And let's test it out:

$ fusermount -u srv
$ ls srv
$ sudo mount -a
The authenticity of host ' (' can't be established.
ECDSA key fingerprint is SHA256:c+Rk2I9VKIN2s+ivEQNz992fEFnytejgQdqs6OZC0uE.
Are you sure you want to continue connecting (yes/no)? yes
read: Connection reset by peer

And it didn't work. Searching the error led me to this AskUbuntu answer /home/admicos/srv fuse user,_netdev,reconnect,uid=1000,gid=1000,IdentityFile=/home/admicos/.ssh/id_rsa,idmap=user,allow_other   0 2

And trying with that...

$ sudo mount -a
$ ls ~/srv
bin/   boot/  dev/  etc/  home/  lib/  lib64/       lost+found/      media/    mnt/          opt/  proc/  root/  run/  sbin/
snap/  srv/   sys/  tmp/  usr/   var/  initrd.img@  initrd.img.old@  vmlinuz@  vmlinuz.old@

Bingo! Now it should automatically mount the server on boot.

The only thing left is to export my notes into the server user's ~/notes/ and the rest should be done.

Did it work?

UPDATE: For some reason, my zsh setup hangs on start whenever I mount the server root with sshfs. So, if you're going to do this, just mount your server user's home. You don't need the rest.