Creating base backups

After teaching PostgreSQL to archive those WAL files, it is time to create a first backup. The idea is to have a backup and to replay WAL files based on that backup to reach any point in time.

To create an initial backup, we can turn to pg_basebackup, which is a command-line tool used to perform backups. Let's call pg_basebackup and see how it works:

pg_basebackup -D /some_target_dir 
-h localhost
--checkpoint=fast
--wal-method=stream

As we can see, we will use four parameters here:

  • -D: Where do we we want the base backup to live? PostgreSQL requires an empty directory. At the end of the backup, we will see a copy of the server's data directory (destination).
  • -h: Indicates the IP address or the name of the master (source). This is the server you want to back up.
  • --checkpoint=fast: Usually, pg_basebackup waits for the master to checkpoint. The reason for this is that the replay process has to start somewhere. A checkpoint ensures that data has been written up to a certain point and so PostgreSQL can safely jump in there and start the replay process. Basically, it can also be done without the --checkpoint=fast parameter. However, it might take a while before pg_basebackup starts to copy data in this case. Checkpoints can be up to one hour apart, which can delay our backups unnecessarily.
  • --wal-method=stream: By default, pg_basebackup connects to the master server and starts copying files over. Now, keep in mind that those files are modified while they are copied. The data reaching the backup is therefore inconsistent. This inconsistency can be repaired during the recovery process using the WAL. The backup itself, however, is not consistent. By adding the --wal- method=stream parameter, it is possible to create a self-contained backup; it can be started directly without replaying the transaction log. This is a nice method if we just want to clone an instance and not use PITR. Fortunately, -wal-method=stream is actually already the default in PostgreSQL 10.0. However, in 9.6 or earlier, it is recommended to use the predecessor, named -xlog-method=stream.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset