#!/bin/ksh

# SGALOGDIR=../../logs/sga/
SGALOGDIR=${SYSTEM_DIR}/logs/sga/
BACKUPDIR=./

print "Fazendo backup dos logs do SGA..."

if [ ! -d ${SGALOGDIR} ]; then
  print "No foi encontrado o diretrio de logs ${SGALOGDIR}."
  exit
fi
cd ${SGALOGDIR}

backup="${BACKUPDIR}/sga-backup-`date +%Y%m%d%H%M%S`.tar"

# No posso remover os arquivos de log que esto em uso...
# Para cada SGA, no coloca no backup o arquivo mais recente.

sgas=`ls -1 sga*.dat | cut -d'-' -f2 | sort | uniq`
allfiles=""
for host in $sgas ; do
  # All files from host $host
# print $host
  files=`ls -1 sga-${host}-*| sort -r `
  # All files but the first
  allbutfirst=`print ${files} | cut -s -d' ' -f2-`
  if test "${allbutfirst}" ; then
#   print "Faz backup"
    allfiles="${allfiles} ${allbutfirst}"
  fi
done
if test "${allfiles}"; then
  tar -cf ${backup} ${allfiles}
  gzip ${backup}
  rm -f ${allfiles}
  print "Backup completado."
else
  print "No foram encontrados logs antigos para backup."
fi
