Last Updated: February 23, 2020
·
18.48K
· jaymabazza

Using Rsync to deploy a website. Easy one liner command

Updated August 2014

Below is a basic script to deploy files on a target server and directory. in a nutshell, rsync eliminates the need to manually copy modified files onto a server. It works even better if you have your key installed on your target server. You can deploy all your modified files in a single command.

Requirement: Rsync installed on your local and on your target server.

Create a "deploy" file on your root directory. Make sure it's executable.

$> touch deploy
$> chmod +x deploy

Now, paste in the following script

#!/bin/bash

$ERRORSTRING="Error. Please make sure you've indicated correct parameters"
if [ $# -eq 0 ]
    then
        echo $ERRORSTRING;
elif [ $1 == "live" ]
    then
        if [[ -z $2 ]]
            then
                echo "Running dry-run"
                rsync --dry-run -az --force --delete --progress --exclude-from=rsync_exclude.txt -e "ssh -p22" ./ username@server:/var/www/website-name
        elif [ $2 == "go" ]
            then
                echo "Running actual deploy"
                rsync -az --force --delete --progress --exclude-from=rsync_exclude.txt -e "ssh -p22" ./ username@server:/var/www/website-name
        else
            echo $ERRORSTRING;
        fi
fi

Now, optionally create a file called rsync_exclude.txt beside the deploy script. Put your ignore directories and files like so

uploads/
logs/
cache/
nbproject/
.svn
.DS_Store

To run the script, just fire up your terminal and execute the script. It takes 2 parameters. If you want a dry run to see which files are to be pushed, run this:

$> ./deploy live

If you're happy with the dry run output, append go. This will do the actual push.

$> ./deploy live go

Deploy in Action (Running a dry run)
Picture

Deploy in Action (Actual Deploy)
Picture

The script accepts multiple server setup, just modify it according to your needs. I recommend using a 2-step sync process. One as Staging site and another for Production.

$> ./deploy dev
$> ./deploy dev go
$> ./deploy live
$> ./deploy live go

Updated August 2014

3 Responses
Add your response

Thanks a lot.
This really helped refine my workflow for deploying all my flat file CMS websites.

over 1 year ago ·

The script has some errors, described in this thread:
http://stackoverflow.com/questions/23065722/error-command-not-found-bash-script

You may want to fix those.

over 1 year ago ·

Thanks for pointing that out! I have updated the script above :)

over 1 year ago ·