Application-Consistent Protection for PostgreSQL on Kubernetes

Updated

On this page

You can add a pre-process script file or a post-process script file for PostgreSQL on the Kubernetes access nodes.

Examples

The following example is a pre-process script for PostgreSQL. Edit the example script to include the PostgreSQL port, database user credentials, and the name of a database on the PostgreSQL server, other than the template0 database.

# cat /opt/commvault/Base/kscripts/mynamespace.myapplication.prescript

#!/usr/bin/env python

import psycopg2

import os, time, datetime

dt=datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")

file1 = open("/scripts/pre-freeze.log","a+" )

conn = None

exit_status = 0

try:

conn = psycopg2.connect(host='localhost',port='<POSTGRESQL_PORT>',user='<POSTGRESQL_USER>',password='<POSTGRESQL_PASSWORD>',database='<POSTGRESQL_DB_NAME>')

cur = conn.cursor()

cur.execute ("select version();")

data = cur.fetchone()

file1.write (dt)

file1.write ("\n-------------------------------------------\n")

file1.write ("\t PostgreSQL version is %s: "%data)

file1.write ("\n-------------------------------------------\n")

file1.write (dt)

file1.write ("\t executing query to quiesce the database \n")

cur.execute (" select pg_start_backup('label'); ")

file1.write (dt)

file1.write ("\t Database is quiesced now successfully \n")

cur.close()

except (Exception, psycopg2.DatabaseError) as error:

file1.write(dt)

file1.write(str(error))

file1.write( "\n Error in quiesce DB, please check PostgreSQL logs for more info\n")

exit_status = 1

finally:

if conn is not None:

conn.close()

file1.write( "\n Database connection closed\n")

file1.close()

exit(exit_status)

The following example is a post-process script for PostgreSQL. Edit the example script to include the PostgreSQL port, database user credentials, and the name of a database on the PostgreSQL server, other than the template0 database.

# cat /opt/commvault/Base/kscripts/mynamespace.myapplication.postscript #!/usr/bin/env python

import psycopg2

import os, time, datetime

dt=datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")

file1 = open("/scripts/post-thaw.log","a+" )

conn = None

exit_status = 0

try:

conn = psycopg2.connect(host='localhost',port='<POSTGRESQL_PORT>',user='<POSTGRESQL_USER>',password='<POSTGRESQL_PASSWORD>',database='<POSTGRESQL_DB_NAME>')

cur = conn.cursor()

cur.execute ("select version();")

data = cur.fetchone()

file1.write (dt)

file1.write ("\n-------------------------------------------\n")

file1.write ("\t PostgreSQL version is %s: "%data)

file1.write ("\n-------------------------------------------\n")

file1.write (dt)

file1.write ("\t executing query to unquiesce the database \n")

cur.execute ("select pg_stop_backup();")

file1.write (dt)

file1.write ("\t Database is unquiesced now \n")

cur.close()

except(Exception, psycopg2.DatabaseError) as error:

file1.write(dt)

file1.write(str(error))

file1.write( "\n Error in unquiesce DB, please check PostgreSQL logs for more info \n")

exit_status = 1

finally:

if conn is not None:

conn.close()

file1.write( "\n Database connection closed\n")

file1.close()

exit(exit_status)