code-dumps/sh/pgdump.sh
2019-01-02 16:15:37 +08:00

25 lines
558 B
Bash

#!/usr/bin/env bash
# @description pg dump script
# @version 0.1
# @author kfong
#
# Requires:
# * trusting postgres user from localhost
# # pg_hba.conf
# local all postgres trust
#
DATE=`date +%Y%m%d`
DUMPDIR=/dumps
DUMPUSER=postgres
DAYS2KEEP=14
# dump all databases in separate files
mkdir -p $DUMPDIR/PGD-$DATE
for d in `psql -U$DUMPUSER -lt | cut -d\| -f1 | grep -v template`; do
pg_dump -U$DUMPUSER -Z9 $d -f $DUMPDIR/PGD-$DATE/$d.pg.gz
done
# remove old dumps
find $DUMODIR -type d -mtime +$DAYS2KEEP -name "PGD-*" -exec rm -Rf {} \;