● PostgreSQL provides native backup tools for both Logical and Physical backups.
● Backups similar to mysqldump and Xtrabackup are automatically included with Community PostgreSQL
● Backups like RMAN in Oracle may be achieved using Open Source tools like pgBackRest and pgBarman
○ Logical Backups
■ pg_dump (Both Custom(Compressed and non human-readable) and Plain Backups)
■ pg_restore (To restore the custom backups taken using pg_dump)
■ pg_dumpall (To backup Globals - Users and Roles)
■ Logical Backups cannot be used to setup Replication and perform a PITR
■ You cannot apply WAL’s after restoring a Backup taken using pg_dump
pg_dump options
-a : Data only . No data definition
-s : Data Definition only
-n (schema) : Dump specified schema
-t ( table) : Dump Specified Table only
-f ( file name ) : Dump data to specified file
-Fp : Dump in plan text ( default )
-Ft : Dump in tar format
-Fc : Dump in compressed format
Eg
pg_dump -U postgres -d nano > /tmp/backup ( backup will be created in text format )
Let’s use pgbench to create some sample tables
$ pgbench -i percona (Initialize)
$ pgbench -T 10 -c 10 -j 2 percona (load some data)
● Use pg_dump to backup the DDL (schema-only) of database: percona
$ pg_dump -s percona -f /tmp/percona_ddl.sql
● Use pg_dump to backup a table (with data) using custom and plain text format
$ pg_dump -Fc —t public.pgbench_history -d percona -f /tmp/pgbench_history
$ pg_dump -t public.pgbench_branches -d percona -f /tmp/pgbench_branches
● Create an another database and restore both the tables using pg_restore and psql
$ psql -c "CREATE DATABASE testdb"
$ pg_restore -t pgbench_history -d testdb /tmp/pgbench_history
$ psql -d testdb -f /tmp/pgbench_branches
pg_dumpall
○ Can dump all the databases of a cluster into a script file
○ Use psql to restore the backup taken using pg_dumpall
○ Can be used to dump global objects such as ROLES and TABLESPACES
● To dump only Globals using pg_dumpall, use the following syntax
$ pg_dumpall -g > /tmp/globals.sql
● To dump all databases (or entire Cluster), use the following syntax
$ pg_dumpall > /tmp/globals.sql
We can also use unix tools along
eg : pg_dump dbname | gzip > filename.gz
pg_dump -U postgres -d nano | gzip > /tmp/backup ( backup will be created in zipped text format )
pg_dump -U postgres -d nano | split -b 1k > /tmp/backup ( backup will be created in splitted text format )
○ Physical Backups
■ pg_basebackup : File System Level & Online Backup, similar to Xtrabackup for MySQL
■ Useful to build Replication and perform PITR
■ This Backup can only use one process and cannot run in parallel
■ Explore Open Source Backup tools like : pgBackRest, pgBarman and WAL-e for more features like Xtrabackup
Command line options for pg_basebackup
$ pg_basebackup --help
-D --> Target Location of Backup
-cfast -—> Issues a fast checkpoint to start the backup earlier
-Ft -—> Tar format. Use -Fp for plain
-v --> Print the Backup statistics/progress.
-U --> A User who has Replication Privilege.
-W --> forcefully ask for password of replication User above. (Not mandatory)
-z --> Compresses the Backup
-R --> Creates a recovery.conf file that can be used to setup replication
-P --> Shows the progress of the backup
-l --> Creates a backup_label file
pg_basebackup -U postgres -p 5432 -h 127.0.0.1 -D /tmp/backup_11052018 -Ft -z -Xs -P -R -l backup_label
Taking Consistent Backup using pg_basebackup
1) Enable Wal archiving for point in time recovery
wal_level = archive
archive_mode = on
psql select * from pg_settings where name ='wal_level';
psql show wal_level you
psql select name, setting, sourcefile, sourceline from pg_settings where name = 'wal_level';
psql -c 'show config_file;'
grep wal_level /data/pgsql/9.5/data/postgresql.conf
2) create a checkpoint using pg_start_backup and Copy contents of the data directory
pg_basebackup -U psql -h 10.0.0.110 -x -D /pgbase/
This script will take the backup of postgreSQL cluster daily basis and it will remove the backup files if backup age reached 7 days. its the
#Backup Dir
#Make sure this is a dedicated mount point to PostgreSQL Backups
#Don't put traling / in path
#Backup Details
backup_dir=/backup
export PGPASSWORD="Nijam@1234"
export PGPORT="5432"
echo -e "\n\nBackup Status: $(date +"%d-%m-%y")" >> $backup_dir/Status.log
echo -e "-----------------------" >> $backup_dir/Status.log
echo -e "\nStart Time: $(date)\n" >> $backup_dir/Status.log
/usr/lib/postgresql/9.6/bin/pg_basebackup -U psql -w -D $backup_dir/PostgreSQL_Base_Backup_$(date +"%d-%m-%y") -l "`date`" -P -F tar -z -R &>> $backup_dir/Status.log
echo -e "\nEnd Time: $(date)" >> $backup_dir/Status.log
#Auto Deletion for Backups
#Value 7 for retention_duration will keep 8 days backups
#retention_duration=7
#find $backup_dir/PostgreSQL_Base_Backup* -type d -mtime +$retention_duration -exec rm -rv {} \;
Found below document relevant to restoration of base backup :
https://dbsguru.com/restore-backup-using-pg_basebackup-postgresql/
https://www.percona.com/blog/2018/12/21/backup-restore-postgresql-cluster-multiple-tablespaces-using-pg_basebackup/
https://docs.google.com/document/d/1J8feIwMfXmKqOd4oXREFCnO9c7cwFoOT/edit?usp=sharing&ouid=108567388003714488593&rtpof=true&sd=true