Script To Take The PostgreSQL Physical Backup
Here we written the script for taking postgresql physical backup with replication and witout replication
Backup scripts for without replication:
psql -U postgres -c "SELECT pg_start_backup('base backup')" if [ "$?" != "0" ]; then echo Broken exit 1 fi tar cfz /some/where/base.tar.gz /var/lib/pgsql/data --exclude "*pg_xlog*" if [ "$?" != "0" ]; then echo Broken psql -U postgres -c "SELECT pg_stop_backup()" exit 1 fi psql -U postgres -c "SELECT pg_stop_backup()" if [ "$?" != "0" ]; then echo Broken exit 1 fi
Backup scripts for with replication:
And when you’re setting up a replication slave, it might look something like this:
psql -U postgres -h masterserver -c "SELECT pg_start_backup('replication base', 't')" if [ "$?" != "0" ]; then echo Broken exit 1 fi rsync -avz --delete --progress postgres@masterserver:/var/lib/pgsql/data /var/lib/pgsql if [ "$?" != "0" ]; then echo Broken psql -U postgres -c "SELECT pg_stop_backup()" exit 1 fi psql -U postgres -c "SELECT pg_stop_backup()" if [ "$?" != "0" ]; then echo Broken exit 1 fi