bash rsync后如何执行bash脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18185933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:12:59  来源:igfitidea点击:

How to execute bash script after rsync

bashunixrsync

提问by Diego

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...

当我在我的开发服务器上部署时,我使用 rsync。但是在 rsync 之后,我需要执行一个 .sh 文件以进行“之后”部署操作,例如清除缓存...

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

通常我通过 SSH 执行此操作,但如果我经常部署它很无聊写:

  • ssh ...
  • write password
  • cd /var/www/myapp/web
  • ./after_deploy.sh
  • ssh ...
  • 写密码
  • cd /var/www/myapp/web
  • ./after_deploy.sh

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

有没有办法快速做到这一点?这是我的 rsync.sh:

#!/bin/bash
host=""
directory="/var/www/myapp/web"
password=""
usage(){
        echo "Cant do rsync";
        echo "Using:";
        echo "   
sshpass -p "$password" ssh $host "cd $dir && ./after_deploy.sh"
direct"; echo "Or:"; echo "
[myTransfers]
     path = /path/to/stuff
     auth users = username
     secrets file = /path/to/rsync.secrets
     pre-xfer exec = /usr/local/bin/someScript.sh
     pre-xfer exec = /usr/local/bin/someOtherscript.sh
dry"; } echo "Host: $host"; echo "Directory: $directory" if [ $# -eq 1 ]; then if [ "" == "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 [ "" == "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

采纳答案by paul

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

您可以在命令之后添加一个命令rsync来执行它,而不是启动一个 shell。

Add the following after the rsync command :

在 rsync 命令后添加以下内容:

rsync -av . username@hostname::myTransfers/ 

回答by TommyPeanuts

If instead of using rsync over SSH you run the rsync daemon on the server, you can use the pre-xfer execand post-xfer execoptions in /etc/rsyncd.confto specify a command to be run before and/or after the transfer.

如果不是在 SSH 上使用 rsync,而是在服务器上运行 rsync 守护程序,则可以使用pre-xfer execpost-xfer exec选项/etc/rsyncd.conf来指定要在传输之前和/或之后运行的命令。

For example, in /etc/rsyncd.conf:

例如,在 /etc/rsyncd.conf 中:

##代码##

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

然后就可以从客户端进行传输了,相关的脚本会在服务器上自动运行,例如:

##代码##

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.conffor more information.

有关更多信息,请参阅https://linux.die.net/man/5/rsyncd.conf