运行 2 rsync 命令并将输出打印到日志文件

run 2 rsync commands and print the output to a log file

我是脚本新手,想了解如何根据布尔逻辑打印出变量。

#!/bin/bash

# set variables
WEBAPPS_YES="Successfully synced webapps folder"
WEBAPPS_NO="Could not sync webapps folder"
RSYNC_YES="Successfully synced rsync log file"
RSYNC_NO="Could not sync rsync log file"

# Command to rsync 'webapps' folder and write to a log file
rsync -azvh ~/webapps -e ssh user@something.com:/home/directories >> /path/to/rsync.log 2>&1

# Command to rsync 'rsync.log' to a log file on backup server 'Larry'
rsync -azvh --delete ~/rsync.log -e ssh user@something.com:/path/to/logs

if [ $? -eq 0 ]
 then
  echo 
  exit 0
else
  echo  >&2
  exit 1
fi

我希望整个 ifthenelse 部分在 echo 中打印出来,无论这两个部分是否成功。我知道我需要某种逻辑语句,但无法理解。

您可以在运行每个rsync命令后查看结果,并在之后显示结果。我认为这会起作用:

# Command to rsync 'webapps' folder and write to a log file
rsync -azvh ~/webapps -e ssh user@something.com:/home/directories >> /path/to/rsync.log 2>&1
RESULT1="$?"

# Command to rsync 'rsync.log' to a log file on backup server 'Larry'
rsync -azvh --delete ~/rsync.log -e ssh user@something.com:/path/to/logs
RESULT2="$?"

if [ "$RESULT1" != "0" ]; then
    echo "$WEBAPPS_NO"
else
    echo "$WEBAPPS_YES"
fi

if [ "$RESULT2" != "0" ]; then
    echo "$RSYNC_NO"
else
    echo "$RSYNC_YES"
fi