Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Tar usage in bash script

I have the following directory structure:

/test_dir/logs:
            admin-access.log
            admin-access.log00001
            admin-access.log00002
            admin-access.log00003
            admin-access.log00004
            admin-access.log00005

/test_dir/installer_logs:
            AITimeListener.log
            installer.log


/test_dir/servers/clust1/logs/:
            clust1.out
            clust1.out00001
            clust1.out00002
            clust1.out00003
            clust1.out00004

I'm using the following set of commands to archive the folder with exclusions:

cd /test_dir/
tar -czhf /backup/test_dir_node01_2016-02-19_initial.tgz --anchored --exclude "logs/*" --exclude "servers/*/logs/*" --exclude "installer_logs/*"  *

And as result I've got the following directory structure after archive untar:

/backup/logs:
          no files here

/backup/installer_logs:
          no files here

/backup/servers/clust1/logs/:         
           no files here

I'm trying to code the bash script which is supposed to do the same and with no luck, here's my code:

backup.parameters file:

NC_HOME=/test_dir
BACKUP_HOME=/backup

EXCLUDE_LIST="
        --exclude \"logs/*\"
        --exclude \"servers/*/logs/*\"
        --exclude \"installer_logs/*\"
"

backup_initial.sh file

#!/bin/bash

if [ ! -f backup.parameters ]; then echo "backup.parameters file not found! exiting...."; exit 1; fi
. ./backup.parameters

TAR_ARGS="czf"
DATE=`date +%F`
HOSTNAME=`hostname`

cd $NC_HOME
echo "Initial backup  started on $HOSTNAME at `date \"+%F %T\"`"
tar -czhf $BACKUP_HOME/$(basename $NC_HOME)_"$HOSTNAME"_"$DATE"_initial.tgz --anchored  $EXCLUDE_LIST *
echo "Initial backup finished on $HOSTNAME at `date \"+%F %T\"`"
echo "Initial backup location: $BACKUP_HOME/$(basename $NC_HOME)_"$HOSTNAME"_"$DATE"_initial.tgz"

After untar of archive which is created by the script I see there're still files in exclusion directories:

/backup/logs:
            admin-access.log
            admin-access.log00001
            admin-access.log00002
            admin-access.log00003
            admin-access.log00004
            admin-access.log00005

/backup/installer_logs:
            AITimeListener.log
            installer.log


/backup/servers/clust1/logs/:
            clust1.out
            clust1.out00001
            clust1.out00002
            clust1.out00003
            clust1.out00004

Please help how can I code this out, also please don't advise me to use -X option. I really need to do it with --exclude.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try changing EXCLUDE_LIST to:

EXCLUDE_LIST=(
        --exclude 'logs/*'
        --exclude 'servers/*/logs/*'
        --exclude 'installer_logs/*'
)

And then expand it like this:

tar -czhf ... --anchored  "${EXCLUDE_LIST[@]}" *

The reason why what you're doing now doesn't work is because EXCLUDE_LIST is being expaned to --exclude "logs/*" .... The double quotes aren't removed and remain part of the argument, and so the pattern doesn't match anything since you don't have any files or directories that have double quotes in their names.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda