Since I’ve moved my website to a new hosting, I’ve begun to use rsync instead of ftp to maintain it. There are a couple of major gains in doing so:
- it’s more secure: rsync is tunnelled through an SSH connection. My previous hoster only supported plain FTP, meaning that everything was in cleartext, including username and password.
- it’s waaay more comfortable: I can do any change I wish on my local copy of the site and when everything is ready I can just launch a mini-script to go live. I don’t have to worry about forgetting something anymore 😉
So how do I do it?
I’ve created a simple sync_my_site.sh script like this:
rsync -rltuO \n --progress \n --delay-updates \n --delete --delete-after \n -e 'ssh -i /home/wiz/.ssh/my_hosting_id' \n --exclude-from=/www/site/.rsyncignore \n /www/site/ user@host:/home/user/site/
ssh -i /home/wiz/.ssh/my_hosting_id user@host 'chmod -R o+r site'
The first parameters (rltuO) tell rsync to recursively update the site, preserving file times and symbolic links and skipping files newer than the local copy.
–delay-updates is useful to replace all the files when the upload is complete, to minimize the probabilities of breaking the site while some users are browsing on it 😉
–delete tells rsync to remove files on the remote host that are not present on your local system
-e is needed in my case to specify the identity I wish to use for the SSH connection.
–exclude-from points to a text file which contains a list of files that rsync should not consider. It’s useful to avoid it from uploading your local configuration files overwriting the production values 😉
Finally, after rsync completes its job, I launch another command on the remote server to fix the file permissions so that the webserver can read all the files. If your production and development servers have the same configuration, you can also skip that line and add -p on rsync’s command line, so that it will preserve the file permissions as well.