Skip to content

Commit

Permalink
cleanup: use explicit tests instead of ...IfError
Browse files Browse the repository at this point in the history
The ...IfError functions have multiple problems, see the comments at
their definition.
  • Loading branch information
pcahyna committed Jun 21, 2021
1 parent a1925f0 commit e7efac5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
Expand Up @@ -37,8 +37,7 @@ local backup_prog_rc
# probably using the BACKUP=NETFS workflow instead
if [ -d "${opath}" ]; then
if [ ! -f "${opath}/selinux.autorelabel" ]; then
> "${opath}/selinux.autorelabel"
StopIfError "Failed to create selinux.autorelabel on ${opath}"
> "${opath}/selinux.autorelabel" || Error "Failed to create selinux.autorelabel on ${opath}"
fi
fi
;;
Expand Down
14 changes: 6 additions & 8 deletions usr/share/rear/output/RSYNC/default/200_make_prefix_dir.sh
Expand Up @@ -2,21 +2,19 @@
# RSYNC_PREFIX=$HOSTNAME as set in default.conf

# create temporary local work-spaces to collect files (we already make the remote backup dir with the correct mode!!)
mkdir -p $v -m0750 "${TMP_DIR}/rsync/${RSYNC_PREFIX}" >&2
StopIfError "Could not mkdir '${TMP_DIR}/rsync/${RSYNC_PREFIX}'"
mkdir -p $v -m0755 "${TMP_DIR}/rsync/${RSYNC_PREFIX}/backup" >&2
StopIfError "Could not mkdir '${TMP_DIR}/rsync/${RSYNC_PREFIX}/backup'"
mkdir -p $v -m0750 "${TMP_DIR}/rsync/${RSYNC_PREFIX}" >&2 || Error "Could not mkdir '${TMP_DIR}/rsync/${RSYNC_PREFIX}'"
mkdir -p $v -m0755 "${TMP_DIR}/rsync/${RSYNC_PREFIX}/backup" >&2 || Error "Could not mkdir '${TMP_DIR}/rsync/${RSYNC_PREFIX}/backup'"

case $RSYNC_PROTO in

(ssh)
$BACKUP_PROG -a $v -r "${TMP_DIR}/rsync/${RSYNC_PREFIX}" "${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PATH}" >/dev/null 2>&1
StopIfError "Could not create '${RSYNC_PATH}/${RSYNC_PREFIX}' on remote ${RSYNC_HOST}"
$BACKUP_PROG -a $v -r "${TMP_DIR}/rsync/${RSYNC_PREFIX}" "${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PATH}" >/dev/null 2>&1 \
|| Error "Could not create '${RSYNC_PATH}/${RSYNC_PREFIX}' on remote ${RSYNC_HOST}"
;;

(rsync)
$BACKUP_PROG -a $v -r "${TMP_DIR}/rsync/${RSYNC_PREFIX}" ${BACKUP_RSYNC_OPTIONS[@]} "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/" >/dev/null
StopIfError "Could not create '${RSYNC_PATH}/${RSYNC_PREFIX}' on remote ${RSYNC_HOST}"
$BACKUP_PROG -a $v -r "${TMP_DIR}/rsync/${RSYNC_PREFIX}" ${BACKUP_RSYNC_OPTIONS[@]} "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/" >/dev/null \
|| Error "Could not create '${RSYNC_PATH}/${RSYNC_PREFIX}' on remote ${RSYNC_HOST}"
;;

esac
28 changes: 14 additions & 14 deletions usr/share/rear/output/RSYNC/default/900_copy_result_files.sh
Expand Up @@ -6,39 +6,39 @@ LogPrint "Copying resulting files to $OUTPUT_URL location"
# if called as mkbackuponly then we just don't have any result files.
if test "$RESULT_FILES" ; then
Log "Copying files '${RESULT_FILES[@]}' to $OUTPUT_URL location"
cp $v "${RESULT_FILES[@]}" "${TMP_DIR}/rsync/${RSYNC_PREFIX}/"
StopIfError "Could not copy files to local rsync location"
cp $v "${RESULT_FILES[@]}" "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" \
|| Error "Could not copy files to local rsync location"
fi

echo "$VERSION_INFO" >"${TMP_DIR}/rsync/${RSYNC_PREFIX}/VERSION"
StopIfError "Could not create VERSION file on local rsync location"
echo "$VERSION_INFO" >"${TMP_DIR}/rsync/${RSYNC_PREFIX}/VERSION" \
|| Error "Could not create VERSION file on local rsync location"

