153 lines
4.3 KiB
Bash
Executable File
153 lines
4.3 KiB
Bash
Executable File
#!/bin/bash
|
|
display_help() {
|
|
(>&2 echo "Usage: $(basename "$0") [-chqv] [-o outfile] indir iseq")
|
|
(>&2 echo "UCF tar auxiliary packer")
|
|
(>&2 echo)
|
|
(>&2 echo " indir path to input directory")
|
|
(>&2 echo " iseq sequence number with leading zeros")
|
|
(>&2 echo " -c, --checksum compares checksums after the archive is created")
|
|
(>&2 echo " -h, --help display this help message")
|
|
(>&2 echo " -o, --outfile output file (default: snapshot_XXXX.ucf.tar)")
|
|
(>&2 echo " -q, --quicksum same as --checksum, but compares only first bytes")
|
|
(>&2 echo " -v, --verbose verbose output")
|
|
}
|
|
exit_script() {
|
|
#trap - SIGINT SIGTERM # clear the trap
|
|
#kill -- -$$ # Sends SIGTERM to child/sub processes
|
|
(>&2 echo "SIGINT/SIGTERM received: removing archive")
|
|
rm $fout
|
|
}
|
|
# Parse command line arguments
|
|
if [ $# -eq 0 ]; then
|
|
display_help
|
|
exit -1
|
|
fi
|
|
fout=""
|
|
verbose=0
|
|
checksum=0
|
|
quicksum=0
|
|
POSITIONAL=()
|
|
while [[ $# -gt 0 ]]
|
|
do
|
|
key="$1"
|
|
case $key in
|
|
-h|--help)
|
|
display_help
|
|
exit 0
|
|
shift # past argument
|
|
;;
|
|
-o|--outfile)
|
|
fout="$2"
|
|
shift # past argument
|
|
shift # past value
|
|
;;
|
|
-v|--verbose)
|
|
verbose=1
|
|
shift # past argument
|
|
;;
|
|
-c|--checksum)
|
|
checksum=1
|
|
shift # past argument
|
|
;;
|
|
-q|--quicksum)
|
|
quicksum=1
|
|
shift # past argument
|
|
;;
|
|
*) # unknown option
|
|
POSITIONAL+=("$1") # save it in an array for later
|
|
shift # past argument
|
|
;;
|
|
esac
|
|
done
|
|
set -- "${POSITIONAL[@]}"
|
|
|
|
# Parse input filename
|
|
din=$1
|
|
seqnum=$2
|
|
|
|
# Construct output filename if not specified
|
|
if [ -z "$fout" ]; then
|
|
fout="auxiliary_${seqnum}.ucf.tar"
|
|
fi
|
|
|
|
# Display verbose info
|
|
if [ $verbose -eq 1 ]; then
|
|
(>&2 echo "Creating archive: $fout")
|
|
fi
|
|
|
|
# Get input files:
|
|
# following files must exist:
|
|
# parameters, grid, proc
|
|
# glob auxiliary files:
|
|
# every file which matches "*_${seqnum}.*", except uvwp, scal, particles
|
|
fparam="parameters_${seqnum}.asc"
|
|
fgrid="grid_${seqnum}.bin"
|
|
fproc="proc_${seqnum}.bin"
|
|
|
|
#GLOBIGNORE="${din}/parameters_*:${din}/grid_*:${din}/proc_*:${din}/uvwp_*:${din}/scal_*:${din}/particles_*"
|
|
#faux=( $(basename $din/*_${seqnum}.*) )
|
|
#unset GLOBIGNORE
|
|
faux=( $(find ${din} -name "*_${seqnum}.*" ! -name "parameters_*" ! -name "grid_*" ! -name "proc_*" ! -name "particles_*" ! -name "uvwp_*" ! -name "scal_*" -exec basename {} \;) )
|
|
|
|
# Check if obligatory files are present
|
|
if [ ! -s ${din}/${fparam} ]; then
|
|
(>&2 echo "[Error] File not found or empty: ${din}/${fparam}")
|
|
exit 1
|
|
fi
|
|
if [ ! -s ${din}/${fgrid} ]; then
|
|
(>&2 echo "[Error] File not found or empty: ${din}/${fgrid}")
|
|
exit 1
|
|
fi
|
|
if [ ! -s ${din}/${fproc} ]; then
|
|
(>&2 echo "[Error] File not found or empty: ${din}/${fproc}")
|
|
exit 1
|
|
fi
|
|
|
|
# Create a full file list for the archive
|
|
flist=(${fparam} ${fgrid} ${fproc} ${faux[@]})
|
|
|
|
# Now tar them and remove seqence number from file names while doing so
|
|
flagtar=""
|
|
if [ $verbose -eq 1 ]; then
|
|
flagtar="$flagtar --verbose"
|
|
fi
|
|
trap exit_script SIGINT SIGTERM
|
|
tar $flagtar --format ustar --transform="flags=r;s|_$seqnum||" --directory=${din} -cf ${fout} ${flist[@]}
|
|
tarexit=$?
|
|
# Set exit status accoring to tar
|
|
if [ $tarexit -ne 0 ]; then
|
|
(>&2 echo "tar failed with exit code $tarexit")
|
|
exit 254
|
|
fi
|
|
|
|
# Compare checksums (CNC32), if flag is set
|
|
#din="./archive/" #for testing
|
|
flistx=($(echo ${flist[@]} | sed s/"_$seqnum"/""/g))
|
|
if [ $checksum -eq 1 ]; then
|
|
for ii in "${!flistx[@]}"; do
|
|
if [ $verbose -eq 1 ]; then
|
|
(>&2 echo "Verifying checksum: ${flist[$ii]}")
|
|
fi
|
|
crcori=$(cksum ${din}/${flist[$ii]} | awk '{ print $1, $2 }')
|
|
crctar=$(tar --to-command='cksum -' -xf ${fout} ${flistx[$ii]} | awk '{ print $1, $2 }')
|
|
if [ "$crcori" != "$crctar" ]; then
|
|
(>&2 echo "Verification failed: ${flist[$ii]} ${flistx[$ii]}")
|
|
exit 5
|
|
fi
|
|
done
|
|
elif [ $quicksum -eq 1 ]; then
|
|
for ii in "${!flistx[@]}"; do
|
|
if [ $verbose -eq 1 ]; then
|
|
(>&2 echo "Verifying partial checksum: ${flist[$ii]}")
|
|
fi
|
|
crcori=$(head -c 1M ${din}/${flist[$ii]} | cksum -)
|
|
crctar=$(tar --to-command='head -c 1M' -xf ${fout} ${flistx[$ii]} | cksum -)
|
|
if [ "$crcori" != "$crctar" ]; then
|
|
(>&2 echo "Verification failed: ${flist[$ii]} ${flistx[$ii]}")
|
|
exit 5
|
|
fi
|
|
done
|
|
fi
|
|
|
|
exit 0
|