towebhost: a Handy Script for Mirroring Multiple Websites
I maintain quite a few small websites. I have several of my own under different domains (shallowsky.com, nmbilltracker.com and so forth), plus a few smaller projects like flask apps running on a different port. In addition, I maintain websites for several organizations on a volunteer basis (because if you join any volunteer organization and they find out you're at all technical, that's the first job they want you to do).
I typically maintain a local copy of each website, so I can try out any change locally first. In /etc/apache2/sites-available/ I have entries for each virtual host, corresponding to entries in my localhost line in /etc/hosts:
127.0.0.1 localhost myhostname website1-local website2-local etc...
When I'm happy with a change, I use rsync (over ssh) to mirror files to the actual web server.
But it can be a hassle remembering hostnames, login names, and paths for the different servers. So a few years ago I wrote a shell function called towebhost that kept a list of servers, logins and paths, and the local directories they correspond to. So I can define:
webhosts=( user@mywebserver1.com:public_html mywebserver2.com:web ) weblocalpaths=( $HOME/mywebdir /public/myotherwebdir )
Then I can run towebhost ~/mywebdir/some/local/path
and towebhost figures out that ~/mywebdir/some/path
corresponds to the remote user@mywebserver1.com:public_html,
and runs the appropriate rsync command to mirror the local
~/mywebdir/some/path/ to
user@mywebserver1.com:public_html/some/path.
Not a big deal, but it saves a lot of typing, as well as trying to remember which username and web directory goes with which website.
Eventually I expanded it from a shell function in my .zshrc to a script of its own (caveat: it does use a few zsh-isms, so it won't work as a bash or sh script without a little rewriting). I realized that I never got around to publishing towebhost in my scripts repository, so I've just done that in case someone else might find it as handy as I do: towebhost.zsh on github.
[ 16:18 Feb 22, 2024 More linux | permalink to this entry | ]