#!/bin/bash

# usage: backup_db dbname
# Dump une base postgresql, et garde plusieurs dumps dans le passe
# Les dumps sont compresses (gzip).
#
# E. Viennet pour ScoDoc, 2005-2011

# Note: pour restaurer un backup (en supprimant la base existante !!!)
# 1- supprimer la base existante si elle existe (dropdb)
# 2- recreer la base, vide: createdb -E UTF-8 NOMDELABASE
# 3- pg_restore  --create -d SCOINFO SCOINFO_pgdump


# Nombre de copies a conserver dans le passe:
NUMBACK=500


DBNAME=$1
DUMPBASE=BACKUP-"$DBNAME"
DUMPFILE="$DUMPBASE"/"$DBNAME"_pgdump

function remove_leadings_zeros {
   n=$1
   if [ $n = "0000" ]; then
      echo 0
   else
     while [ "${n:0:1}" = '0' ]; do
        n=${n:1}
     done
     echo $n 
   fi
}

# Check if base pathname for backups exist
if [ ! -e ${DUMPBASE} ]; then
    mkdir ${DUMPBASE}
fi


# 1- rotate previous dumps
if [ -e ${DUMPFILE}.gz ]
then
  LIST=$(ls -r1 ${DUMPFILE}*)
fi


for i in $LIST; do
   x=${i#*.}
   num=${x%.*} # just the number
   num=$(remove_leadings_zeros $num)
   if [ "$i" != "$DUMPFILE".gz ]; then
      if [ $num -lt $NUMBACK ]; then
        # rename file
        base=${i%%.*}	
	ni=$(($num+1))
	mv $i "$base".$(printf "%04d" $ni).gz
      else
        # remove file
        rm $i
      fi
   fi
done

# rename last dump to .0000
if [ -e "$DUMPFILE".gz ]
then
   mv "$DUMPFILE".gz "$DUMPFILE.0000.gz"
fi

# 2- dump
#pg_dump "$DBNAME" > $DUMPFILE

pg_dump --format=t "$DBNAME" -f $DUMPFILE

gzip $DUMPFILE