#! /usr/bin/env bash # script simply does a pgdump to the entire database, placing the result in # DBSAVEPATH VERSION=1.0.0 # fully path where backups go. Will be created if it doesn't exist DBSAVEPATH=/home/dbbackup/postgres # Get date in dd-mm-yyyy format NOW="$(date +"%Y-%m-%d")" # make up a file name based on todays date POSTGRES_BACKUP_NAME=$NOW.posgres.dmp # find our commands CHOWN="$(which chown)" CHMOD="$(which chmod)" GZIP="$(which gzip)" PG_DUMP="$(which pg_dumpall)" # make sure pg_dumpall exists if [ ! -e $PG_DUMP ] then echo Can not find pg_dumpall, exiting exit 0 fi # create save path, if it does not exist if [ ! -e $DBSAVEPATH ] then mkdir -p $DBSAVEPATH chown postgres:root $DBSAVEPATH fi # tell the user something echo Backing up Postgres to $DBSAVEPATH # get all postgres databases, just do a db dump. Note that pg_dumpall must # run as usre postgres su postgres -c 'pg_dumpall -d -c' | $GZIP -9 > $DBSAVEPATH/$POSTGRES_BACKUP_NAME.gz # Set ownership and permissions on file $CHOWN root:root $DBSAVEPATH/$POSTGRES_BACKUP_NAME.gz $CHMOD 600 $DBSAVEPATH/$POSTGRES_BACKUP_NAME.gz