Updates from Ricky Lindén

Updating a website using rsync

[linkstandalone]

Originally I got this from Luke (like so many other things), but it's so useful, and I've made some changes to it too, that I make the instructions available through my site as well.

(Thanks to codewizard1975@discuss.getsol.us btw for helping me realize the need for my server user to be part of www-data group.)

How I manage this website

I edit raw html/css locally on laptop. When I'm done I fire up a terminal and write:

syncr

And that's it. The changes are online. Any files that were removed locally will be removed remotely. It's an exact copy of what I did.

Something I recently added is also the possibility to ignore files/folders. Since my local website folder is a git repo I don't want to upload that. But additionally, if I'm working on something that's not done yet I can still push the other changes online because there's an ignore option.

If you know some stuff already, you can probably figure out from looking at the script (or it's more like a command honestly). You find it on my sourcehut, the scripts-repo.

Prerequisites

You need a Linux server, with a user which authenticates using public key authentication and this user needs to be part of the www-data group (or whichever group that owns the website folder, but most webservers uses www-data). If you don't mind logging in with root directly, that works too, but it's better security wise in general to disable that option. So in order to write to /var/www/YOUR-WEBSITE-FOLDER (since that folder is owned by www-data), your user needs to be part of that group.

rsync

So, without further delays, this is the command in question:

rsync -avhP -e 'ssh -p 22' --exclude={.git,.gitignore,README.md,LICENSE.md} \
--delete ~/web/YOUR-WEBSITE-FOLDER/ YOUR-USER@YOUR-WEBSITES-IP:/var/www/YOUR-WEBSITE-FOLDER


To use this, just save it as a bash script, if you don't want to make that many modificiations, you basically need (if the prerequisites are in place) to change:

  • YOUR-WEBSITE-FOLDER - The name of the local and remote website folder.
  • YOUR-USER - The user on the remote server you are logging in as.
  • YOUR-WEBSITES-IP - The ip/domain name of the server.
  • Optionally you might have different folders than in my example you'd like to exclude.

    If you've never used rsync before and you'd like the command a bit more broken down, here you go:

    The first flags -avhP stands for:

  • -a --archive, a bunch of good defaults.
  • -v --verbose, let us see what's going on.
  • -h --human-readable, easier to understand numbers.
  • -P --progress, see the progress of the upload.
  • I added the ssh command with the port option, in case you have ssh listening on a different port or you'd like to specify a certain private key etc.

    --exclude is very useful, since this is files and folders we do not want to upload to the remote servers. Like a git repo or parts of the websites you are still working on.

    --delete means that we want to delete files/folders if they get deleted on the local laptop. I.e. if I used to have a contact.html page, but I merged this one with index.html, when I run syncr it will delete contact.html on remote server as well since it doesn't exist anymore on my local copy.

    Also note rsync is very sensitive when it comes to slash. So if you have issues that it creates something like /var/www/YOUR-WEBSITE-FOLDER/YOUR-WEBSITE-FOLDER or similar, then it's probably that you got an extra slash.

    You can run rsync with --dry-run to see that it's doing what it's supposed do. If all is good, run it and enjoy the ease of use of managing your website. I'm done writing this so I know I will.