Diego
Diego

Reputation: 225

How to execute bash script after rsync

When I deploy on my dev server I use rsync. But after rsync I need to execute a .sh file for "after" deploy operations like clear cache...

Usually I do this via SSH, but if I deploy very often it's boring write:

There is a way to do this quickly? This is my rsync.sh:

#!/bin/bash
host=""
directory="/var/www/myapp/web"
password=""
usage(){
        echo "Cant do rsync";
        echo "Using:";
        echo "   $0 direct";
        echo "Or:";
        echo "   $0 dry";
}
echo "Host: $host";
echo "Directory: $directory"
if [ $# -eq 1 ]; then
        if [ "$1" == "dry" ]; then
                echo "DRY-RUN mode";
                rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory --dry-run
        elif [ "$1" == "direct" ]; then
                echo "Normal mode";
                rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory
        else
                usage;
        fi;
else
        usage;
fi

Upvotes: 4

Views: 13252

Answers (2)

TommyPeanuts
TommyPeanuts

Reputation: 146

If instead of using rsync over SSH, you can run an rsync daemon on the server. This allows you to use the pre-xfer exec and post-xfer exec options in /etc/rsyncd.conf to specify a command to be run before and/or after the transfer.

For example, in /etc/rsyncd.conf:

[myTransfers]
     path = /path/to/stuff
     auth users = username
     secrets file = /path/to/rsync.secrets
     pre-xfer exec = /usr/local/bin/someScript.sh
     post-xfer exec = /usr/local/bin/someOtherscript.sh

You can then do the transfer from the client machine, and the relevant scripts will be run automatically on the server, for example:

rsync -av . username@hostname::myTransfers/ 

This approach may also be useful because environment variables relating to the transfer on the server can also be used by the scripts.

See https://linux.die.net/man/5/rsyncd.conf for more information.

Upvotes: 5

paul
paul

Reputation: 1242

You can add a command after the rsync command to execute it instead of starting a shell.

Add the following after the rsync command :

sshpass -p "$password" ssh $host "cd $dir && ./after_deploy.sh"

Upvotes: 3

Related Questions