pg_dump script for database backup

How I can create an script to backup my postgres databases on linux systems using pg_dump

Already use the comand but i want someting execute itself no user interaction so

pg_dump -U postgres -W -Fc

This ask for password and if I use -w ask for a pgpass file

1 Answer

It's an script for root user, place on /root directory:

#! /bin/sh
USER="postgres"
PASS="password"
## Staorage Dir
bkp_dir="/home/USER_HOME/databases-$(date +%Y%m%d)"
echo "Creating directory: ${bkp_dir}"
mkdir $bkp_dir
## Temporal credential access file
credentialsFile=".pgpass"
echo "*:*:*:$USER:$PASS" >> $credentialsFile
chmod 600 $credentialsFile
echo "Backing up databases"
pg_dump -U postgres -w -Fc > $bkp_dir/
rm $credentialsFile

You can create a crontab for autorun the script as your wish

Resources:

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like