#!/bin/bash # Configuration # Le fichier sera nommé BASE_DIR/PREFIXEbasenameSUFFIXE BASE_DIR="/var/backups/postgresql" PREFIXE="sauv_pgsql" SUFFIXE=_$( date +%Y%m%d-%H%M ).sql.gz DUREE_DE_VIE=5 # Exit at first error # note : check that you receive crontab's mails in case something goes wrong. set -e if [ -n "$BASH" ]; then # Bash required for this. If not available, return code will be 0 # even if pg_dumpall fails (but usually it write something on stderr # so cron will send a mail nevertheless). set -o pipefail fi # Initial checks if [ ! -d "$BASE_DIR" ]; then echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2 exit 1 fi # Backup of the whole default cluster su -l -c pg_dumpall postgres | gzip -c > "$BASE_DIR/$PREFIXE$SUFFIXE" # Deletion of old backups if [ "$1" = "--delete-olds" ]; then find $BASE_DIR -name "$PREFIXE*" -mtime +$DUREE_DE_VIE -print0 | xargs -n 200 -r -0 rm -f fi