cp $v $(get_template "RESULT_usage_$OUTPUT.txt") "${TMP_DIR}/rsync/${RSYNC_PREFIX}/README"
StopIfError "Could not copy usage file to local rsync location"
cp $v $(get_template "RESULT_usage_$OUTPUT.txt") "${TMP_DIR}/rsync/${RSYNC_PREFIX}/README" \
|| Error "Could not copy usage file to local rsync location"

cat "$RUNTIME_LOGFILE" >"${TMP_DIR}/rsync/${RSYNC_PREFIX}/rear.log"
StopIfError "Could not copy $RUNTIME_LOGFILE to local rsync location"
cat "$RUNTIME_LOGFILE" >"${TMP_DIR}/rsync/${RSYNC_PREFIX}/rear.log" \
|| Error "Could not copy $RUNTIME_LOGFILE to local rsync location"

case $RSYNC_PROTO in

(ssh)
Log "$BACKUP_PROG -a ${TMP_DIR}/rsync/${RSYNC_PREFIX}/ ${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PATH}/${RSYNC_PREFIX}/"
# FIXME: Add an explanatory comment why "2>/dev/null" is useful here
# or remove it according to https://github.com/rear/rear/issues/1395
$BACKUP_PROG -a "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" "${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PATH}/${RSYNC_PREFIX}/" 2>/dev/null
StopIfError "Could not copy '${RESULT_FILES[@]}' to $OUTPUT_URL location"
$BACKUP_PROG -a "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" "${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PATH}/${RSYNC_PREFIX}/" 2>/dev/null \
|| Error "Could not copy '${RESULT_FILES[@]}' to $OUTPUT_URL location"
;;

(rsync)
Log "$BACKUP_PROG -a ${TMP_DIR}/rsync/${RSYNC_PREFIX}/ ${BACKUP_RSYNC_OPTIONS[@]} ${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/${RSYNC_PREFIX}/"
# FIXME: Add an explanatory comment why "2>/dev/null" is useful here
# or remove it according to https://github.com/rear/rear/issues/1395
$BACKUP_PROG -a "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" ${BACKUP_RSYNC_OPTIONS[@]} "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/${RSYNC_PREFIX}/" 2>/dev/null
StopIfError "Could not copy '${RESULT_FILES[@]}' to $OUTPUT_URL location"
$BACKUP_PROG -a "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" ${BACKUP_RSYNC_OPTIONS[@]} "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/${RSYNC_PREFIX}/" 2>/dev/null \
|| Error "Could not copy '${RESULT_FILES[@]}' to $OUTPUT_URL location"
;;

esac

# cleanup the temporary space (need it for the log file during backup)
rm -rf "${TMP_DIR}/rsync/${RSYNC_PREFIX}/"
LogIfError "Could not cleanup temporary rsync space: ${TMP_DIR}/rsync/${RSYNC_PREFIX}/"
rm -rf "${TMP_DIR}/rsync/${RSYNC_PREFIX}/" \
|| Log "Could not cleanup temporary rsync space: ${TMP_DIR}/rsync/${RSYNC_PREFIX}/"
11 changes: 5 additions & 6 deletions usr/share/rear/prep/RSYNC/default/100_check_rsync.sh
Expand Up @@ -79,8 +79,7 @@ esac

# check if host is reachable
if test "$PING" ; then
ping -c 2 "$RSYNC_HOST" >/dev/null
StopIfError "Backup host [$RSYNC_HOST] not reachable."
ping -c 2 "$RSYNC_HOST" >/dev/null || Error "Backup host [$RSYNC_HOST] not reachable."
else
Log "Skipping ping test"
fi
Expand All @@ -90,14 +89,14 @@ case "$RSYNC_PROTO" in

