Thursday, December 29, 2022

Using nmonchart and python to visualize the nmon output from linux server

 Objective: In the previous blog, we saw how to collect nmon data and convert the same to html files. Now we will see how we can host it in a web.

To host the nmon charts using a simple solution follow the below steps...

1) Let us recall the directory structure first...

Directory structure:
Nmon data loc: /pgBACKUP/nmondata
nmonchart bin: /pgBACKUP/nmonchartbin/nmonchart
nmonchart dest: /pgBACKUP/nmondata/nmonchartop

Let us one more refinement...
We will create one directory per server in nmonchart dest, this means we can use one url from one machine (using a remote nfs, we can collect data from all the servers) to access multiple machines chart.

Directory structure:
Nmon data loc: /pgBACKUP/nmondata
nmonchart bin: /pgBACKUP/nmonchartbin/nmonchart
nmonchart dest for 10.0.2.7 machine: /pgBACKUP/nmondata/nmonchartop/10.0.2.7/
nmonchart dest for 10.0.2.8 machine: /pgBACKUP/nmondata/nmonchartop/10.0.2.8/
nmonchart dest for 10.0.2.9 machine: /pgBACKUP/nmondata/nmonchartop/10.0.2.9/

So how it looks:

[root@localhost 10.0.2.8]# ls -altr
total 448
-rw-r--r--. 1 root root 457432 Dec 29 18:26 localhost_221229_1659.html
drwxr-xr-x. 2 root root     40 Dec 29 18:33 .
drwxr-xr-x. 3 root root     22 Dec 29 18:39 ..
[root@localhost 10.0.2.8]# pwd
/pgBACKUP/nmondata/nmonchartop/10.0.2.8
[root@localhost 10.0.2.8]


2) All we need now is python, if we already have python installed, you can just start a simple webpage from the location you have the host wise directory structure.


[root@localhost vagrant]# cd /pgBACKUP/nmondata/nmonchartop
[root@localhost nmonchartop]# ls -altr
total 0
drwxr-xr-x. 3 root root 193 Dec 29 18:13 ..
drwxr-xr-x. 2 root root  40 Dec 29 18:33 10.0.2.8
drwxr-xr-x. 3 root root  22 Dec 29 18:39 .
[root@localhost nmonchartop]# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
>>>> now our website is up.


3) Before trying to access the web, please be sure to exempt the 8000 port from firewall or for time being stop it completely.


4) In case your testing this in a virtual box using NatNetwork, please do add port forwarding like below...

127.0.0.1:8000 -> 10.0.2.8:80000

5) Then try access the url...

http://127.0.0.1:8000/



6) Navigate through the url.






7) Observe the python command line output for incoming accesses....

[root@localhost nmonchartop]# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
10.0.2.2 - - [29/Dec/2022 18:46:01] "GET / HTTP/1.1" 200 -
10.0.2.2 - - [29/Dec/2022 18:46:46] "GET /10.0.2.8/ HTTP/1.1" 200 -
10.0.2.2 - - [29/Dec/2022 18:47:22] "GET /10.0.2.8/localhost_221229_1659.html HTTP/1.1" 200 -


So now we have the data accessible from web url.

Thank you!

---this closes the blog.


Using nmon in linux to collect performance data

Objective:  Let us nmon to collect performance data from linux using the frequency as we need.

Step 1 - nmon installation in linux:

Search for nmon in repo:
[root@localhost ~]# yum search nmon
Loaded plugins: auto-update-debuginfo, fastestmirror
Loading mirror speeds from cached hostfile
 * base: anorien.csc.warwick.ac.uk
 * epel: mirrors.xtom.de
 * epel-debuginfo: mirrors.xtom.de
 * extras: uk.mirrors.clouvider.net
 * updates: mirrors.coreix.net
============================================== N/S matched: nmon ==============================================
nmon-debuginfo.x86_64 : Debug information for package nmon
xfce4-genmon-plugin-debuginfo.x86_64 : Debug information for package xfce4-genmon-plugin
conmon.x86_64 : OCI container runtime monitor
nmon.x86_64 : Nigel's performance Monitor for Linux                         <<<<<<<<< this is what we need
xfce4-genmon-plugin.x86_64 : Generic monitor plugin for the Xfce panel
  Name and summary matches only, use "search all" for everything.
[root@localhost ~]#
Installing nmon:
[root@localhost ~]# yum install nmon.x86_64
Loaded plugins: auto-update-debuginfo, fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                    |  26 kB  00:00:00
epel-debuginfo/x86_64/metalink                                                          |  26 kB  00:00:00
 * base: anorien.csc.warwick.ac.uk
 * epel: mirrors.xtom.de
 * epel-debuginfo: mirrors.xtom.de
 * extras: mirror.freethought-internet.co.uk
 * updates: mirror.bytemark.co.uk
base                                                                                    | 3.6 kB  00:00:00
base-debuginfo                                                                          | 2.5 kB  00:00:00
epel                                                                                    | 4.7 kB  00:00:00
epel-debuginfo                                                                          | 3.0 kB  00:00:00
extras                                                                                  | 2.9 kB  00:00:00
pgdg-common/7/x86_64/signature                                                          |  198 B  00:00:00
...
updates                                                                                 | 2.9 kB  00:00:00
(1/4): epel/x86_64/group_gz                                                             |  99 kB  00:00:00
(2/4): epel/x86_64/updateinfo                                                           | 1.0 MB  00:00:00
(3/4): epel/x86_64/primary_db                                                           | 7.0 MB  00:00:00
(4/4): epel-debuginfo/x86_64/primary_db                                                 | 901 kB  00:00:05
Resolving Dependencies
--> Running transaction check
---> Package nmon.x86_64 0:16g-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================
 Package                 Arch                      Version                       Repository               Size
