Score:0

PostgresSQL dump version recognition Bash script

de flag

I often recreate variuos customer databases on local dockers to test new ERP functionalities in the development environment So, PostgreSQL dump file usually starts with:

--
-- PostgreSQL database dump
--

-- Dumped from database version 13.3 (Ubuntu 13.3-1.pgdg20.04+1)
-- Dumped by pg_dump version 13.3 (Debian 13.3-1.pgdg100+1)
...

Thats why my bash script 'restorefromdump.sh' (building temporary docker of the postgresql server appropriate version for dump file handling) starts with:

#!/bin/bash
DBDUMP=$1
DUMPFROM=$(grep "^-- Dumped.*from " "$DBDUMP")
DUMPVERSION_inclSpaces=$(grep -o '^-- Dumped from.*(' "$DBDUMP" | grep -o -E '[0-9*.]+[^version]')
DUMPVERSION="$(echo -e "${DUMPVERSION_inclSpaces}" | tr -d '[:space:]')"
REMOTEPGVER=postgres:$DUMPVERSION
# calls 'buildtmpdocker' function to build temporary postgres docker 
buildtmpdocker $REMOTEPGVER 
...

After direct command './restorefromdump.sh *.dmp' Seems to be working effectively - it recognises dump version correctly, but maybe there are better ways? Are you used to recognise version inside dump file with Bash script?

muru avatar
us flag
What is the regex `[0-9*.]+[^version]` supposed to be doing according to you?
Andrzej Więcławski avatar
de flag
it finds numbers before 'version' string - example: | $ nano testfile | content: 'asw123version' | after save the 'testfile' | $ grep -o -E '[0-9*.]+[^version]' testfile | result: '123'
Andrzej Więcławski avatar
de flag
it finds ONLY numbers :)
muru avatar
us flag
I don't know how you came up with that, but no, that's not what that regex does. It looks for one or more instances of digits, `*` or `.` followed by anything other than the letters `e`, `i`, `n`, `o`, `r`, `s`, `v`.
Andrzej Więcławski avatar
de flag
So, the conclusion is... What is your opinion? Is my way wrong to get the target: number of PostgreSQL version including points?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.