Skip to main content

Deploy with rsync

Jose CerrejonAbout 1 minDeveloperDeveloper

Deploy with rsync


Deploy for developers means that we upload the changes we have on our development server or local, to production (your hosting provider usually).

There are many ways to do this: ftp, scp, Git, Fabric, Envoy, rsync, etc. Today I want to show you the script I use on every web project to upload changes, based on rsync. For me it's the easier and safer method.


What I do is create two files in each project: and .rsignore. The first one I invoke every time I want to upload the changes to the production server, and the second one is where I add each file or directory I want to ignore when deploy.

# Files to ignore using rsync

### .rsignore example file

The next example file is the script I use with my Raspberry Pi when I want to share a website with a client in a particular way to show my progress. I've tested in macOS, but should work with any Linux distribution.

#! /usr/bin/env bash
# Description : Copy dir with rsync and exclude files-folders inside .rsignore in your project
# Author      : Jose Cerrejon Gonzalez (ulysess@gmail_dot._com)
# * Run on Desktop:
#       ssh-keygen -b 2048 -t rsa -f id_pi -P ""
#       ssh pi@ < ~/.ssh/ 'mkdir -p .ssh && cat >> .ssh/authorized_keys'
# * Edit on Desktop: ~/.ssh/config and add your host:
#       Host myrpi
#           HostName
#           Port 22
#           User pi
#           IdentitiesOnly yes
#           IdentityFile ~/.ssh/id_pi
# * Copy this file in your project dir

if [[ -e $RSIGNORE ]]; then
    PARAM_EXCLUDE=" --exclude-from $RSIGNORE"

echo -e "Listing files to add/update...\n"
rsOutput=$(rsync $PARAM_EXCLUDE -zcrSLh --dry-run --out-format=" * %n%L [%M]" ./ $SSH_HOST:$DESTINATION)

if [[ $rsOutput = "" ]]; then
    echo "Nothing to update."; exit

echo "$rsOutput" ; echo
read -p "Are you sure you want to update? [y/n] " option
case "$option" in
    y*) rsync $PARAM_EXCLUDE -vzcrSLh ./ $SSH_HOST:$DESTINATION;;

What method do you use when deploy?. See you in the comments.

Link: > rsyncopen in new window