0

I am trying to write a bash/shell script to zip up a specific folder and ignore certain sub-dirs in that folder.

This is the folder I am trying to zip "sync_test5":

enter image description here

My bash script generates an ignore list (based on) and calls the zip function like this:

#!/bin/bash

SYNC_WEB_ROOT_BASE_DIR="/home/www-data/public_html"
SYNC_WEB_ROOT_BACKUP_DIR="sync_test5"
SYNC_WEB_ROOT_IGNORE_DIR="dir_to_ignore dir2_to_ignore"

ignorelist=""
if [ "$SYNC_WEB_ROOT_IGNORE_DIR" != "" ];
then
    for ignoredir in $SYNC_WEB_ROOT_IGNORE_DIR
    do
        ignorelist="$ignorelist $SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/**\*"
    done
fi

FILE="$SYNC_BACKUP_DIR/$DATETIMENOW.website.zip"
cd $SYNC_WEB_ROOT_BASE_DIR;
zip -r $FILE $SYNC_WEB_ROOT_BACKUP_DIR -x $ignorelist >/dev/null
echo "Done"

Now this script runs without error, however it is not ignoring/excluding the dirs I've specified.

So, I had the shell script output the command it tried to run, which was:

zip -r 12-08-2014_072810.website.zip sync_test5 -x  sync_test5/dir_to_ignore/**\* sync_test5/dir2_to_ignore/**\*

Now If I run the above command directly in putty like this, it works:

enter image description here

So, why doesn't my shell script exclude working as intended? the command that is being executed is identical (in shell and putty directly).

Latheesan
  • 153

2 Answers2

0

I can't see what you are trying to achieve with "**\*" in the exclude list, but as I read it -x excludes files whose names match a pattern, and does not take account of the directories where they reside.

The simplest way to exclude directories is temporarily to remove execute permission from them, eg:-

chmod -x ...
zip ...
chmod +x ...

Just make sure that you restore execute permission if you abort the script for any reason.

AFH
  • 17,958
0

According to this answer, https://superuser.com/a/496903/356580, you need something like:

ignorelist=""
if [ "$SYNC_WEB_ROOT_IGNORE_DIR" != "" ];
then
    for ignoredir in $SYNC_WEB_ROOT_IGNORE_DIR
    do
        ignorelist="$ignorelist \"$SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/*\""
    done
fi

You can also do some fancy escaping with the asterix so that when it's replaced in the command line, it's escaped, e.g.

ignorelist="$ignorelist $SYNC_WEB_ROOT_BACKUP_DIR/$ignoredir/\\*"

But to each his own. What you want is the command to look like either of the below when it's executed through the substitution:

zip -r sync_test.zip sync_test -x sync_test/dir_to_ignore/\* sync_test/dir2_to_ignore/\*
zip -r sync_test.zip sync_test -x "sync_test/dir_to_ignore/*" "sync_test/dir2_to_ignore/*"

This way, the asterix character gets sent to zip, and there's no file globing on the command line.

zerodiff
  • 111