Before long-running commands, open a screen [1] session. That way, if you get disconnected, the job keeps running, and you can reconnect to the screen session afterwards.
The man page is quite long, use "screen -R <appropriate name" to create the session or reconnect to it, and (IIRC) CTRL-A D to exit screen without closing the session.
It was my first time doing it. I didn’t know it’d be such a frustrating experience.
Nowadays screen is considered legacy and it is recommended to use tmux, but the idea is the same.