备份postsql并以日期命名打包
2017-04-28 本文已影响9人
dotdiw
1.新建 ** bak_postsql.sh**(PostgreSQL的版本 9.6.2)
#! /bin/sh
DATE=`date +%Y%m%H`
BACKDIR="/tmp/psqlbak"
PGDUMP='/usr/local/pgsql/bin/pg_dump'
if [ ! -d $BACKDIR ]
then
mkdir $BACKDIR
fi
for DB in `psql -U postgres -l -q -t | awk '{print $1}'`
do
if [ $DB != "|" ] && [ $DB != 'template0' ] && [ $DB != 'template1' ]; then
FILENAME=${DATE}_${DB};
echo "backup ${DB} ..."
$PGDUMP -U postgres -f $BACKDIR/$FILENAME.sql $DB
tar -C $BACKDIR -c -j -f $BACKDIR/$FILENAME.tar.bz2 $FILENAME.sql && rm -f $BACKDIR/$FILENAME.sql
fi
done
sync```