0

I am having a postgresql database on SLES 12 SP3 system whose service is managed by systemd through the standard unit file. I wanted to automatically restart the service on failure, so create the directory /etc/systemd/system/postgresql.service.d/psql.conf with the following content.

[Service]
Restart=on-failure

This works well and I was happy. Now there is a requirement to setup High Availability system with a slave on standby in case the master fails. The team working on HA has requested to disable the Restart service flag when cluster is setup as they will monitor the service and fail over to the slave when postgresql stops. So I created a file /etc/systemd/system/potgresqlHA.service which is the copy of the existing unit file and HA team started using this file.

systemctl start/stop/restart/status postgresqlHA

Now there is a script which takes a backup of the database when requested. This script checks the status of postgresql before backup to make sure the service is running.

systemctl status postgresql

This works well when cluster is not setup. But when the cluster is setup the status of the service is stopped because postgresqlHA is the service which is started.

How to make the backup script work irrespective of the database service which is running? Is there something I can configure in one of the unit files to make this easier to manage?

I have same problem checking the status for some other services (apache2, tomcat etc) in other scripts.

Thanks, Abhishek

0

Why not instead change the systemctl status postgresql test in the backup script to something like this?

...
if systemctl is-active postgresql
then
    echo "PostgreSQL is active in non-clustered mode"
    # add here any pre-backup commands specific to non-clustered mode
elif systemctl is-active postgresqlHA
then
    echo "PostgreSQL is active in HA mode"
    # add here any pre-backup command specific to HA mode
else
    echo "PostgreSQL backup FAILURE: PostgreSQL is not running." >&2
    # add any commands to send a backup failure alert here if necessary
    exit 69 # EX_UNAVAILABLE
fi
# commands to run the backup here
...

Note that systemctl status <service...> is designed mostly for interactive use; for scripting, systemctl is-active <service...> or systemctl is-failed <service> can be more convenient. If you list multiple services, the commands will return a 0 result code if at least one service satisfies the condition.

If you don't need to care which version of the service is running, you can even test them at the same time:

...
if ! systemctl is-active postgresql postgresqlHA
then
    echo "PostgreSQL backup FAILURE: neither clustered or non-clustered service is running." >&2
    exit 69 # EX_UNAVAILABLE
fi
# commands to run the backup here
...
  • Thanks @telcoM. I did not know about is-active command. In the specific backup case the last is-active check you provided will work. – Abhishek Kumar Mar 22 '18 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.