LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   excluding directory from backup in bash... (http://www.linuxquestions.org/questions/programming-9/excluding-directory-from-backup-in-bash-889265/)

astanton 06-30-2011 05:23 PM

excluding directory from backup in bash...
 
I've got the following code and kludge.

It's working okay except that there's a directory called "Archive" which I don't need to backup.

I've tried various combinations of &&, -a, etc., and I'm kind of at a loss for getting this right.

Here's the code I have so far:

Code:


#!/bin/sh

# backup subdirs as individual tarballs with prepended timestamps and
# move them somewhere else.
# --exclude "/path/to/dir/*" (Doesn't seem to work)

#      For now a kludge that rm's the 'Archive' dir after the tarring.

# set VARS

DSTAMP=$(date +%Y%m%d)
HOMEDIRS=/home
BUDIRS=/home/budirs

#      Make the archive point if it doesn't already exist.

mkdir -p $BUDIRS

#      Loop through the dirs and prepend the stamps to the tarballs.

cd $HOMEDIRS
for file in *; do
 if [[ -d "${file}" ]]; then
  tar cpzvf "$BUDIRS/${DSTAMP}-${file}".tar.gz "${file}"
 fi
rm -rf "$BUDIRS/$DSTAMP-Archive.tar.gz"      # Need to fix this kludge.
done

The problem is with this line:

Code:

if [[ -d "${file}" ]]
The script works great until I try to "Exclude" the directory "Archive" from being tarred up.

I've tried:

Code:

if [[ -d "${file}" && !"Archive" ]]
And other variations of using an and, it just bombs.

How can I write that line so that I can loop through all of the subdirs, tarring them up, but excluding the "Archive" subdirectory?

macemoneta 06-30-2011 05:46 PM

Your syntax is incorrect for a compound if:

Code:

if [ -d "${file}" ] && [ "$file" != "Archive" ]

David the H. 06-30-2011 05:52 PM

Please be more specific about how it "bombs". Do you get any error messages? Or any output at all?

Change your shebang to #!/bin/bash. /bin/sh restricts the script to posix-compatible mode, and many bash-only features are disabled.

If it still doesn't work after that, you might try a different approach, and use extended globbing to exclude the directory.

Code:

shopt -s extglob

for file in ./!(Archive); do

More on globbing here:
http://mywiki.wooledge.org/glob?acti...irect=globbing


Edit @macemoneta. His syntax is just fine when using bash's new [[ test keyword. It's only the old test command that needs to be broken up like that.

http://mywiki.wooledge.org/BashFAQ/031

Ramurd 06-30-2011 06:06 PM

You can also give tar the --exclude option; might be a bit easier?

astanton 06-30-2011 06:11 PM

Quote:

Originally Posted by macemoneta (Post 4400774)
Your syntax is incorrect for a compound if:

Code:

if [ -d "${file}" ] && [ "$file" != "Archive" ]

I tried that as well as:

Code:

if [ -d "${file}" ] && [ "${file}" != "Archive" ]
(With the braces around 'file'), and both ways I'm now getting the following error:

Code:

./hbak.sh: line 28: syntax error near unexpected token `fi'
./hbak.sh: line 28: `  fi'

But you're right that it looks like it should work now.

astanton 06-30-2011 06:21 PM

Oh the horror...

That was a der move on my part. nevermind and thank you so much for your help!

The problem, the second time around was a lacking:

Code:

; then
on the end of that line when I did the cut/paste.

I'll mark this as solved. Again, thanks so much :)

astanton 06-30-2011 06:56 PM

Quote:

Originally Posted by Ramurd (Post 4400797)
You can also give tar the --exclude option; might be a bit easier?

That was how I originally tried it, but it didn't pan out for me, and I assumed it was because the tar command was looping within an if statement.

Anyway, everything is working great now.

Thanks :)


All times are GMT -5. The time now is 10:59 AM.