(rsync)
Log "Test: $BACKUP_PROG ${BACKUP_RSYNC_OPTIONS[@]} ${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/"
$BACKUP_PROG ${BACKUP_RSYNC_OPTIONS[@]} ${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/ >/dev/null
StopIfError "Rsync daemon not running on $RSYNC_HOST"
$BACKUP_PROG ${BACKUP_RSYNC_OPTIONS[@]} ${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/ >/dev/null \
|| Error "Rsync daemon not running on $RSYNC_HOST"
;;

(ssh)
Log "Test: ssh ${RSYNC_USER}@${RSYNC_HOST} /bin/true"
ssh ${RSYNC_USER}@${RSYNC_HOST} /bin/true >/dev/null 2>&1
StopIfError "Secure shell connection not setup properly [$RSYNC_USER@$RSYNC_HOST]"
ssh ${RSYNC_USER}@${RSYNC_HOST} /bin/true >/dev/null 2>&1 \
|| Error "Secure shell connection not setup properly [$RSYNC_USER@$RSYNC_HOST]"
;;

esac
Expand Up @@ -10,8 +10,8 @@ if [ -z "$RSYNC_PROTOCOL_VERSION" ]; then
case $RSYNC_PROTO in

(ssh)
ssh ${RSYNC_USER}@${RSYNC_HOST} rsync --version >"$TMP_DIR/rsync_protocol" 2>&1
StopIfError "Secure shell connection not setup properly [$RSYNC_USER@$RSYNC_HOST]"
ssh ${RSYNC_USER}@${RSYNC_HOST} rsync --version >"$TMP_DIR/rsync_protocol" 2>&1 \
|| Error "Secure shell connection not setup properly [$RSYNC_USER@$RSYNC_HOST]"
grep -q "protocol version" "$TMP_DIR/rsync_protocol"
if [ $? -eq 0 ]; then
RSYNC_PROTOCOL_VERSION=$(grep 'protocol version' "$TMP_DIR/rsync_protocol" | awk '{print $6}')
Expand Down Expand Up @@ -41,8 +41,8 @@ if [ "${RSYNC_USER}" != "root" ]; then
else
# when using --fake-super we must have user_xattr mount options on the remote mntpt
remote_mountpoint=$(ssh ${RSYNC_USER}@${RSYNC_HOST} 'cd ${RSYNC_PATH}; df -P .' 2>/dev/null | tail -1 | awk '{print $6}')
ssh ${RSYNC_USER}@${RSYNC_HOST} "cd ${RSYNC_PATH} && touch .is_xattr_supported && setfattr -n user.comment -v 'File created by ReaR to test if this filesystems supports extended attributes.' .is_xattr_supported && getfattr -n user.comment .is_xattr_supported 1>/dev/null; find .is_xattr_supported -empty -delete"
StopIfError "Remote file system $remote_mountpoint does not have user_xattr mount option set!"
ssh ${RSYNC_USER}@${RSYNC_HOST} "cd ${RSYNC_PATH} && touch .is_xattr_supported && setfattr -n user.comment -v 'File created by ReaR to test if this filesystems supports extended attributes.' .is_xattr_supported && getfattr -n user.comment .is_xattr_supported 1>/dev/null; find .is_xattr_supported -empty -delete" \
|| Error "Remote file system $remote_mountpoint does not have user_xattr mount option set!"
#BACKUP_RSYNC_OPTIONS+=( --xattrs --rsync-path="rsync --fake-super" )
# see issue #366 for explanation of removing --xattrs
BACKUP_RSYNC_OPTIONS+=( --rsync-path="rsync --fake-super" )
Expand Down
Expand Up @@ -3,12 +3,12 @@
case $RSYNC_PROTO in

(ssh)
ssh ${RSYNC_USER}@${RSYNC_HOST} "ls -ld ${RSYNC_PATH}/${RSYNC_PREFIX}/backup" >/dev/null 2>&1
StopIfError "Archive not found on [$RSYNC_USER@$RSYNC_HOST:${RSYNC_PATH}/${RSYNC_PREFIX}]"
ssh ${RSYNC_USER}@${RSYNC_HOST} "ls -ld ${RSYNC_PATH}/${RSYNC_PREFIX}/backup" >/dev/null 2>&1 \
|| Error "Archive not found on [$RSYNC_USER@$RSYNC_HOST:${RSYNC_PATH}/${RSYNC_PREFIX}]"
;;

(rsync)
$BACKUP_PROG "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/${RSYNC_PREFIX}/backup" >/dev/null 2>&1
StopIfError "Archive not found on [$RSYNC_USER@$RSYNC_HOST:${RSYNC_PATH}/${RSYNC_PREFIX}]"
$BACKUP_PROG "${RSYNC_PROTO}://${RSYNC_USER}@${RSYNC_HOST}:${RSYNC_PORT}/${RSYNC_PATH}/${RSYNC_PREFIX}/backup" >/dev/null 2>&1 \
|| Error "Archive not found on [$RSYNC_USER@$RSYNC_HOST:${RSYNC_PATH}/${RSYNC_PREFIX}]"
;;
esac

0 comments on commit e7efac5

Please sign in to comment.