Salva

From PostgreSQL wiki
Jump to navigationJump to search

1-Srcipt de salva programada en Perl

 $PGSQL_HOME/bin/psql -d template1 -U postgres -e -f
 $BACKUP_SQL/begin_backup.sql >> $BACKUP_LOGS_DIR/$backup_log_filename
 echo "Copying data dirs now..." >> $BACKUP_LOGS_DIR/$backup_log_filename
 echo " " >> $BACKUP_LOGS_DIR/$backup_log_filename
 ## copy data directory into backup location including postgres.conf and any soft
 links used for tablespaces.
 cp -r -P $PGDATA $BACKUP_HOME/a01_data.${TIMESTAMP}
 if [ $? -eq 0 ] then
   echo "data dir copy of a01 is successful." >> $BACKUP_LOGS_DIR/$backup_log_filename
   echo " " >> $BACKUP_LOGS_DIR/$backup_log_filename
 else
   echo "data dir copy of a01 failed." >> $BACKUP_LOGS_DIR/$backup_log_filename
   echo " " >> $BACKUP_LOGS_DIR/$backup_log_filename
 fi
 ## get the database out of backup mode
 $PGSQL_HOME/bin/psql -d template1 -U postgres -e -f
 $BACKUP_SQL/end_backup.sql >> $BACKUP_LOGS_DIR/$backup_log_filename
 ## put the database in backup mode

2-Backup_diario.sh

 #! /bin/sh 
 #############################################
 #Nombre: backup_diario.sh Fecha: 18/04/2007     
 # Script para realizar Backup de las Bases de datos existentes
 # Ademas se controla la cantidad de copias que se desea guardar
 # Luego de esto envia por SSH a otro servidor el respaldo,
 # evitando asi cualquier tipo de riesgo por daño en el soporte
 # físico (HDD)
 #############################################
 BACKUP_DIR=/home/postgres/backup
 BACKUP_DIR_REMOTO=192.168.7.1:/home/backup/
 BACKUP_NUM=7 
 # Realizar Backup de las DB'S
  databases=`su -l postgres -c 'psql -q -t -c "select datname from pg_database;" template1'`
  for d in $databases; do
   if [ ! -d $BACKUP_DIR/$d ]; 
   then echo -n "Creando directorio de respaldo $BACKUP_DIR/$d... "
        su -l postgres -c "mkdir $BACKUP_DIR/$d" ] || continue
        echo "done."
  fi
 # Establecer cantidad maxima del mismo backup $BACKUP_NUM
  archive=$BACKUP_DIR/$d/$d.gz 
  if [ -f $archive.$BACKUP_NUM ]; then 
     rm -f $archive.$BACKUP_NUM; 
  fi  
   n=$(( $BACKUP_NUM - 1 )) 
  while [ $n -gt 0 ]; do
        if [ -f $archive.$n ]; then 
          mv $archive.$n $archive.$(( $n + 1 )) 
        fi
        n=$(( $n - 1 )) 
  done
  if [ -f $archive ]; then
      mv $archive $archive.1;
  fi 
  echo -n "Respaldando la base $d... " 
  su -l postgres -c "(pg_dump $d |gzip -9) > $archive" 
  echo "Transfiriendo archivo $archive" 
  scp $archive root@$BACKUP_DIR_REMOTO  
  echo "Tarea Finalizada." 
  done