Deploying with rsync, setting desired file and directory permissions

I’m currently building a new site using a minimal flat file markdown-embracing CMS called Pico. Once nice thing about flat file systems, of course, is easy deployment. Just grab all your files, push them to any host and you’re good to go.

I’m using Git only for keeping track of the codebase and don’t want to add content files to the repository.
However, it would be tedious to manually keep track of the files added, changed and removed, so I decided to let rsync take care of that.

I found this nice shell script on Codewall


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

All you have to do is put that into your project’s root, make it executable and change the username, server and path to suite your environment. You can optionally provide a list of files and directories in a file called rsync_exclude.txt. Here’s how my exclude list looks like currently:


When I used this script to deploy from my local development (Mac) to the stage (Linux) environment I was a bit disappointed. All newly transferred files lacked proper permissions, resulting in 403 Permission Denied errors when trying to access them on the web. After trying various different rsync modes I gave up and added the desired chmod command as a parameter to the rsync command: --chmod=Du=rwx,Dg=rx,Do=rx,Fu=rw,Fg=r,Fo=r

I found this on StackOverflow.

Leave a Reply