simplify the master<->slave first synchronisation

This commit is contained in:
asyncnomi 2025-08-02 23:25:16 +02:00
parent fd3c1e8537
commit 84c8e901de

View file

@ -62,24 +62,12 @@ in
};
};
# This preStart script sync the slaves to the master
# systemd.services.<name>.preStart has a mergeable type
systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) ''
if test -e ${cfg.dataDir}/.first_startup; then
# systemd.services.<name>.preStart has a mergeable type,
# this code will be prepend to the default one
systemd.services.postgresql.preStart = lib.mkIf (builtins.elem myName mapping.db.slaves) (lib.mkBefore ''
if ! test -e ${cfg.dataDir}/PG_VERSION; then
echo "Setting up PostgreSQL slave replication..."
# This is a sl that's defined by the default preStart script
# We need an empty dataDir for the pg_basebackup
# And there is no easy ways that I'm aware of
# to get the hash of that file without recomputing it
PSQL_CONF_PATH="${cfg.dataDir}/postgresql.conf"
PSQL_CONF_TARGET=$(readlink "$PSQL_CONF_PATH")
# Remove data dir
if [ -d "${cfg.dataDir}" ]; then
echo "Deleting postgres data dir: ${cfg.dataDir}"
rm -rf "${cfg.dataDir}"
fi
# Perform base backup from master
echo "Starting base backup from master: ${masterIP}"
${cfg.package}/bin/pg_basebackup \
@ -91,12 +79,9 @@ in
-Xs \
-R
# Symlink back the psql configFile
ln -sf "$PSQL_CONF_TARGET" "$PSQL_CONF_PATH"
echo "PostgreSQL slave setup completed successfully"
else
echo "PostgreSQL standby already configured (standby.signal exists), skipping base backup"
fi
'';
'');
}