Last edit by philipp October 15, 2017 09:52

How to create a Dump of a Postgres database

This document shows, how to create a dump of a Postgres database with Cron.

mkdir /etc/scripts
nano /etc/scripts/dump.postgres.sh
#!/bin/sh
# Copyright 2015 Typomedia Foundation. All rights reserved.
# Use is subject to license terms.
#
# Postgres Dump Script v1.0

DBHOST=127.0.0.1
DBUSER=dbmaster
PASSWORD=******
DATE=`date '+%Y%m%d'`

PGPASSWORD=$PASSWORD pg_dump --host=$DBHOST --username=$DBUSER \
databasename > /var/backups/pgsql/${DATE}_databasename.sql
chmod +x /etc/scripts/dump.postgres.sh

Cron

crontab -e
4 * * * * /etc/scripts/dump.postgres.sh

Restore

psql databasename < /var/backups/pgsql/20170914_databasename.sql

The content on this page is licensed under Creative Commons Attribution 4.0 International license.