Skip to content

Instantly share code, notes, and snippets.

@sebastianwebber
Last active October 11, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianwebber/644c7d4a51a4f610db87c0b05f2705e8 to your computer and use it in GitHub Desktop.
Save sebastianwebber/644c7d4a51a4f610db87c0b05f2705e8 to your computer and use it in GitHub Desktop.
#!/bin/bash
## check_dump.sh
### Verifica a integridade do backup, convertendo ele do tipo diretório para SQL Plain
### Procura por arquivos .tar no diretório `DUMP_DIR`, extrai eles no `TEMP_DIR` e efetua o teste
## Baseado na idéia do @fabriziomello
#########
function show_usage() {
app_name=$(basename $0 .sh)
cat <<EOF
${app_name} is a PostgreSQL dump validator.
Usage:
${app_name} [OPTIONS]
General options:
-d, --dump-dir=PATH set dump directory path
-t, --temp-dir=PATH set temporary directory path (defaults to '/tmp')
EOF
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--dump-dir=*)
DUMP_DIR=$( echo "${1}" | cut -d '=' -f 2- )
;;
-d)
DUMP_DIR="$2"
shift
;;
--temp-dir=*)
TEMP_DIR=$( echo "${1}" | cut -d '=' -f 2- )
;;
-t)
DUMP_DIR="$2"
shift
;;
*)
show_usage
exit 2
;;
esac
shift
done
if [ "${DUMP_DIR}X" = "X" ]; then
show_usage
fi
if [ "${TEMP_DIR}X" = "X" ]; then
TEMP_DIR='/tmp'
fi
function cleanup_tempdir() {
rm -rf "${TEMP_DIR}"
mkdir -p "${TEMP_DIR}"
}
for file in $(ls "${DUMP_DIR}"); do
dump_file="${TEMP_DIR}/${DUMP_DIR:1}/"$(basename "${file}" .tar)
cleanup_tempdir
cd "${TEMP_DIR}"
tar xf "${DUMP_DIR}/${file}"
/bin/pg_restore -Fd -U postgres -f /dev/null "${dump_file}"
if [ $? -ne 0 ]; then
echo "${file}: PROBLEMA"
else
echo "${file}: OK"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment