Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g5DTl-0002mY-0P for pgsql-general@arkaria.postgresql.org; Wed, 26 Sep 2018 17:20:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g5DTi-0005Ud-Q4 for pgsql-general@arkaria.postgresql.org; Wed, 26 Sep 2018 17:19:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g5DTi-0005UW-72 for pgsql-general@lists.postgresql.org; Wed, 26 Sep 2018 17:19:58 +0000 Received: from sonic312-22.consmr.mail.gq1.yahoo.com ([98.137.69.203]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g5DTa-0007vw-OH for pgsql-general@lists.postgresql.org; Wed, 26 Sep 2018 17:19:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1537982387; bh=XSc9pLqLNF61fOV5rjZbyQ99EfXJBqYHnSwUn6JIMhU=; h=Date:From:To:In-Reply-To:References:Subject:From:Subject; b=i+5OkH4VUtsfiYngYs/rKFv4YurK2oK2DXxTEvE6CTgPXOe8au0C+kODI+2kE91ozrIlsSUXPy3C6Vfnpki562C7SLBMJtvXz6e26EpN1V9g9gLnU6OZ1RBwppn1a1dqJ3BqbIQl9thjtVVvHeIeITdYidJ3NRC/QujEpjjVSZmpbe0+KaKlDq3U3GVktYsnQKWLDnjkHJfKU0M91S9CV2X9ib1EPNka820Z0OR05wGsi1LElkDvfl91pYwacy4QKfJZv8RYhhbnTpT2khjmdkytt8/Tfw3dttnXNIKVoQbplzWujhJp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ic.gate.mail.ne1.yahoo.com by sonic312.consmr.mail.gq1.yahoo.com with HTTP; Wed, 26 Sep 2018 17:19:47 +0000 Date: Wed, 26 Sep 2018 17:19:44 +0000 (UTC) From: Pierre Timmermans To: pgsql-general@lists.postgresql.org, Yuri Kanivetsky Message-ID: <307588119.3233604.1537982384823@mail.yahoo.com> In-Reply-To: References: Subject: Re: Setting up continuous archiving MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_3233603_1249900002.1537982384817" X-Mailer: WebService/1.1.12487 YMailNorrin Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 Content-Length: 18642 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_3233603_1249900002.1537982384817 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello What you are doing is called "log shipping", which means that when a wal (w= rite-ahead log) is filled in on the database server you ship it to a backup= server via rsync. It is fine but as you said the disadvantage is that the = file is shipped only when it is full, so you could have data loss (the last= wal not shipped) A more modern and easy way is to use streaming replication: in this case th= e logs are streamed continuously to the standby or to the backup server (on= e can use streaming replication without implementing a standby database). L= ook at the doc on the next page than the one you referred to (https://www.p= ostgresql.org/docs/9.3/static/high-availability.html) There is a nice tool that does one you plan to do (rsync of archived file) = but also the more modern way (streaming replication): it is called barman:= =C2=A0https://www.pgbarman.org/. You should probably use their tool but you= can also read the doc to get the concepts and some ideas Rgds, Pierre=20 On Wednesday, September 26, 2018, 9:21:29 AM GMT+2, Yuri Kanivetsky wrote: =20 =20 Hi, I'm trying to compile a basic set of instruction needed to set up continuous archiving and to recover from a backup. I'm running PostgreSQL 9.3 on Debian Stretch system. I've got a database and a backup servers. The idea is to set up WAL archiving, and occasionally do full (base) backups. A base backup plus WAL segment files from that point onward must provide enough information to restore database to the latest state. Except for a window for data loss that is caused by the fact that WAL segment files aren't transferred momentarily, and more importantly that WAL segment files are only transferred upon being filled. --- Setting up continuous archiving * Set up WAL archiving =C2=A0 =C2=A0 * on backup server under postgres user =C2=A0 =C2=A0 =C2=A0 =C2=A0 * create /var/lib/postgresql/wal_archive dir =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ mkdir /var/lib/postgresql/wal_archive =C2=A0 =C2=A0 * on database server under postgres user =C2=A0 =C2=A0 =C2=A0 =C2=A0 * generate ssh key =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ ssh-keygen -f /var/lib/postgresql/.ssh/id_rsa= =C2=A0 # providing path to key file makes it =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # to not ask questi= ons =C2=A0 =C2=A0 =C2=A0 =C2=A0 * add corresponding record to known_hosts file =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ ssh-keyscan -t rsa BACKUP_SRV >> ~/.ssh/known= _hosts =C2=A0 =C2=A0 * locally =C2=A0 =C2=A0 =C2=A0 =C2=A0 * authorize login from database to backup serve= r =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ ssh DATABASE_SRV cat ~/.ssh/id_rsa.pub | ssh = BACKUP_SRV 'mkdir --mode 0700 .ssh; cat >> ~/.ssh/authorized_keys; chmod 0600 .ssh/authorized_keys' =C2=A0 =C2=A0 * on database server under root =C2=A0 =C2=A0 =C2=A0 =C2=A0 * change postgresql.conf =C2=A0 =C2=A0 =C2=A0 =C2=A0 wal_level =3D archive =C2=A0 =C2=A0 =C2=A0 =C2=A0 archive_mode =3D on =C2=A0 =C2=A0 =C2=A0 =C2=A0 archive_command =3D 'rsync -a %p BACKUP_SRV:/var/lib/postgresql/wal_archive/%f' =C2=A0 =C2=A0 =C2=A0 =C2=A0 * restart PostgreSQL =C2=A0 =C2=A0 =C2=A0 =C2=A0 # systemctl resart postgresql * Make a base backup =C2=A0 =C2=A0 * on database server under root =C2=A0 =C2=A0 =C2=A0 =C2=A0 * add a line to postgresql.conf =C2=A0 =C2=A0 =C2=A0 =C2=A0 max_wal_senders =3D 1 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * add a line to pg_hba.conf =C2=A0 =C2=A0 =C2=A0 =C2=A0 host=C2=A0 replication=C2=A0 replication=C2=A0 = BACKUP_SRV_IP/BACKUP_SRV_NETMASK=C2=A0 trust =C2=A0 =C2=A0 =C2=A0 =C2=A0 * restart PostgreSQL =C2=A0 =C2=A0 =C2=A0 =C2=A0 # systemctl restart postgresql =C2=A0 =C2=A0 * on database server under postgres user =C2=A0 =C2=A0 =C2=A0 =C2=A0 * create replication user =C2=A0 =C2=A0 =C2=A0 =C2=A0 CREATE USER replication WITH REPLICATION; =C2=A0 =C2=A0 =C2=A0 =C2=A0 or =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ createuser --replication replication =C2=A0 =C2=A0 * on backup server under postgres user =C2=A0 =C2=A0 =C2=A0 =C2=A0 * make base backup =C2=A0 =C2=A0 =C2=A0 =C2=A0 $ pg_basebackup -h DATABASE_SRV -U replication = -D /var/lib/postgresql/base_backups/$(date +%Y%m%d-%H%M%S) Restoring from a backup * under root =C2=A0 =C2=A0 * stop PostgreSQL if running =C2=A0 =C2=A0 # systemctl stop postgresql * under postgres user =C2=A0 =C2=A0 * move data dir =C2=A0 =C2=A0 $ mv 9.3{,-$(date +%Y%m%d-%H%M%S)} =C2=A0 =C2=A0 * copy backup =C2=A0 =C2=A0 $ mkdir 9.3 =C2=A0 =C2=A0 $ cp -r base_backups/TIMESTAMP 9.3/main =C2=A0 =C2=A0 * copy unarchived segment files =C2=A0 =C2=A0 $ find 9.3-TIMESTAMP/main/pg_xlog -maxdepth 1 -type f -exec c= p -t 9.3/main/pg_xlog {} + =C2=A0 =C2=A0 * create recovery.conf in 9.3/main =C2=A0 =C2=A0 restore_command =3D 'cp /var/lib/postgresql/wal_archive/%f %p= ' * under root =C2=A0 =C2=A0 * start PostgreSQL =C2=A0 =C2=A0 # systemctl start postgresql A few notes. Running out of space on backup server can lead in its turn to database server running out of space, since WAL segment files stop being archived and keep piling up. The same might happen when archiving falls behind. Which also widens the window for data loss. WAL archiving doesn't track changes to configuration files. pg_basebackup will back up configuration files only if they are inside data dir. If database doesn't generate much WAL traffic, there could be a long delay between completing a transaction and archiving its results (archive_timeout). You might want to prevent users from accessing the database until you're sure the recovery was successful (pg_hba.conf). I'm not considering here possible issues with tablespaces and other caveats= : https://www.postgresql.org/docs/9.3/static/continuous-archiving.html#CONTIN= UOUS-ARCHIVING-CAVEATS --- Most importantly, does it makes sense to keep more than one base backup? Also, does it look any good? Does it make sense to make ~/wal_archive and ~/base_backups dirs not readable by group and the world? From what I can see files in ~/wal_archive are 0600, ~/base_backups/TIMESTAMP is 0700. How can I confirm that it's working properly? Is WAL segments files appearing in ~/wal_archive enough? Thanks in advance. Regards, Yuri Kanivetsky =20 ------=_Part_3233603_1249900002.1537982384817 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello

What you are doing is called "l= og shipping", which means that when a wal (write-ahead log) is filled in on= the database server you ship it to a backup server via rsync. It is fine b= ut as you said the disadvantage is that the file is shipped only when it is= full, so you could have data loss (the last wal not shipped)
A more modern and easy way is to use streaming replication: in = this case the logs are streamed continuously to the standby or to the backu= p server (one can use streaming replication without implementing a standby = database). Look at the doc on the next page than the one you referred to (<= a href=3D"https://www.postgresql.org/docs/9.3/static/high-availability.html= " rel=3D"nofollow" target=3D"_blank">https://www.postgresql.org/docs/9.3/st= atic/high-availability.html)

There is a nice t= ool that does one you plan to do (rsync of archived file) but also the more= modern way (streaming replication): it is called barman: https://www.pg= barman.org/. You should probably use their tool but you can also read t= he doc to get the concepts and some ideas

Rgds, Pierre


=20
=20
On Wednesday, September 26, 2018, 9:21:29 AM GMT+2, Yur= i Kanivetsky <yuri.kanivetsky@gmail.com> wrote:


Hi,

I'm trying to compile a basic set of instruction needed= to set up
continuous archiving and to recover fr= om a backup. I'm running
PostgreSQL 9.3 on Debian= Stretch system.

I've = got a database and a backup servers. The idea is to set up WAL
archiving, and occasionally do full (base) backups. A base ba= ckup plus
WAL segment files from that point onwar= d must provide enough
information to restore data= base to the latest state. Except for a
window for= data loss that is caused by the fact that WAL segment files
aren't transferred momentarily, and more importantly that WAL s= egment
files are only transferred upon being fill= ed.

---

Setting up continuous archiving
<= /div>

* Set up WAL archiving
=

    * on backup= server under postgres user

        * create /var/lib/postgresql/wal_archive = dir

    &nbs= p;   $ mkdir /var/lib/postgresql/wal_archive

    * on database server under postgr= es user

    =     * generate ssh key

        $ ssh-keygen -f /var/lib/postgresql/= .ssh/id_rsa  # providing
path to key file m= akes it
           =                     &nbs= p;                     &n= bsp;   # to not ask questions

        * add corresponding record to know= n_hosts file

  &n= bsp;     $ ssh-keyscan -t rsa BACKUP_SRV >> ~/.ssh/known_ho= sts

    * lo= cally

    &n= bsp;   * authorize login from database to backup server

        $ ssh DA= TABASE_SRV cat ~/.ssh/id_rsa.pub | ssh BACKUP_SRV
'mkdir --mode 0700 .ssh; cat >> ~/.ssh/authorized_keys; chmod 0600
.ssh/authorized_keys'
    * on database server under root
<= /div>

       = ; * change postgresql.conf

        wal_level =3D archive
        archive_mode =3D on
        archive_command =3D 'rsync -a %p
=
BACKUP_SRV:/var/lib/postgresql/wal_archive/%f'

        * res= tart PostgreSQL

 =       # systemctl resart postgresql

* Make a base backup

    * on database server under root<= br>

      &= nbsp; * add a line to postgresql.conf

<= div dir=3D"ltr">        max_wal_senders =3D 1
=

        * a= dd a line to pg_hba.conf

        host  replication  replication&nbs= p; BACKUP_SRV_IP/BACKUP_SRV_NETMASK  trust
<= br>
        * restart PostgreSQL<= br>

      &= nbsp; # systemctl restart postgresql

    * on database server under postgres user

       = * create replication user

        CREATE USER replication WITH REPLICATION;<= br>

      &= nbsp; or

   =     $ createuser --replication replication

    * on backup server under post= gres user

   = ;     * make base backup

        $ pg_basebackup -h DATABASE_SRV -U= replication -D
/var/lib/postgresql/base_backups/= $(date +%Y%m%d-%H%M%S)

Restoring from a backup

* under root

 =   * stop PostgreSQL if running

    # systemctl stop postgresql

* under postgres user

    * move data dir
=

    $ mv 9.3{,-$(date= +%Y%m%d-%H%M%S)}

&nbs= p;   * copy backup

    $ mkdir 9.3
    $ cp -r= base_backups/TIMESTAMP 9.3/main

    * copy unarchived segment files

    $ find 9.3-TIMESTAMP/main= /pg_xlog -maxdepth 1 -type f -exec cp -t
9.3/main= /pg_xlog {} +

  &= nbsp; * create recovery.conf in 9.3/main

    restore_command =3D 'cp /var/lib/postgresq= l/wal_archive/%f %p'

*= under root

  &nb= sp; * start PostgreSQL

    # systemctl start postgresql

<= /div>
A few notes.

Running out of space on backup server can lead in its turn to = database
server running out of space, since WAL s= egment files stop being
archived and keep piling = up. The same might happen when archiving
falls be= hind. Which also widens the window for data loss.

WAL archiving doesn't track changes to configur= ation files.
pg_basebackup will back up configura= tion files only if they are inside
data dir.
<= /div>

If database doesn't genera= te much WAL traffic, there could be a long
delay = between completing a transaction and archiving its results
(archive_timeout).

You might want to prevent users from accessing the database until<= br>
you're sure the recovery was successful (pg_hba.c= onf).

I'm not consider= ing here possible issues with tablespaces and other caveats:


---

Most importantly, does it makes sense to keep more tha= n one base backup?

Als= o, does it look any good? Does it make sense to make ~/wal_archive
and ~/base_backups dirs not readable by group and the wor= ld? From what
I can see files in ~/wal_archive ar= e 0600, ~/base_backups/TIMESTAMP is
0700. How can= I confirm that it's working properly? Is WAL segments
files appearing in ~/wal_archive enough?
Thanks in advance.

<= /div>
Regards,
Yuri Kanivetsky

------=_Part_3233603_1249900002.1537982384817--