Skip to content

Commit

Permalink
check: ensure arithmetics work with bash older than 4.4 (fix #39)
Browse files Browse the repository at this point in the history
add tests on wal file names before splitting and converting to numbers,
so that we do not have to quote input: bash < 4.4 won't parse them as
hex numbers
  • Loading branch information
orgrim committed Apr 6, 2018
1 parent 48cdd4c commit 355384d
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions pitrery
Expand Up @@ -3358,10 +3358,14 @@ case $action in

# Extract the name of the WAL file from the backup
start_wal_file=$(awk '/^START WAL LOCATION/ { gsub(/[^0-9A-F]/,"",$6); print $6 }' <<< "$backup_label")

# This must be only (hex) digits, or the arithmetic operations below will not do what we hope
[[ $start_wal_file =~ ^[0-9A-F]{24}$ ]] || die "start wal file '$start_wal_file' does not appear to be a WAL segment file name in backup_label"

[[ "$output" != "nagios" ]] && info "start wal file is: $start_wal_file"
start_wal_tln=$(( 0x"$(cut -b 1-8 <<< "$start_wal_file")" ))
start_wal_wal=$(( 0x"$(cut -b 9-16 <<< "$start_wal_file")" ))
start_wal_seg=$(( 0x"$(cut -b 17-24 <<< "$start_wal_file")" ))
start_wal_tln=$(( 0x$(cut -b 1-8 <<< "$start_wal_file") ))
start_wal_wal=$(( 0x$(cut -b 9-16 <<< "$start_wal_file") ))
start_wal_seg=$(( 0x$(cut -b 17-24 <<< "$start_wal_file") ))

# Get the version of PostgreSQL to determine if we
# check WAL files which finish by FF.
Expand Down Expand Up @@ -3409,15 +3413,18 @@ case $action in
for wal_file in "${wal_list[@]}"; do
# Skip archived backup_label files
[[ "$wal_file" =~ .*\.backup.* ]] && continue

wal_file="$(basename -- "$wal_file")"
if ! [[ $wal_file =~ ^[0-9A-F]{24}.*$ ]]; then
[[ "$output" != "nagios" ]] && warn "'$wal_file' does not appear to be a WAL segment file name"
continue
fi

# Initialize or update the counters
if [ -z "$cur_tln" ]; then
[[ "$output" != "nagios" ]] && info "first WAL file checked is: $wal_file"
cur_tln=$(( 0x"$(cut -b 1-8 <<< "$wal_file")" ))
cur_wal=$(( 0x"$(cut -b 9-16 <<< "$wal_file")" ))
cur_seg=$(( 0x"$(cut -b 17-24 <<< "$wal_file")" ))
cur_tln=$(( 0x$(cut -b 1-8 <<< "$wal_file") ))
cur_wal=$(( 0x$(cut -b 9-16 <<< "$wal_file") ))
cur_seg=$(( 0x$(cut -b 17-24 <<< "$wal_file") ))
continue
else
if (( $cur_seg == $max_seg )); then
Expand All @@ -3429,9 +3436,9 @@ case $action in
fi

# split the name to tln wal seg
tln=$(( 0x"$(cut -b 1-8 <<< "$wal_file")" ))
wal=$(( 0x"$(cut -b 9-16 <<< "$wal_file")" ))
seg=$(( 0x"$(cut -b 17-24 <<< "$wal_file")" ))
tln=$(( 0x$(cut -b 1-8 <<< "$wal_file") ))
wal=$(( 0x$(cut -b 9-16 <<< "$wal_file") ))
seg=$(( 0x$(cut -b 17-24 <<< "$wal_file") ))

# check if we have reached the start wal file of the oldest backup
if ( (( $start_wal_tln == $tln )) && (( $start_wal_wal == $wal )) && (( $start_wal_seg == $seg )) ); then
Expand Down

0 comments on commit 355384d

Please sign in to comment.