===============================================================================================================
Installing:
 nmon                    x86_64                    16g-3.el7                     epel                     70 k
Transaction Summary
===============================================================================================================
Install  1 Package
Total download size: 70 k
Installed size: 156 k
Is this ok [y/d/N]: y
Downloading packages:
nmon-16g-3.el7.x86_64.rpm                                                               |  70 kB  00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : nmon-16g-3.el7.x86_64                                                                       1/1
  Verifying  : nmon-16g-3.el7.x86_64                                                                       1/1
Installed:
  nmon.x86_64 0:16g-3.el7
Complete!
[root@localhost ~]#


Launch nmon:

Test launch of nmon


lnmonq16gqqqqqqqqqqqqqqqqqqqqqHostname=localhostqqqqRefresh= 2secs qqq06:46.18qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x                                                                                                             x
x   ------------------------------                                                                            x
x    _ __  _ __ ___   ___  _ __          For help type H or ...                                               x
x   | '_ \| '_ ` _ \ / _ \| '_ \          nmon -?  - hint                                                     x
x   | | | | | | | | | (_) | | | |         nmon -h  - full details                                             x
x   |_| |_|_| |_| |_|\___/|_| |_|                                                                             x
x                                        To stop nmon type q to Quit                                          x
x   ------------------------------                                                                            x
x                                                                                                             x
x   CentOS Linux release 7.9.2009 (Core) VERSION="7 (Core)"                                                   x
x   Vendor=GenuineIntel Model=11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz                                  x
x   MHz=1382.406 bogomips=2764.81          lscpu:CPU=1 Little Endian                                          x
x   ProcessorChips=1 PhyscalCores=1              Sockets=1 Cores=1 Thrds=1                                    x
x                    VirtualCPUs =1              MHz=1382 max=0 min=0                                         x
x                                                                                                             x
x   Use these keys to toggle statistics on/off:                                                               x
x     c = CPU         l = CPU Long-term     - = Faster screen updates                                         x
x     C = " WideView  U = Utilisation       + = Slower screen updates                                         x
x     m = Memory      V = Virtual memory    j = File Systems                                                  x
x     d = Disks       n = Network           . = only busy disks/procs                                         x
x     r = Resource    N = NFS               h = more options                                                  x
x     k = Kernel      t = Top-processes     q = Quit                                                          x
xqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
x                                                                                                             x
x                                                                                                             x
x                                                                                                             x
x                                                                                                             x
x                                                                                                             x
x                                                                                                             x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
Check Cpu usage:
lnmonq16gqqqqqqqqqqqqqqqqqqqqqHostname=localhostqqqqRefresh= 2secs qqq06:47.01qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x CPU Utilisation qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
x---------------------------+-------------------------------------------------+                               x
xCPU User%  Sys% Wait%  Idle|0          |25         |50          |75       100|                               x
x  1   0.0   0.0   0.0 100.0|>                                                |                               x
x---------------------------+-------------------------------------------------+                               x
xqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
x
Cpu and network togather:
lnmonq16gqqqqqq[H for help]qqqHostname=localhostqqqqRefresh= 2secs qqq06:47.10qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk
x CPU Utilisation qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
x---------------------------+-------------------------------------------------+                               x
xCPU User%  Sys% Wait%  Idle|0          |25         |50          |75       100|                               x
x  1   0.0   1.0   0.0  99.0|>                                                |                               x
x---------------------------+-------------------------------------------------+                               x
x Network I/O qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
xI/F Name Recv=KB/s Trans=KB/s packin packout insize outsize Peak->Recv Trans                                 x
x  enp0s3      0.2       4.4       2.0    3.5    98.5 1284.6        1.0     13.9                              x
x      lo      0.0       0.0       0.0    0.0     0.0    0.0        0.0      0.0                              x
x Network Error Counters qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
xI/F Name iErrors iDrop iOverrun iFrame oErrors   oDrop oOverrun oCarrier oColls                              x
x  enp0s3       0       0       0       0       0       0       0       0       0                             x
x      lo       0       0       0       0       0       0       0       0       0                             x
xqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqx
x


Now let us schedule nmon in cron to collect data every 10 secs for 360 times, which means 1hr:


59 * * * * cd /pgBACKUP/nmondata && nmon -f -tT -s10 -c360 1>/dev/null 2>&1


Resultant files are:
-rw-r--r--. 1 root     root     112123 Dec 29 18:13 localhost_221229_1759.nmon
-rw-r--r--. 1 root     root     335301 Dec 29 18:13 localhost_221229_1659.nmon
[root@localhost nmondata]# head localhost_221229_1759.nmon
AAA,progname,nmon
AAA,command,nmon -f -tT -s10 -c360
AAA,version,16g
AAA,disks_per_line,150
AAA,max_disks,256,set by -d option
AAA,disks,14,
AAA,host,localhost
AAA,user,root
AAA,OS,Linux,3.10.0-1160.6.1.el7.x86_64,#1 SMP Tue Nov 17 13:59:11 UTC 2020,x86_64
AAA,runname,localhost
[root@localhost nmondata]#


Also notice the next nmon sampler is running from cron... there is one nmon at any time; as per our setup. After 1hr the existing nmon will die and the next nmon will kick off.


[root@localhost nmondata]# ps -ef|grep -i nmon
root      6276     1  0 17:59 ?        00:00:01 nmon -f -tT -s10 -c360
root      7710  1770  0 18:22 pts/0    00:00:00 grep --color=auto -i nmon
[root@localhost nmondata]#


Now let us look how we can host this as a webpage...


Directory structure:
Nmon data loc: /pgBACKUP/nmondata
nmonchart bin: /pgBACKUP/nmonchartbin/nmonchart
nmonchart dest: /pgBACKUP/nmondata/nmonchartop

So we need to use nmonchart to push the output charts over to the dest location...
Prereqs: You need ksh


[root@localhost nmondata]# ksh
# pwd
/pgBACKUP/nmondata
#

/pgBACKUP/nmonchartbin/nmonchart localhost_221229_1659.nmon ./nmonchartop/localhost_221229_1659.html
Actual:
# /pgBACKUP/nmonchartbin/nmonchart localhost_221229_1659.nmon ./nmonchartop/localhost_221229_1659.html
#
[root@localhost nmondata]# cd nmonchartop/
[root@localhost nmonchartop]# ls -altr
total 448
drwxr-xr-x. 3 root root    193 Dec 29 18:13 ..
drwxr-xr-x. 2 root root     40 Dec 29 18:26 .
-rw-r--r--. 1 root root 457432 Dec 29 18:26 localhost_221229_1659.html
[root@localhost nmonchartop]# pwd
/pgBACKUP/nmondata/nmonchartop
[root@localhost nmonchartop]#

So the html output is available here. We just need to host it.
Let us try to use simple solution like python.
>>>next blog>>>

Wednesday, December 28, 2022

PostgreSQL - Barman backup to a location different from the default one /var/lib/barman

Backup PostgreSQL database using BARMAN to a location different from the default location

Objective: 

Update the postgresql backup location in barman server;

We are looking to migrate the backups from default barman user's home directory to a dedicated filesystem where the backups will be hosted.

For instance /var/lib/barman to /var/lib/barman/pgbackups

/var/lib/barman is a dedicated mountpoint for barman users home.

/var/lib/barman/pgbackups which is mounted on top of this directory acts as dedicated backup destination.

You are welcome to choose a seperate mountpath as well.

Steps:

Before the change server config:

-bash-4.2$ barman show-servers primarypostgresql15
Server primarypostgresql15:
        active: True
        archive_timeout: 0
        archiver: False
        archiver_batch_size: 0
        backup_compression: None
        backup_compression_format: None
        backup_compression_level: None
        backup_compression_location: None
        backup_compression_workers: None
        backup_directory: /var/lib/barman/primarypostgresql15
        backup_method: postgres
        backup_options: BackupOptions(['concurrent_backup'])
        bandwidth_limit: None
        barman_home: /var/lib/barman
        barman_lock_directory: /var/lib/barman
        basebackup_retry_sleep: 30
        basebackup_retry_times: 0
        basebackups_directory: /var/lib/barman/primarypostgresql15/base
        check_timeout: 30
        checkpoint_timeout: 300
        compression: pigz
        config_file: /pgDATA/data/postgresql.conf
        connection_error: None
        conninfo: host=10.0.2.8 user=barman password=*REDACTED* dbname=postgres port=5432
        create_slot: auto
        current_lsn: 0/9A0000D8
        current_size: 1259817117
        current_xlog: 00000001000000000000009A
        custom_compression_filter: None
        custom_compression_magic: None
        custom_decompression_filter: None
        data_checksums: off
        data_directory: /pgDATA/data
        description: Primary PostgreSQL Server Backup
        disabled: False
        errors_directory: /var/lib/barman/primarypostgresql15/errors
        forward_config_path: False
        has_backup_privileges: True
        hba_file: /pgDATA/data/pg_hba.conf
        hot_standby: on
        ident_file: /pgDATA/data/pg_ident.conf
        immediate_checkpoint: False
        incoming_wals_directory: /var/lib/barman/primarypostgresql15/incoming
        is_in_recovery: False
        is_superuser: True
        last_backup_maximum_age: None
        last_backup_minimum_size: None
        last_wal_maximum_age: None
        max_incoming_wals_queue: None
        max_replication_slots: 10
        max_wal_senders: 10
        minimum_redundancy: 0
        msg_list: []
        name: primarypostgresql15
        network_compression: False
        parallel_jobs: 4
        passive_node: False
        path_prefix: "/usr/pgsql-15/bin" # this is needed for streaming archiving, pg_walrecieve command is supplied by postgresql server binary, hence we are installing the postgresql server binary in the barman server
        pg_basebackup_bwlimit: True
        pg_basebackup_compatible: True
        pg_basebackup_installed: True
        pg_basebackup_path: /bin/pg_basebackup
        pg_basebackup_tbls_mapping: True
        pg_basebackup_version: 15.1
        pg_receivexlog_compatible: True
        pg_receivexlog_installed: True
        pg_receivexlog_path: /usr/pgsql-15/bin/pg_receivewal
        pg_receivexlog_supports_slots: True
        pg_receivexlog_synchronous: False
        pg_receivexlog_version: 15.1
        pgespresso_installed: False
        post_archive_retry_script: None
        post_archive_script: None
        post_backup_retry_script: None
        post_backup_script: None
        post_delete_retry_script: None
        post_delete_script: None
        post_recovery_retry_script: None
        post_recovery_script: None
        post_wal_delete_retry_script: None
        post_wal_delete_script: None
        postgres_systemid: 7163531269528114626
        pre_archive_retry_script: None
        pre_archive_script: None
        pre_backup_retry_script: None
        pre_backup_script: None
        pre_delete_retry_script: None
        pre_delete_script: None
        pre_recovery_retry_script: None
        pre_recovery_script: None
        pre_wal_delete_retry_script: None
        pre_wal_delete_script: None
        primary_conninfo: None
        primary_ssh_command: None
        recovery_options: RecoveryOptions([])
        recovery_staging_path: None
        replication_slot: Record(slot_name='barman', active=False, restart_lsn='0/94000000')
        replication_slot_support: True
        retention_policy: RECOVERY WINDOW OF 4 WEEKS
        retention_policy_mode: auto
        reuse_backup: None
        server_txt_version: 15.0
        slot_name: barman
        ssh_command: None
        streaming: True
        streaming_archiver: True
        streaming_archiver_batch_size: 0
        streaming_archiver_name: barman_receive_wal
        streaming_backup_name: barman_streaming_backup
        streaming_conninfo: host=10.0.2.8 user=streaming_barman password=*REDACTED* port=5432
        streaming_supported: True
        streaming_systemid: 7163531269528114626
        streaming_wals_directory: /var/lib/barman/primarypostgresql15/streaming
        synchronous_standby_names: ['']
        tablespace_bandwidth_limit: None
        timeline: 1
        wal_compression: off
        wal_keep_size: 1GB
        wal_level: replica
        wal_retention_policy: MAIN
        wals_directory: /var/lib/barman/primarypostgresql15/wals
        xlog_segment_size: 16777216
        xlogpos: 0/9A0000D8
You have new mail in /var/spool/mail/barman
-bash-4.2


List of directories relying on: /var/lib/barman [which is where our backups are hosted]


Line 16:         backup_directory: /var/lib/barman/primarypostgresql15

Line 20:         barman_home: /var/lib/barman

Line 21:         barman_lock_directory: /var/lib/barman

Line 24:         basebackups_directory: /var/lib/barman/primarypostgresql15/base

Line 42:         errors_directory: /var/lib/barman/primarypostgresql15/errors

Line 49:         incoming_wals_directory: /var/lib/barman/primarypostgresql15/incoming

Line 119:         streaming_wals_directory: /var/lib/barman/primarypostgresql15/streaming

Line 127:         wals_directory: /var/lib/barman/primarypostgresql15/wals


Change server config in the conf file:

[root@localhost barman.d]# diff primarypostgresql15.conf primarypostgresql15.conf_BKP
11,12d10
< ;to alter backup location
< backup_directory = /var/lib/barman/pgbackups/primarypostgresql15
[root@localhost barman.d]#


After the change of server config:

-bash-4.2$ barman show-servers primarypostgresql15
Server primarypostgresql15:
        active: True
        archive_timeout: 0
        archiver: False
        archiver_batch_size: 0
        backup_compression: None
        backup_compression_format: None
        backup_compression_level: None
        backup_compression_location: None
        backup_compression_workers: None
        backup_directory: /var/lib/barman/pgbackups/primarypostgresql15
        backup_method: postgres
        backup_options: BackupOptions(['concurrent_backup'])
        bandwidth_limit: None
        barman_home: /var/lib/barman
        barman_lock_directory: /var/lib/barman
        basebackup_retry_sleep: 30
        basebackup_retry_times: 0
        basebackups_directory: /var/lib/barman/pgbackups/primarypostgresql15/base
        check_timeout: 30
        checkpoint_timeout: 300
        compression: pigz
        config_file: /pgDATA/data/postgresql.conf
        connection_error: None
        conninfo: host=10.0.2.8 user=barman password=*REDACTED* dbname=postgres port=5432
        create_slot: auto
        current_lsn: 0/9A0001C0
        current_size: 1259817117
        current_xlog: 00000001000000000000009A
        custom_compression_filter: None
        custom_compression_magic: None
        custom_decompression_filter: None
        data_checksums: off
        data_directory: /pgDATA/data
        description: Primary PostgreSQL Server Backup
        disabled: False
        errors_directory: /var/lib/barman/pgbackups/primarypostgresql15/errors
        forward_config_path: False
        has_backup_privileges: True
        hba_file: /pgDATA/data/pg_hba.conf
        hot_standby: on
        ident_file: /pgDATA/data/pg_ident.conf
        immediate_checkpoint: False
        incoming_wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/incoming
        is_in_recovery: False
        is_superuser: True
        last_backup_maximum_age: None
        last_backup_minimum_size: None
        last_wal_maximum_age: None
        max_incoming_wals_queue: None
        max_replication_slots: 10
        max_wal_senders: 10
        minimum_redundancy: 0
        msg_list: []
        name: primarypostgresql15
        network_compression: False
        parallel_jobs: 4
        passive_node: False
        path_prefix: "/usr/pgsql-15/bin" # this is needed for streaming archiving, pg_walrecieve command is supplied by postgresql server binary, hence we are installing the postgresql server binary in the barman server
        pg_basebackup_bwlimit: True
        pg_basebackup_compatible: True
        pg_basebackup_installed: True
        pg_basebackup_path: /bin/pg_basebackup
        pg_basebackup_tbls_mapping: True
        pg_basebackup_version: 15.1
        pg_receivexlog_compatible: True
        pg_receivexlog_installed: True
        pg_receivexlog_path: /usr/pgsql-15/bin/pg_receivewal
        pg_receivexlog_supports_slots: True
        pg_receivexlog_synchronous: False
        pg_receivexlog_version: 15.1
        pgespresso_installed: False
        post_archive_retry_script: None
        post_archive_script: None
        post_backup_retry_script: None
        post_backup_script: None
        post_delete_retry_script: None
        post_delete_script: None
        post_recovery_retry_script: None
        post_recovery_script: None
        post_wal_delete_retry_script: None
        post_wal_delete_script: None
        postgres_systemid: 7163531269528114626
        pre_archive_retry_script: None
        pre_archive_script: None
        pre_backup_retry_script: None
        pre_backup_script: None
        pre_delete_retry_script: None
        pre_delete_script: None
        pre_recovery_retry_script: None
        pre_recovery_script: None
        pre_wal_delete_retry_script: None
        pre_wal_delete_script: None
        primary_conninfo: None
        primary_ssh_command: None
        recovery_options: RecoveryOptions([])
        recovery_staging_path: None
        replication_slot: Record(slot_name='barman', active=True, restart_lsn='0/9A000000')
        replication_slot_support: True
        retention_policy: RECOVERY WINDOW OF 4 WEEKS
        retention_policy_mode: auto
        reuse_backup: None
        server_txt_version: 15.0
        slot_name: barman
        ssh_command: None
        streaming: True
        streaming_archiver: True
        streaming_archiver_batch_size: 0
        streaming_archiver_name: barman_receive_wal
        streaming_backup_name: barman_streaming_backup
        streaming_conninfo: host=10.0.2.8 user=streaming_barman password=*REDACTED* port=5432
        streaming_supported: True
        streaming_systemid: 7163531269528114626
        streaming_wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/streaming
        synchronous_standby_names: ['']
        tablespace_bandwidth_limit: None
        timeline: 1
        wal_compression: off
        wal_keep_size: 1GB
        wal_level: replica
        wal_retention_policy: MAIN
        wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/wals
        xlog_segment_size: 16777216
        xlogpos: 0/9A0001C0
-bash-4.2$


What all server config were impacted by the change:

-bash-4.2$ diff prechng.out postchng.out
12c12
<         backup_directory: /var/lib/barman/primarypostgresql15
---
>         backup_directory: /var/lib/barman/pgbackups/primarypostgresql15
20c20
<         basebackups_directory: /var/lib/barman/primarypostgresql15/base
---
>         basebackups_directory: /var/lib/barman/pgbackups/primarypostgresql15/base
28c28
<         current_lsn: 0/9A0000D8
---
>         current_lsn: 0/9A0001C0
38c38
<         errors_directory: /var/lib/barman/primarypostgresql15/errors
---
>         errors_directory: /var/lib/barman/pgbackups/primarypostgresql15/errors
45c45
<         incoming_wals_directory: /var/lib/barman/primarypostgresql15/incoming
---
>         incoming_wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/incoming
99c99
<         replication_slot: Record(slot_name='barman', active=False, restart_lsn='0/94000000')
---
>         replication_slot: Record(slot_name='barman', active=True, restart_lsn='0/9A000000')
115c115
<         streaming_wals_directory: /var/lib/barman/primarypostgresql15/streaming
---
>         streaming_wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/streaming
123c123
<         wals_directory: /var/lib/barman/primarypostgresql15/wals
---
>         wals_directory: /var/lib/barman/pgbackups/primarypostgresql15/wals
125,127c125,126
<         xlogpos: 0/9A0000D8
< You have new mail in /var/spool/mail/barman
< -bash-4.2
---
>         xlogpos: 0/9A0001C0
> -bash-4.2$
-bash-4.2$

Confirmation of the changes: <NO CH> - No changes. <OK> - changed as expected

Line 16:         backup_directory: /var/lib/barman/primarypostgresql15 <OK>
Line 20:         barman_home: /var/lib/barman <NO CH>
Line 21:         barman_lock_directory: /var/lib/barman <NO CH>
Line 24:         basebackups_directory: /var/lib/barman/primarypostgresql15/base <OK>
Line 42:         errors_directory: /var/lib/barman/primarypostgresql15/errors <OK>
Line 49:         incoming_wals_directory: /var/lib/barman/primarypostgresql15/incoming <OK>
Line 119:         streaming_wals_directory: /var/lib/barman/primarypostgresql15/streaming <OK>
Line 127:         wals_directory: /var/lib/barman/primarypostgresql15/wals <OK>

The objective was to move the actual backup content, which occupies space to a dedicated mountpoint rather than keeping them in barman's home directory.

This seem to be achieved now.


A test backup routine:

-bash-4.2$ barman backup primarypostgresql15
Starting backup using postgres method for server primarypostgresql15 in /var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949
Backup start at LSN: 0/9C000060 (00000001000000000000009C, 00000060)
Starting backup copy via pg_basebackup for 20221228T081949
Copy done (time: 1 minute, 9 seconds)
Finalising the backup.
This is the first backup for server primarypostgresql15
WAL segments preceding the current backup have been found:
        000000010000000000000095 from server primarypostgresql15 has been removed
        000000010000000000000096 from server primarypostgresql15 has been removed
        000000010000000000000097 from server primarypostgresql15 has been removed
        000000010000000000000098 from server primarypostgresql15 has been removed
        000000010000000000000099 from server primarypostgresql15 has been removed
        00000001000000000000009A from server primarypostgresql15 has been removed
        00000001000000000000009B from server primarypostgresql15 has been removed
        00000001000000000000009C from server primarypostgresql15 has been removed
Backup size: 1.2 GiB
Backup end at LSN: 0/9E000000 (00000001000000000000009D, 00000000)
Backup completed (start time: 2022-12-28 08:19:49.192492, elapsed time: 1 minute, 10 seconds)
Processing xlog segments from streaming for primarypostgresql15
        00000001000000000000009D
You have new mail in /var/spool/mail/barman
-bash-4.2$


Verify if it created files as expected:


-bash-4.2$ barman show-backup primarypostgresql15 20221228T081949
Backup 20221228T081949:
  Server Name            : primarypostgresql15
  System Id              : 7163531269528114626
  Status                 : DONE
  PostgreSQL Version     : 150000
  PGDATA directory       : /pgDATA/data
  Base backup information:
    Disk usage           : 1.2 GiB (1.2 GiB with WALs)
    Incremental size     : 1.2 GiB (-0.00%)
    Timeline             : 1
    Begin WAL            : 00000001000000000000009D
    End WAL              : 00000001000000000000009D
    WAL number           : 1
    WAL compression ratio: 99.89%
    Begin time           : 2022-12-28 08:19:49.189472-05:00
    End time             : 2022-12-28 08:20:59.048208-05:00
    Copy time            : 1 minute, 9 seconds
    Estimated throughput : 17.2 MiB/s
    Begin Offset         : 40
    End Offset           : 0
    Begin LSN           : 0/9D000028
    End LSN             : 0/9E000000
  WAL information:
    No of files          : 0
    Disk usage           : 0 B
    Last available       : 00000001000000000000009D
  Catalog information:
    Retention Policy     : VALID
    Previous Backup      : - (this is the oldest base backup)
    Next Backup          : - (this is the latest base backup)
-bash-4.2$
-bash-4.2$ barman list-files primarypostgresql15 20221228T081949
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/backup.info
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/PG_VERSION
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/backup_label
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/backup_manifest
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/current_logfiles
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/pg_hba.conf
...
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/pg_multixact/offsets/0000
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/1/112
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/1/113
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/1/1247
...
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/24605/827
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/24605/828
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/24605/PG_VERSION
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/base/24605/pg_filenode.map
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/pg_xact/0000
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/pg_logical/replorigin_checkpoint
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/log/postgresql-Fri.log
...
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/log/postgresql-Thu.log
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/log/postgresql-Tue.log
/var/lib/barman/pgbackups/primarypostgresql15/base/20221228T081949/data/log/postgresql-Wed.log
/var/lib/barman/pgbackups/primarypostgresql15/wals/0000000100000000/00000001000000000000009D

Final check:


-bash-4.2$ barman list-files primarypostgresql15 20221228T081949|wc -l
1611
You have new mail in /var/spool/mail/barman
-bash-4.2$ barman list-files primarypostgresql15 20221228T081949|grep "/var/lib/barman/pgbackups/primarypostgresql15"|wc -l
1611
-bash-4.2$


As we expected all the files were backedup on the /var/lib/barman/pgbackups/primarypostgresql15 directory.


This closes the blog.


Thank you!

Wednesday, December 14, 2022

Backup postgresql in windows using barman in linux

 Objective: Configure windows postgresql DB backup using linux Barman server

Steps first:
Step1) Collect the postgresql DB server details
Step2) Identify the barman server where the backup will be configured
Step3) Ensure you have access to both the servers - postgres and barman
Step4) Decide on the backup retention policy or collect it
Step5) Verify the version of the postgresql installed on the postgresql server
Step6) Verify that both the postgres and barman server has postgres and epel repos available
Step7) Verify if the user postgres and barman are available in the barman server - if yes, the binary for barman and postgres might have already been installed in the barman server
Step8) Verify the barman rpms are installed already on the barman server (barman-cli, barman, python3-barman, rsync etc..)
Step9) Create barman (superuser,replication) and streaming_barman (replication) user on the postgresql server
Step10) Verify the barman and streaming_barman user exists in the postgresql server
Step11) Setup passwordless ssh between the postgresql server and the barman server (postgres -> barman user) - Mandatory. Reverse for now is optional.
Step12) As root, edit the /etc/barman.conf file to include below listed config setting... in barman server

####begin
[root@10 ~]# grep -Ev ";|^$" /etc/barman.conf
[barman]
barman_user = barman
configuration_files_directory = /etc/barman.d
barman_home = /var/lib/barman
log_file = /var/log/barman/barman.log
log_level = INFO
compression = gzip
parallel_jobs = 4
#last_backup_maximum_age = 1 DAYS
#retention_policy = RECOVERY WINDOW OF 1 WEEKS
####end

Step13) As root (ensure the candidate file has r permission to barman user), create server specific configuration file in barman server  
/etc/barman.d/Primary-Windows-Postgresql.conf

####begin
[Primary-Windows-Postgresql]
description = "Primary-Windows-Postgresql 15 DB Server"
conninfo = host=<ip/hostname> user=barman password=<barman_password> dbname=postgres port=5432
streaming_conninfo = host=<ip/hostname> user=streaming_barman password=<streaming_barman_password> port=5432
#if_linux#ssh_command = ssh postgres@10.0.2.8
backup_method = postgres
streaming_archiver = on
slot_name = barman
create_slot = auto
path_prefix = "/usr/pgsql-12/bin"
retention_policy = RECOVERY WINDOW OF 7 days
#archiver = on ## keep this commented for windows, we have streaming_archiver anyway
####end

Step14) Verify the config files once
Step15) Make the below changes in postgresql.conf in $PGDATA directory in the postgresql db server

wal_level = replica
archive_mode = on
#archive_command = 'rsync -a %p barman@<servername>:/var/lib/barman/Primary-Windows-Postgresql/streaming/%f'


archive_command needs to be set properly, if not set we will face an issue. You will see this in actual execution

Step16) Make the below changes in pg_hba.conf file in the $PGDATA directory in the postgresql server

host all barman <barmanserver/ip>/32 trust
host all streaming_barman <barmanserver/ip>/32 trust

Step17) coordinate with application team for an outage and restart the postgresql service once in the postgresql server
Step18) Check in the outage the output & verify the results are ok in the barman server
barman check Primary-Windows-Postgresql
barman list-backup Primary-Windows-Postgresql
Step19) Bring the application back online in case any
Step20) Verify the capacity of the /var/lib/barman and compare it against the DB capacity
Step21) Schedule the backup in cron, after creating necessary directory /var/lib/barman/backupscr/log as barman in barman server 

/usr/bin/barman backup Primary-Windows-Postgresql 1> /var/lib/barman/backupscr/log/barman_full_backup_Primary-Windows-Postgresql_$(date +\%d\%m\%y%_\%H\%M\%S).log 2>&1

Step22) Verify the successful backup

Now actual execution:

Step1) Collect the postgresql DB server details

VAGRANT|10.0.2.5

Step2) Identify the barman server where the backup will be configured

localhost.localdomain|10.0.2.9

Step3) Ensure you have access to both the servers - postgres and barman

Yes we do

Step4) Decide on the backup retention policy or collect it

1 week

Step5) Verify the version of the postgresql installed on the postgresql server

postgresql 14.6.1

Step6) Verify that both the postgres and barman server has postgres and epel repos available

postgres server is windows
barman has all the repos.

Step7) Verify if the user postgres and barman are available in the barman server - if yes, the binary for barman and postgres might have already been installed in the barman server

-bash-4.2$ id barman
uid=996(barman) gid=994(barman) groups=994(barman)
-bash-4.2$ id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
-bash-4.2$

Step8) Verify the barman rpms are installed already on the barman server (barman-cli, barman, python3-barman, rsync etc..)

-bash-4.2$ rpm -qa|grep -i barman
barman-3.2.0-1.rhel7.noarch
python-barman-3.2.0-1.rhel7.noarch
-bash-4.2$

Step9) Create barman (superuser,replication) and streaming_barman (replication) user on the postgresql server

postgres=# \du
                                       List of roles
    Role name     |                         Attributes                         | Member of
------------------+------------------------------------------------------------+-----------
 barman           | Superuser, Replication                                     | {}
 postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 streaming_barman | Replication                                                | {}


postgres=#

Step10) Verify the barman and streaming_barman user exists in the postgresql server

Allset as we see in last step.

Step11) Setup passwordless ssh between the postgresql server and the barman server (postgres -> barman user) - Mandatory. Reverse for now is optional.

NA for windows

Step12) As root, edit the /etc/barman.conf file to include below listed config setting... in barman server

####begin
-bash-4.2$ cat barman.conf|grep -Ev "^$|^;"
[barman]
barman_user = barman
configuration_files_directory = /etc/barman.d
barman_home = /var/lib/barman
log_file = /var/log/barman/barman.log
log_level = INFO
compression = pigz
parallel_jobs = 4
retention_policy = RECOVERY WINDOW OF 4 WEEKS
-bash-4.2$
####end

Step13) As root (ensure the candidate file has r permission to barman user), create server specific configuration file in barman server 

/etc/barman.d/Primary-Windows-Postgresql.conf

####begin
[Primary-Windows-Postgresql]
description = "Primary-Windows-Postgresql 15 DB Server"
conninfo = host=<ip/hostname> user=barman password=<barman_password> dbname=postgres port=5432
streaming_conninfo = host=<ip/hostname> user=streaming_barman password=<streaming_barman_password> port=5432
#if_linux#ssh_command = ssh postgres@10.0.2.8
backup_method = postgres
streaming_archiver = on
slot_name = barman
create_slot = auto
path_prefix = "/usr/pgsql-12/bin"
retention_policy = RECOVERY WINDOW OF 7 days
#archiver = on
####end


Step14) Verify the config files once

Allset

Step15) Make the below changes in postgresql.conf in $PGDATA directory in the postgresql db server

wal_level = replica
archive_mode = on
#archive_command = 'rsync -a %p barman@<servername>:/var/lib/barman/Primary-Windows-Postgresql/streaming/%f'

We are missing archive_command here, see what happens during backup

Step16) Make the below changes in pg_hba.conf file in the $PGDATA directory in the postgresql server

host    all barman              10.0.2.9/32 trust
host    all streaming_barman    10.0.2.9/32 trust

Step17) coordinate with application team for an outage and restart the postgresql service once in the postgresql server

>done

Step18) Check in the outage the output & verify the results are ok in the barman server

barman check Primary-Windows-Postgresql
barman list-backup Primary-Windows-Postgresql

-bash-4.2$ barman check Primary-Windows-Postgresql
Server Primary-Windows-Postgresql:
        PostgreSQL: OK
        superuser or standard user with backup privileges: OK
        PostgreSQL streaming: OK
        wal_level: OK
        replication slot: OK
        directories: OK
        retention policy settings: OK
        backup maximum age: OK (no last_backup_maximum_age provided)
        backup minimum size: OK (0 B)
        wal maximum age: OK (no last_wal_maximum_age provided)
        wal size: OK (0 B)
        compression settings: OK
        failed backups: OK (there are 0 failed backups)
        minimum redundancy requirements: OK (have 0 backups, expected at least 0)
        pg_basebackup: OK
        pg_basebackup compatible: OK
        pg_basebackup supports tablespaces mapping: OK
        systemid coherence: OK (no system Id stored on disk)
        pg_receivexlog: OK
        pg_receivexlog compatible: OK
        receive-wal running: OK
        archiver errors: OK
-bash-4.2$

Step19) Bring the application back online in case any

Step29) Fire an adhoc backup and check...
==========

The backup got stuck for ever, due to archive_Command comment out in postgres db server...

-bash-4.2$ barman backup Primary-Windows-Postgresql
Starting backup using postgres method for server Primary-Windows-Postgresql in /var/lib/barman/Primary-Windows-Postgresql/base/20221214T161132
Backup start at LSN: 0/2000060 (000000010000000000000002, 00000060)
Starting backup copy via pg_basebackup for 20221214T161132
....
Wait
....
Wait
....

psql - pg_stat_activity:

-[ RECORD 4 ]----+-------------------------------------------------------------------------
datid            |
datname          |
pid              | 3888
leader_pid       |
usesysid         | 16395
usename          | streaming_barman
application_name | barman_streaming_backup
client_addr      | 10.0.2.9
client_hostname  |
client_port      | 58826
backend_start    | 2022-12-14 13:11:29.76526-08
xact_start       |
query_start      | 2022-12-14 13:11:29.784469-08
state_change     | 2022-12-14 13:11:29.78448-08
wait_event_type  | IPC
wait_event       | BackupWaitWalArchive
state            | active
backend_xid      |
backend_xmin     |
query_id         |
query            | BASE_BACKUP  LABEL 'pg_basebackup base backup'  PROGRESS  MANIFEST 'yes'
backend_type     | walsender
-[ RECORD 5 ]----+-------------------------------------------------------------------------

see BackupWaitWalArchive, we are waiting for wal archive to finish in primary windows machine, but there is no archive_command, since commented it out!!!

Related info message can be seen in barman.log as well...

2022-12-14 16:28:52,336 [4346] barman.backup_executor INFO: WARNING:  still waiting for all required WAL segments to be archived (960 seconds elapsed)
2022-12-14 16:28:52,337 [4346] barman.backup_executor INFO: HINT:  Check that your archive_command is executing properly.  You can safely cancel this backup, but the database backup will not be usable without all the WAL segments.


Fix: is to introduce the archive_command with the below attribute, restart the postgresql. The backup just finished fine :)

archive_command = 'copy "%p" "C:\\Users\\Administrator\\PostgreSQL\\14\\data\\pg_archive\\%f"'


-bash-4.2$ barman backup Primary-Windows-Postgresql
Starting backup using postgres method for server Primary-Windows-Postgresql in /var/lib/barman/Primary-Windows-Postgresql/base/20221214T163834
Backup start at LSN: 0/60000D8 (000000010000000000000006, 000000D8)
Starting backup copy via pg_basebackup for 20221214T163834
Copy done (time: 1 minute, 17 seconds)
Finalising the backup.
This is the first backup for server Primary-Windows-Postgresql
WAL segments preceding the current backup have been found:
        000000010000000000000002 from server Primary-Windows-Postgresql has been removed
        000000010000000000000003 from server Primary-Windows-Postgresql has been removed
        000000010000000000000004 from server Primary-Windows-Postgresql has been removed
        000000010000000000000005 from server Primary-Windows-Postgresql has been removed
        000000010000000000000006 from server Primary-Windows-Postgresql has been removed
Backup size: 121.4 MiB
Backup end at LSN: 0/8000060 (000000010000000000000008, 00000060)
Backup completed (start time: 2022-12-14 16:38:34.640789, elapsed time: 1 minute, 20 seconds)
Processing xlog segments from streaming for Primary-Windows-Postgresql
        000000010000000000000007
        000000010000000000000008
You have new mail in /var/spool/mail/barman
-bash-4.2$

-bash-4.2$ barman list-backup Primary-Windows-Postgresql
Primary-Windows-Postgresql 20221214T163834 - Wed Dec 14 13:39:50 2022 - Size: 121.4 MiB - WAL Size: 0 B
Primary-Windows-Postgresql 20221214T161132 - FAILED
-bash-4.2

Earlier cancelled or failed one: 20221214T161132
Successful latest one: 20221214T163834

-bash-4.2$ barman show-backup Primary-Windows-Postgresql 20221214T163834
Backup 20221214T163834:
  Server Name            : Primary-Windows-Postgresql
  System Id              : 7167807434816183596
  Status                 : DONE
  PostgreSQL Version     : 140006
  PGDATA directory       : C:/Users/Administrator/PostgreSQL/14/data

  Base backup information:
    Disk usage           : 121.4 MiB (121.4 MiB with WALs)
    Incremental size     : 121.4 MiB (-0.00%)
    Timeline             : 1
    Begin WAL            : 000000010000000000000007
    End WAL              : 000000010000000000000008
    WAL number           : 2
    WAL compression ratio: 99.89%
    Begin time           : 2022-12-14 13:38:33.171758-08:00
    End time             : 2022-12-14 13:39:50.066436-08:00
    Copy time            : 1 minute, 17 seconds
    Estimated throughput : 1.6 MiB/s
    Begin Offset         : 96
    End Offset           : 96
    Begin LSN           : 0/7000060
    End LSN             : 0/8000060

  WAL information:
    No of files          : 0
    Disk usage           : 0 B
    WAL rate             : 1105.99/hour
    Last available       : 000000010000000000000008

  Catalog information:
    Retention Policy     : VALID
    Previous Backup      : - (this is the oldest base backup)
    Next Backup          : - (this is the latest base backup)
-bash-4.2$


Please note I used postgresql 15 binary in barman to backup postgresql 14.6.1 in windows.

All set!!
All the best & thank you for your visit!

Flashback data archive steps

 Objective: Ways to track DML changes in a table Detailed objective: We should be able to track the changes in the table at any point in tim...