获取输出的 Rsync 状态

获取输出的 Rsync 状态

在 rsync 作业成功完成后,我得到了典型的结果:

sent 6022622 bytes  received 4751961 bytes  69738.40 bytes/sec
total size is 3328464484  speedup is 308.92

我通过 shell 脚本调用 rsync 作业,并在脚本末尾将其写入文本文件作为日志。如果不将整个 rsync 作业记录到文本文件中,有没有办法只捕获此消息是否在末尾输出,以便我可以在输出中写入类似“OK”的内容来表示同步已成功运行?

谢谢你!

答案1

正如评论中指出的那样,最好的方法是检查 rsync 的返回码。以下是您可能想要使用的一些 bash 代码,用于将返回码转换为有意义的字符串:

rsync -a -v -h -i --stats --dry-run -A -H $source $target

case $? in
  0)
    echo "\e[32mSuccess"
    ;;
  1)
    echo "Syntax or usage error"
    ;;
  2)
    echo "Protocol incompatibility"
    ;;
  3)
    echo "Errors selecting input/output files, dirs"
    ;;
  4)
    echo "Requested action not supported: an attempt was made to manipulate 64-bit files on a platform that cannot support them; or an option was specified that is supported by the client and not by the server."
    ;;
  5)
    echo "Error starting client-server protocol"
    ;;
  6)
    echo "Daemon unable to append to log-file"
    ;;
  10)
    echo "Error in socket I/O"
    ;;
  11)
    echo "Error in file I/O"
    ;;
  12)
    echo "Error in rsync protocol data stream"
    ;;
  13)
    echo "Errors with program diagnostics"
    ;;
  14)
    echo "Error in IPC code"
    ;;
  20)
    echo "Received SIGUSR1 or SIGINT"
    ;;
  21)
    echo "Some error returned by waitpid()"
    ;;
  22)
    echo "Error allocating core memory buffers"
    ;;
  23)
    echo "Partial transfer due to error"
    ;;
  24)
    echo "Partial transfer due to vanished source files"
    ;;
  25)
    echo "The --max-delete limit stopped deletions"
    ;;
  30)
    echo "Timeout in data send/receive"
    ;;
  35)
    echo "Timeout waiting for daemon connection"
    ;;
  *)
    echo "Unknown return code $? from rsync"
esac

相关内容