From f573f46b84a4a6b5c25d3cc13044529cf7265a50 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 16 Sep 2020 21:35:29 +0200 Subject: [PATCH 1/5] add postgress backup script Signed-off-by: vincent --- script/script/pg_dump.sh | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 script/script/pg_dump.sh diff --git a/script/script/pg_dump.sh b/script/script/pg_dump.sh new file mode 100755 index 0000000..3575916 --- /dev/null +++ b/script/script/pg_dump.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# Inspiré d'un script trouvé sur phpnews.fr (plus en ligne) +# Version 0.4 16/09/2020 +# Script sous licence BEERWARE +set -eu +## Paramètres +# Répertoire de stockage des sauvegardes +DATADIR="/mnt/diskstation/git/backup/postgres/" +# Répertoire de travail (création/compression) +DATATMP=$DATADIR +# Nom du dump +DATANAME="dump_$(date +%d.%m.%y@%Hh%M)" +# Compression +COMPRESSIONCMD="tar -czf" +COMPRESSIONEXT=".tar.gz" +# Rétention / rotation des sauvegardes +RETENTION=30 +# Exclure des bases +EXCLUSIONS='(postgres|template1|template0)' +# Email pour les erreurs (0 pour désactiver +EMAIL=0 +# Log d'erreur +exec 2> ${DATATMP}/error.log + +## Début du script + +ionice -c3 -p$$ &>/dev/null +renice -n 19 -p $$ &>/dev/null + +function cleanup { + if [ "`stat --format %s ${DATATMP}/error.log`" != "0" ] && [ "$EMAIL" != "0" ] ; then + cat ${DATATMP}/error.log | mail -s "Backup Postgres $DATANAME - Log error" ${EMAIL} + fi +} +trap cleanup EXIT + +# On crée sur le disque un répertoire temporaire +mkdir -p ${DATATMP}/last + +# On place dans un tableau le nom de toutes les bases de données du serveur +databases="$(psql -d postgres -c 'select datname from pg_catalog.pg_database;' -t|grep -v -E $EXCLUSIONS)" + +# Pour chacune des bases de données trouvées ... +for database in ${databases[@]} +do + echo "dump : $database" + pg_dump $database > ${DATATMP}/last/${database}.sql +done + +# On tar tous +cd ${DATATMP} +${COMPRESSIONCMD} ${DATANAME}${COMPRESSIONEXT} last/ +chmod 600 ${DATANAME}${COMPRESSIONEXT} + +# On le déplace dans le répertoire +if [ "$DATATMP" != "$DATADIR" ] ; then + mv ${DATANAME}${COMPRESSIONEXT} ${DATADIR} +fi + +# Lien symbolique sur la dernier version +#cd ${DATADIR} +#set +eu +#unlink last${COMPRESSIONEXT} +#set -eu +#ln ${DATANAME}${COMPRESSIONEXT} last${COMPRESSIONEXT} + +# On supprime le répertoire temporaire +#rm -rf ${DATATMP}/${DATANAME} + +echo "Suppression des vieux backup : " +find ${DATADIR} -name "*${COMPRESSIONEXT}" -mtime +${RETENTION} -print -exec rm {} \; From a8fbeb832c501f8694850a576f0585362b941435 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 16 Sep 2020 21:56:20 +0200 Subject: [PATCH 2/5] test From 7a83f23ed4e011f8faaae23300c27ddd92789634 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 16 Sep 2020 22:05:24 +0200 Subject: [PATCH 3/5] test From 73d23589733c200786cc97ee4191f732132af2c1 Mon Sep 17 00:00:00 2001 From: vincent Date: Wed, 16 Sep 2020 22:23:46 +0200 Subject: [PATCH 4/5] modify check service for pg --- script/script/check_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/script/check_service.py b/script/script/check_service.py index cfa203e..84b1b5f 100644 --- a/script/script/check_service.py +++ b/script/script/check_service.py @@ -3,7 +3,7 @@ import time import sys services=("syncthing@vincent", - "mariadb", + "postgres", "nginx", "php-fpm", "gitea", From 854393898cc8002e0f5efdde97152259a6066bf8 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 17 Sep 2020 20:58:10 +0200 Subject: [PATCH 5/5] update check service --- script/script/check_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/script/check_service.py b/script/script/check_service.py index 84b1b5f..8a5d3b3 100644 --- a/script/script/check_service.py +++ b/script/script/check_service.py @@ -3,7 +3,7 @@ import time import sys services=("syncthing@vincent", - "postgres", + "postgresql", "nginx", "php-fpm", "gitea",