LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-30-2011, 05:23 PM   #1
astanton
Member
 
Registered: Nov 2010
Distribution: Slackware64 -current
Posts: 82

Rep: Reputation: 5
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?
 
Old 06-30-2011, 05:46 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Your syntax is incorrect for a compound if:

Code:
if [ -d "${file}" ] && [ "$file" != "Archive" ]
 
Old 06-30-2011, 05:52 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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

Last edited by David the H.; 06-30-2011 at 05:55 PM.
 
Old 06-30-2011, 06:06 PM   #4
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 542

Rep: Reputation: 74
You can also give tar the --exclude option; might be a bit easier?
 
Old 06-30-2011, 06:11 PM   #5
astanton
Member
 
Registered: Nov 2010
Distribution: Slackware64 -current
Posts: 82

Original Poster
Rep: Reputation: 5
Unhappy

Quote:
Originally Posted by macemoneta View Post
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.
 
Old 06-30-2011, 06:21 PM   #6
astanton
Member
 
Registered: Nov 2010
Distribution: Slackware64 -current
Posts: 82

Original Poster
Rep: Reputation: 5
Talking

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
 
Old 06-30-2011, 06:56 PM   #7
astanton
Member
 
Registered: Nov 2010
Distribution: Slackware64 -current
Posts: 82

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by Ramurd View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux zip, excluding whole directory path SirLouen Linux - Software 1 09-12-2010 03:40 PM
Excluding executables in a sub-directory from tar backups sorvad Linux - General 5 07-29-2010 06:04 AM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM
Excluding specific folders from backup karloslambchop Ubuntu 7 07-21-2006 08:10 PM
Excluding a directory when copying Woodsman Slackware 2 10-30-2005 06:25 PM


All times are GMT -5. The time now is 01:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration