Sometime we face problem to take
backup or make (Definition) scripts of Functions or Tables separately for entire schema.
When we want to take backup
separately for objects like function, table etc., here's need to generate
object wise backup scripts separately.So for achieving this, go through below:
In this example I am using Database(DB)=postgres , User (U)=postgres, Schema=public and OS=Centos7 x64
In this example I am using Database(DB)=postgres , User (U)=postgres, Schema=public and OS=Centos7 x64
You can change name of your Database,
User and Schema as per your requirement.
(A) Make separate Function
scripts
Step 1. Create an .sh(shell script) file and paste below commands to
generate functions script by their functions name :
Suppose I have created funcscript.sh
Step 2 Open it in vi editor :
vi funcscript.sh
# Paste below text or change it as per your requirement.
# Config:
DB=postgres
U=postgres
export PGPASSWORD=<your postgres user password>
# funcname searchpattern, if you
want all funcs enter "":
P=""
# Directory to dump files without
trailing slash:
DIR=~/psql_db_dump_dir/public
mkdir -p $DIR
publicfuncS="$(psql -d $DB -U $U
-t -c "SELECT nspname||'.'||proname
FROM
pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p ON
pronamespace = n.oid
WHERE nspname = 'public' order by 1")"
for publicfunc in $publicfuncS; do
echo backup $publicfunc ...
psql -d $DB -U $U -t -c "SELECT
pg_get_functiondef('$publicfunc'::regproc)" > $DIR/$publicfunc.sql;
done;
echo done
Step 3. Give execute permission to this file funcscript.sh
chmod 700 funcscript.sh [Hint
: Change it as per your requirement]
Step 4. Run this file
./funcscript.sh
Step 5 . Go to /psql_db_dump_dir/public directory to check
your dump scripts.
(B) Make
separate Table wise scripts
Step 1. Create an .sh(shell script) file and paste below commands to generate
table script by their table name :
Suppose I have created tablescript.sh
Step 2 Open it in vi editor :
vi tablescript.sh
# Paste below text or change
it as per your requirement.
# Config:
DB=postgres
U=postgres
export PGPASSWORD=<your postgres user password>
# tablename search pattern, if you
want all tables enter "":
P=""
# directory to dump files without
trailing slash:
DIR=~/psql_db_dump_dir/public
mkdir -p $DIR
TABLES="$(psql -d $DB -U $U -t
-c "SELECT table_schema||'.' ||table_name
FROM information_schema.tables WHERE
table_type='BASE TABLE' AND table_schema = 'public' ORDER BY table_name")"
for table in $TABLES; do
echo backup $table ...
# pg_dump $DB -U $U -w -t
$table > $DIR/$table.sql;
pg_dump -U postgres -F plain --schema-only -t $table -w postgres >
$DIR/$table.sql;
done;
echo done
[Hint : -F
plain , it takes plain text backup , you can change format as
per your requirement]
Step 3. Give execute permission to this file tablescript.sh
chmod 700 tablescript.sh [Hint
:Change it as per your requirement]
Step 4. Run this file
./funcscript.sh
Step 5 . Go to /psql_db_dump_dir/public directory to check
your dump scripts.
Great work Prakashji. Keep it up
ReplyDeleteGood Solutions.
ReplyDeleteGood post Prakash ji, bro helpful 👍👍👍
ReplyDeleteThank you sir. We are expecting more from yours.
ReplyDeleteGreat work.... Thank you for sharing your knowledge.....
ReplyDelete
ReplyDeleteNice article sir.
I have published xml tutorials if anybody want to learn XML.
A very well needed example and guidance, typically I have to backup all DB and structure if changes are made in only 1 schema. Thank you very much.
ReplyDeleteMuch appreciated.
It would be of great help if along with postgres, MySQL tutorials are also mentioned.
Thanks in advance !!!!
Great work
ReplyDeleteIt informative articles
ReplyDeleteThank you so much for providing information and highlighting many tools and utilities to look for vivid aspects and solve broader errors.
ReplyDeleteSSIS Postgresql Write
Great Post with valuable info. Thank you for the updates.
ReplyDeleteLearn Dot NET Online
Dot NET Certification Course Online