You are not logged in.

#1 2018-08-02 08:30:52

7thSon
Member
Registered: 2017-05-07
Posts: 132

[SOLVED] Bash - getting exit code and logging to file at the same time

I'm trying to log the output from my borg backup and at the same time store the exit code of the commands in a variable, this proved more difficult than I thought. The borg commands are run in a bash script and the relevant part is below:

#!/bin/bash
LOG="/volume1/scripts/borg/borg.log"
borg create [options] 2>&1 | tee -a $LOG; backup_exit=${PIPESTATUS[0]}

The above line is what I found when googling, but the PIPESTATUS[0] is never anything other than 0, even though I can follow the terminal output and see that the borg command exits with error code 2 because the backup already exists.
I've also tried setting "set -o pipefail" at the very top of the script, and using "backup_exit=$?" on the third line instead, but that still always returns 0.
What do I need to do in order to get this to work?

Last edited by 7thSon (2018-08-07 06:30:39)

Offline

#2 2018-08-02 14:40:02

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,282

Re: [SOLVED] Bash - getting exit code and logging to file at the same time

I tested this syntax here with the simple scripts 'testerror' and 'logexit' and it works as expected.

$ cat testerror
#!/bin/bash
if [[ $1 == "error" ]]; then
    echo "An error occured"
    exit 1
fi
echo "Normal output"
$ cat logexit
#!/bin/bash
LOG="$HOME/tmp/logexit.log"
testerror "$1" 2>&1 | tee -a $LOG; exit_value=${PIPESTATUS[0]}
echo "***$exit_value***"
$ logexit
Normal output
***0***
$ logexit error
An error occured
***1***

Are you sure 'borg' exits with a non zero error when the backup already exists ?
Because your syntax is correct for me.

Offline

#3 2018-08-02 22:08:30

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 413

Re: [SOLVED] Bash - getting exit code and logging to file at the same time

7thSon, I've never heard of PIPESTATUS before but your command above works for me. Any borg error is correctly reported in ${PIPESTATUS[0]}.

Offline

#4 2018-08-05 16:19:43

7thSon
Member
Registered: 2017-05-07
Posts: 132

Re: [SOLVED] Bash - getting exit code and logging to file at the same time

That's strange, When I run my script below I can see the terminal output saying that I get an error and "rc 2" which I assume is return code 2 and not 0.
Is there something in my script interfering perhaps?

#!/bin/bash
set -o pipefail

LOG="/volume1/scripts/borg/borgSynology.log"

#declare associative array (path + backup prefix)
declare -a destinations
destinations=( 	"/volume1/Backups/borg/Synology"  	\
		"ssh://borg@192.168.2.100:22/volume1/Backups/borg/Synology"	)

for (( i=0; i<${#destinations[@]}; i++ ));
do
	# Setting this, so the repo does not need to be given on the commandline:
	export BORG_REPO=${destinations[$i]}

	# Setting this, so you won't be asked for your repository passphrase:
	export BORG_PASSPHRASE=''
	# or this to ask an external program to supply the passphrase:
	#export BORG_PASSCOMMAND=''

	# some helpers and error handling:
	info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
	trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM

	info "Starting backup"

	# Backup the most important directories into an archive named after
	# the machine this script is currently running on:

	borg create                         			\
			--verbose                       	\
			--list                          	\
			--stats                         	\
			--show-rc                       	\
			--compression lz4               	\
			--exclude-caches                	\
			--exclude '/var/cache'        		\
			--exclude '/var/tmp'          		\
			--exclude '/var/run'            	\
			--exclude '/var/lib/lxcfs'      	\
			--exclude '/dev'                	\
			--exclude '/lost+found'         	\
			--exclude '/mnt'   					\
			--exclude '/proc'               	\
			--exclude '/run'                	\
			--exclude '/sys'                	\
			--exclude '/tmp'                	\
			   									\
			::'synology.{now:%Y-%m-%d}'   		\
			/bin					\
			/config					\
			/etc					\
			/opt					\
			/root					\
			/usr					\
			/var					\
			/volume1/@appstore/		\
			/volume1/@database/		\
			/volume1/@docker/		\
			/volume1/@homes/		\
			/volume1/@entware-ng/	\
			/volume1/scripts/		
	2>&1 | tee -a $LOG; backup_exit=${PIPESTATUS[0]}
	echo "backup_exit: $backup_exit" 

	info "Pruning repository"

	# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
	# archives of THIS machine. The '{hostname}-' prefix is very important to
	# limit prune's operation to this machine's archives and not apply to
	# other machines' archives also:

	borg prune                          \
	    --list                          \
	    --prefix 'synology'           	\
	    --show-rc                       \
	    --keep-daily    7               \
	    --keep-weekly   4               \
	    --keep-monthly  6               
	2>&1 | tee -a $LOG; prune_exit=${PIPESTATUS[0]}
	echo "prune_exit: $prune_exit"

	# use highest exit code as global exit code
	global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
done

if [ ${global_exit} -eq 0 ];
then
	info "Backup and/or Prune finished successfully"
	/usr/syno/bin/synodsmnotify "@administrators" "Borg" "Borg backup successful."
fi

if [ ${global_exit} -eq 1 ];
then
	info "Backup and/or Prune finished with a warning"
	/usr/syno/bin/synodsmnotify "@administrators" "Borg" "Borg backup finished with warning."
fi

if [ ${global_exit} -gt 1 ];
then
	info "Backup and/or Prune finished with an error"
fi

exit ${global_exit}

Last edited by 7thSon (2018-08-05 16:20:42)

Offline

#5 2018-08-05 17:14:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 20,114
Website

Re: [SOLVED] Bash - getting exit code and logging to file at the same time

You're missing a '\' on line 62, and 79.

The only thing you are piping into `tee` is `2>&1` which succeeds and gives exit code 0.  The fact that borg may have failed on a previously line does not impact this.

Last edited by Trilby (2018-08-05 17:16:53)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2018-08-07 06:30:25

7thSon
Member
Registered: 2017-05-07
Posts: 132

Re: [SOLVED] Bash - getting exit code and logging to file at the same time

Trilby wrote:

You're missing a '\' on line 62, and 79.

The only thing you are piping into `tee` is `2>&1` which succeeds and gives exit code 0.  The fact that borg may have failed on a previously line does not impact this.

*Facepalm*
You're absolutely right, adding the backslashes to both commands fixes the problem.
Thanks for the help and the keen eye smile

Offline

Board footer

Powered by FluxBB