MOCKSTACKS
EN
Questions And Answers

More Tutorials









postgreSQL Backup script for a production DB

Syntax


•The script allows you to create a backup directory for each execution with the following syntax : Name of database backup directory + date and time of execution

• Example : prodDir22-11-2016-19h55

•After it's created, it creates two backup files with the following syntax : Name of database + date and time of execution

• Example :

• dbprod22-11-2016-19h55.backup (dump file)

• dbprod22-11-2016-19h55.sql (sql file)

• At the end of one execution at 22-11-2016 @ 19h55, we get :

• /save_bd/prodDir22-11-2016-19h55/dbprod22-11-2016-19h55.backup

• /save_bd/prodDir22-11-2016-19h55/dbprod22-11-2016-19h55.sql

Examples

saveProdDb.sh


In general, we tend to back up the DB with the pgAdmin client. The following is a sh script used to save the database (under linux) in two formats:

• SQL file: for a possible resume of data on any version of PostgreSQL.

• Dump file: for a higher version than the current version.


#!/bin/sh
cd /save_db
#rm -R /save_db/*
DATE=$(date +%d-%m-%Y-%Hh%M)
echo -e "Sauvegarde de la base du ${DATE}"
mkdir prodDir${DATE}
cd prodDir${DATE}
#dump file
/opt/postgres/9.0/bin/pg_dump -i -h localhost -p 5432 -U postgres -F c -b -w -v -f
"dbprod${DATE}.backup" dbprod
#SQL file
/opt/postgres/9.0/bin/pg_dump -i -h localhost -p 5432 -U postgres --format plain --verbose -f
"dbprod${DATE}.sql" dbprod


Conclusion

In this page (written and validated by ) you learned about postgreSQL Backup script for a production DB . What's Next? If you are interested in completing postgreSQL tutorial, your next topic will be learning about: postgreSQL COALESCE.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.