↓Skip to main content

Well That Was Careless

·2 mins

This morning I noticed some of the services on my home server were down and it didn’t take long to find out that it was because the PostgreSQL database service had failed to start after an automatic reboot. I thought this was strange as nothing had changed on the server for some time, but the problem was quickly identified:

journalctl -u pg.service -S today

This command reveals the following logs:

Nov 14 00:36:06 limiting-factor systemd[1]: Starting pg.service - PostgreSQL Service...
Nov 14 00:36:06 limiting-factor pg[2519]: Error: statfs /srv/backup/scripts/run-pg-backup.sh: no such file or directory
Nov 14 00:36:06 limiting-factor podman[2519]: 2024-11-14 00:36:06.910119597 +0000 UTC m=+0.105339796 image pull 56bb918d933e6e7330d6dd9098af9ccc880c16ae94e03968fa2ba15ff8>
Nov 14 00:36:06 limiting-factor systemd[1]: pg.service: Main process exited, code=exited, status=125/n/a
Nov 14 00:36:06 limiting-factor systemd[1]: pg.service: Failed with result 'exit-code'.
Nov 14 00:36:06 limiting-factor systemd[1]: Failed to start pg.service - PostgreSQL Service.

The /srv/backup/scripts/run-pg-backup.sh script had disappeared! It’s mounted as a volume by the PostgreSQL container, so its absence prevents the container from starting.

A little background #

I have some systemd timer units that trigger daily backups of services storing important data. Every day the backups are written to a date-stamped file in /srv/backup/${SERVICE_NAME}/ and when they have completed another process copies them to a remote server. I don’t want to accumulate too many of these local backups, so a weekly timer triggers a clean-up job and - you guessed it! - I forgot to exclude the scripts from the clean-up.

The fix #

I updated the find command to prune out the scripts directory:

diff --git a/backups.yml b/backups.yml
index 4bd69bc..f1e908d 100644
--- a/backups.yml
+++ b/backups.yml
@@ -81,7 +81,7 @@
 
           [Service]
           Type = oneshot
-          ExecStart = find /srv/backup -type f -mtime +10 -exec rm {} \;
+          ExecStart = find /srv/backup -path /srv/backup/scripts -prune -o -type f -mtime +10 -exec rm {} \;
       notify:
         - Reload systemd
     - name: Create timer to trigger backup prune

But what about the scripts themselves? Well, they are under version control and were installed by ansible in the first place, so I just had to re-run a couple of playbooks and all was good.