Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jn02K-0002Vh-2m for pgsql-general@arkaria.postgresql.org; Sun, 21 Jun 2020 13:29:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jn01m-0006SN-Bh for pgsql-general@arkaria.postgresql.org; Sun, 21 Jun 2020 13:28:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jn01m-0006SG-3w for pgsql-general@lists.postgresql.org; Sun, 21 Jun 2020 13:28:54 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jn01d-0004BE-5F for pgsql-general@lists.postgresql.org; Sun, 21 Jun 2020 13:28:53 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 8A60632CA; Sun, 21 Jun 2020 15:28:43 +0200 (CEST) Date: Sun, 21 Jun 2020 15:28:43 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: The backup API and general purpose backup software Message-ID: <20200621132843.GB21591@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="wq9mPyueHGvFACwf" Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --wq9mPyueHGvFACwf Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable This is inspired by the thread with the subject "Something else about Redo Logs disappearing", but since that thread is already quite long, since I have lost track what exactly "Peter"'s problem is and since his somewhat belligerent tone makes it unappealing to reread the whole thread I'll attempt a fresh start. To make a full backup with the "new" (non-exclusive) API, a software must do the following 1. open a connection to the database 2. invoke pg_start_backup('label', false, false) in the connection from step 1. 3. copy the contents of the data directory to the backup medium 4. invoke pg_stop_backup(false, true) in the connection from step 1. 5. copy the result from the previous step to the backup medium. (It is assumed that any archived WALs are also archived in a manner that they can be restored together with this backup. If this is not the case adding them to the backup would be step 6.) So far so good and writing a program which implements this should not pose a great difficulty. General purpose backup software often assumes that it can perform a backup in three steps: 1. Invoke a pre-backup script. 2. Copy files to the backup medium in such a way that they can be identified as a group and be restored together and without mixing them with data from another backup session. 3, Invoke a post-backup script. (I have to admit that is has been a long time since I've looked at any backup system in any detail. Doubtlessly many have a more complicated model, but I'm fairly confident that this is still the lowest common denominator.) Now we have two problems. The first is that the pre-backup script has to exit before the proper backup can begin, but it also has to open a connection which will stay open during the backup because it will be needed in the post-backup script again. This can be solved by starting a background process which keeps the connection open and providing some communication channel (perhaps a Unix pipe) for the post-backup script to communicate with this background process. A bit awkward, but no great deal. The second problem is that the post-backup script will only be called after the backup is finished. So the information returned by pg_stop_backup cannot be included in the backup which makes the backup useless. This is indeed serious, and I don't see a way around this in this simple model. But there is a workaround: Split the backup. I am assuming that the backup software uses a unique ID to identify each backup and passes that ID to the pre-backup and post-backup script. This ID is used as the label in the call to pg_start_backup().=20 It is also included in the information returned by pg_stop_backup(). So the post-backup script stores that information in a different place (maybe including the ID in the filename(s) to avoid conflicts and for redundancy) and then triggers a backup of that place (or alternatively that can be backed up independently). To restore a backup, you will then also need two steps: 1) Restore the summary information from the second backup. Inspect the backup_label to find the ID of the backup of the data. 2) Restore that backup (3) Put the backup label where it belongs, make sure the archived WALs are accessible and start the database hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --wq9mPyueHGvFACwf Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl7vYIYACgkQ8g5IURL+ KF1ojw/+OE/+JV2FWbiceOheIfFPPYfM1gxXUf+kyVXt/PZid6PHa3V4UACAlz2m ItfbR/kFAEMmPH+mZAWKNiT8RtZ5WNKGdG1ocr/sViI0GA31BpudGz6apefk7Q0W z9nsZQPx4M0Mf34b/ON61s5DwHoj/3ZzsAqMioZHD4Uuf+noieytDyeH8MCVJqCl W10QLKIFGzsvbSWq+386x5bbaLlMYwEmBy8X/GhLkqafZse5Z3xkUyafHd4lxEEP 55a7RUIUF1aODWPvseqfWSDbB7IGYrgBEvOuoL3sjCMSNL5WLC0pGFnyDBlG0lxk sBnFb+RUbtCraguXLxGbCwK/rpPs4LqWZJMFSH1bg+n2DWzttLXcOH073t7Y1L+5 3iFFNqR5fltpFfFgYonVm1/jrb3Er6rVFXcxyizQpFW9FA7tfnCWL1yMTDz7aZhe lLJZmI7UzaZ9RpUZDrlZe6ovItaS0IuaxdLEar5ZaYsm1+wwRCQZZpa3L0VNm9+e tlzjusFzximdCdPK6AGmRlwsBHV9TRaFojPfNiEUr1ezRHaghTU4rtg0OTgGV/6z V83B4CqnDJ0UCkEj6VHvU4E00eU+eAFBbmSPoGCZe74mLb/9NZzrj6Ec1unGtv3R C3fe7sdUbD9FkElRZR+KEiodQno9+kNPZH4rx7L3aIbltSQzvMY= =5EtG -----END PGP SIGNATURE----- --wq9mPyueHGvFACwf--