Very nice written script. Thanks for sharing, Phil.
I adapted one for serverpilot users, shared it on github here:
https://gist.github.com/liuyigh/34868d44c248d87887c145949e5c164d