Backup standby database using LVM snapshot Objective:  Backup standby database (synchronous streaming replication) using LVM snapshot.

When running pg_start_backup:

postgres=# SELECT pg_start_backup('label');
ERROR:  recovery is in progress
HINT:  WAL control functions cannot be executed during recovery.


Is it possible to copy backup_label and tablespace_map from the primary database's cluster directory into the standby database's cluster directory in order to perform the LVN snapshot backup of the standby cluster directory?
To do that on the standby server you need to configure it like a master server to enable the cascading replication[1].


  To enable it, you need to change the follow parameters:
  
  
  wal_level to archive or hot_standby
  If plan to use streamming replication:
  
  
  max_wal_senders to 2
  
  If you plan to use wal archiving:
  
  
  archive_mode to on
  archive_command to some place
  
  
  
  Then restart your standby database.
  
  When you configure that, you can run the pg_start_backup function without a problem.


To do what you need (backup with LVM) you must:


  PLEASE test this steps on a test machine to avoid any issues on your production database server.



run pg_start_backup:

psql -U postgres -c "SELECT pg_start_backup('my lvm backup');"

make the lvm snapshot[2] (and export it to a file[3]), eg:

## Repeat that action for each tablespace
## TODO: Review max grow size for your snapshot
lvcreate --size 10G --snapshot --name pgdata_backup_snapshot /path/to/pgdata/dir
## export the LVM to a image (with the `dd` command): 
### TODO: Replace the `vg_name` by correct one
dd if=/dev/vg_name/pgdata_backup_snapshot of=/path/to/pgdata_backup.dd
## now remove the snapshot
lvremove /dev/vg_name/pgdata_backup_snapshot

run pg_stop_backup function:

psql -U postgres -c "SELECT pg_stop_backup();"



Hope it helps.

References:


https://www.postgresql.org/docs/current/static/warm-standby.html#CASCADING-REPLICATION
http://www.tecmint.com/take-snapshot-of-logical-volume-and-restore-in-lvm/
https://www.howtoforge.com/linux_lvm_snapshots_p2
http://www.tldp.org/HOWTO/LVM-HOWTO/snapshots_backup.html