From e10abc5b8dc53608a5a73ca31af2b68b7037df71 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 23 Oct 2022 18:29:33 +0200 Subject: [PATCH] first commit --- .drone.yml | 58 ++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 13 ++++++++++ makefile | 7 ++++++ pg_dump.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 makefile create mode 100755 pg_dump.sh diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..0cdf200 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,58 @@ +--- +kind: pipeline +name: build_amd64 +platform: + os: linux + arch: amd64 + +steps: + - name: docker + image: plugins/docker + settings: + repo: ducampsv/docker-backup-postgres + auto_tag: "true" + auto_tag_suffix: amd64 + username: + from_secret: docker_username + password: + from_secret: docker_password +--- +kind: pipeline +name: build_arm +platform: + os: linux + arch: arm + +steps: + - name: docker + image: plugins/docker + settings: + repo: ducampsv/docker-backyp-postgres + auto_tag: "true" + auto_tag_suffix: arm + username: + from_secret: docker_username + password: + from_secret: docker_password +--- +kind: pipeline +type: docker +name: manifest + +steps: + - name: manifest + image: plugins/manifest + settings: + auto_tag: "true" + target: ducampsv/docker-backup-postgres:latest + template: ducampsv/pacoloco:ARCH + username: + from_secret: docker_username + password: + from_secret: docker_password + platforms: + - linux/amd64 + - linux/arm +depends_on: + - build_amd64 + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4ba6017 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine:latest +COPY pg_dump.sh / +ENV \ + PGDATABASE="" \ + PGHOST="" \ + PGPORT="" \ + PGUSER="" \ + PGPASSWORD="" + + +RUN apk --no-cache add postgresql-client bash +VOLUME ["/backup"] +CMD ["/pg_dump.sh"] diff --git a/makefile b/makefile new file mode 100644 index 0000000..25b774c --- /dev/null +++ b/makefile @@ -0,0 +1,7 @@ + +DOCKER_ORGANIZATION := ducampsv +DOCKER_IMAGE:= docker-backup-postgres + + +build: + docker buildx build . -t $(DOCKER_ORGANIZATION)/$(DOCKER_IMAGE) diff --git a/pg_dump.sh b/pg_dump.sh new file mode 100755 index 0000000..269ac29 --- /dev/null +++ b/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="/backup" +# 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 -c %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 {} \;