Skip to content

Instantly share code, notes, and snippets.

@sebastianwebber
Last active December 2, 2015 12:28
Show Gist options
  • Select an option

  • Save sebastianwebber/680bfddd6cb5c84ba809 to your computer and use it in GitHub Desktop.

Select an option

Save sebastianwebber/680bfddd6cb5c84ba809 to your computer and use it in GitHub Desktop.
Laborário PostgreSQL Administração - Módulo 5: Backup

Instruções

Faça o download o script no diretório /root/lab (crie-o se necessário):

wget https://gist.githubusercontent.com/sebastianwebber/680bfddd6cb5c84ba809/raw/a9b00d047f751145c69abf523dba7a3bf880f9ea/lab5-1.sh -O ~/lab/lab5-1.sh

Ajuste as permissões necessárias:

chmod +x ~/lab/lab5-1.sh

Execute o script:

./lab/lab5-1.sh /caminho/para/o/PGDATA

Importante: Lembre de substituir o /caminho/para/o/PGDATA pelo PGDATA do servidor PostgreSQL.

#!/bin/bash
pgdata="${1}"
target_db='producao'
target_table='pessoa'
function execute_psql() {
query="${1}"
result=$(psql -U postgres -d "${target_db}" -c "${query}" -t | xargs echo)
echo "${result}"
}
if [ "${pgdata}X" = "X" ]; then
pgdata=$(execute_psql "SHOW data_directory")
fi
## TODO: testar se o banco está no tablespace diferente pra não apagar o arquivo errado.
table_node=$(execute_psql "SELECT pg_relation_filepath(oid) FROM pg_class WHERE relname = '${target_table}'; ")
## no mundo real, utilizar a função pg_relation_filepath
## ex:
### SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'customer';
table_file="${pgdata}/${table_node}"
echo "Removing '"${table_file}"' datafile from table '"${target_table}"'..."
rm -f "${table_file}"
echo Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment