summaryrefslogtreecommitdiff
path: root/psqlbak.sh
blob: 513cfd6703fc69a4d6010e1285c7de08a679102f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
#
## Daily PostgreSQL maintenance: vacuuming and backuping.
#
###
set -e
for DB in $(psql -l -t -U postgres -hlocalhost |awk '{ print $1}' |grep -vE '^-|:|^List|^Name|template[0|1]|postgres|\|'); do
    echo "[`date`] Maintaining $DB"
    echo 'VACUUM' | psql -U postgres -hlocalhost -d $DB
    DUMP="/home/lxf/bak/postgres/$DB.`date '+%Y%m%d'`.sql.gz"
    pg_dump -U postgres -hlocalhost $DB | gzip -c > $DUMP
    PREV="$DB.`date -d'1 day ago' '+%Y%m%d'`.sql.gz"
    md5sum -b $DUMP > $DUMP.md5
    if [ -f $PREV.md5 ] && diff $PREV.md5 $DUMP.md5; then
        rm $DUMP $DUMP.md5
    fi
done