Google Answers Logo
View Question
 
Q: Exclude directory in tar and zip ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: Exclude directory in tar and zip
Category: Computers > Programming
Asked by: makman-ga
List Price: $2.00
Posted: 19 Jun 2006 16:18 PDT
Expires: 19 Jul 2006 16:18 PDT
Question ID: 739467
I have a directory I need to backup using "tar" and/or "zip".  I need
to exclude a directory from the archive. Backup script running on
Unix.

Directory to archive: /home/
Directory to exclude: /home/cpanel/

I need the solution for zip and tar.
Answer  
Subject: Re: Exclude directory in tar and zip
Answered By: eiffel-ga on 20 Jun 2006 03:26 PDT
Rated:5 out of 5 stars
 
Hi makman-ga,

These commands should do it:

zip -r backup.zip /home -x "/home/cpanel/*"

tar -cf backup.tar /home --exclude "/home/cpanel"

Please request clarification if there are any problems.

Regards,
eiffel-ga

Request for Answer Clarification by makman-ga on 20 Jun 2006 22:24 PDT
Neither of those commands work.  I still get the cpanel archived in
both zip and tar.  These are the exact commands I used to begin with. 
In the man pages, seems that --exclude and -x work for FILES and not
directories.  I need a solution to ignore a directory... =o)

Here is the exact commands I am running:

// Create compression method parameters
        if ($archiveFormat == "tar" || $archiveFormat == "tar.gz" ||
$archiveFormat == "tbz") {
                $cMethod = "tar -cvf";
        } elseif ($archiveFormat == "zip") {
                $cMethod = "zip -v -9";
        } elseif ($archiveFormat == "gzip") {
                $cMethod = "gzip -v -9";
        }

// Create system commands
$date = date(m_d_Y);

        if ($archiveDir == "") {
                $archiveDir = system("pwd");
        }

// Process excluded files/directories

        if ($excludeDir != "" && $archiveFormat == "tar" ||
$archiveFormat == "tar.gz" || $archiveFormat == "tbz") {
                $excludeDir = "--exclude " . '"' . $excludeDir . '"';
        } elseif ($excludeDir != "" && $archiveFormat == "zip" ||
$archiveFormat == "gzip") {
                $excludeDir = "-x " . '"' . $excludeDir . '*"';
        }

$compress = "$cMethod $filePrefix" . "_" . "$date.$archiveFormat
$archiveDir $excludeDir";
$move = "mv " . $filePrefix . "_" . "$date.$archiveFormat $location";

// Compress all user directories in one archive
print ("\n\n( Compressing $archiveDir Using $cMethod... )\n\n");
$command = system($compress, $result);

        if($result == 0){
                $result = "OK";
        } else {
                $result = "FAILED";
        }




$archiveFormat is defined in config as "tar.gz" OR "zip"
$excludeDir is defined in config as "/home/cpanel"

The "Process excluded files/directories" section is where I am
constructing the "exclude" part of the command with the information
you gave me.  The end result looks exactly like you showed.  here is a
copy of the output from this script with path names replaced:

zip -vr -9 httpdocs_06_20_2006.zip /home/user/domain.com/site -x "/excluded-dir/*"

tar -cvf httpdocs_06_20_2006.tar.gz /home/user/domain.com/site
--exclude "/excluded-dir"

Let me know if you see anything wrong with this output.

THANKS!

-MAK

Clarification of Answer by eiffel-ga on 21 Jun 2006 02:36 PDT
Hi makman-ga,

You need to use the full pathname for the excluded directory. I assume
the excluded directory is a subdirectory of the one that you are
archiving:

zip -vr -9 httpdocs_06_20_2006.zip /home/user/domain.com/site -x
"/home/user/domain.com/site/excluded-dir/*"

tar -cvf httpdocs_06_20_2006.tar.gz /home/user/domain.com/site
--exclude "/home/user/domain.com/site/excluded-dir"

Does that solve the problem?

Regards,
eiffel-ga

Request for Answer Clarification by makman-ga on 21 Jun 2006 19:09 PDT
ok...  gotcha.  thanks!  both tar and zip work great!  That helps a
lot...  now I can finish my program.  I appreciate the help!

Clarification of Answer by eiffel-ga on 22 Jun 2006 03:10 PDT
Thanks, makman-ga, for the comments and kind tip.

Request for Answer Clarification by makman-ga on 22 Jun 2006 19:11 PDT
just to add to this in case anyone needs the information.  I also
found out to exclude multiple directories, the command is as follows:

tar -cvf httpdocs_06_20_2006.tar.gz /home/user/domain.com/site
--exclude "/home/user/domain.com/site/excluded-dir1" --exclude
"/home/user/domain.com/site/excluded-dir2"

zip -vr -9 httpdocs_06_20_2006.tar.gz /home/user/domain.com/site
-x "/home/user/domain.com/site/excluded-dir1/*" -x
"/home/user/domain.com/site/excluded-dir2/*"

NOTE: Must use multiple instances of "--exclude" or "-x" and cannot
have ANY spaces after the last one!! I had trouble creating a for loop
to condition this, however, I got it working thanks to eiffel-ga's
help and advanced conditional statements!

-MAK

Clarification of Answer by eiffel-ga on 23 Jun 2006 03:05 PDT
Hi makman-ga,

Thanks for the extra information. (I'm posting this here, so that the
question no longer shows "Needs Clarification" when I login.)
makman-ga rated this answer:5 out of 5 stars and gave an additional tip of: $3.00
command worked perfectly!  simple, but I couldnt find the answer
through the traditional google search.  Thanks!  Hope this one helps
someone else as well...

-MAK

Comments  
Subject: Re: Exclude directory in tar and zip
From: pgmer6809-ga on 27 Jun 2006 15:57 PDT
 
You can also create a file containing a list of directories to
exclude. This saves having to type --exclude "exdir1" --exclude
"exdir2" etc. etc.
Here is a copy of a session that shows how this is done. 
the name of the directory to be tar'd is /tmp/MYDIR and the file that
contains the directories to exclude is /tmp/excl_file. The resulting
archive is /tmp/MyArchive.
The first 16 lines in what follows are just creating dummy files and
directories for the tar to work on. The magic tar cmd is in line 17
(Note the X in -cvfX is UPPER CASE!!). The lines after  that are the
output of the tar cmd, and a tar TOC.
-------------------------- code begins --------------------
     1  Script started on Tue Jun 27 18:27:59 2006
     2  sh-2.03$ mkdir MYDIR
     3  sh-2.03$ cd MYDIR
     4  sh-2.03$ mkdir subdir1
     5  sh-2.03$ mkdir subdir2 
     6  sh-2.03$ mkdir excldir1
     7  sh-2.03$ mkdir excldir2
     8  sh-2.03$ for D in `ls `; do 
            echo "File1 in $D" >${D}/File1_${D}; 
            echo "File2 in $D" >${D}/File2_${D}; done
     9  sh-2.03$ cd ..
    10  sh-2.03$ pwd
    11  /tmp
    12  sh-2.03$ echo ./excldir1 >excl_file
    13  sh-2.03$ echo ./excldir2 >>excl_file
    14  sh-2.03$ cat excl_file
    15  ./excldir1
    16  ./excldir2
    17  sh-2.03$ tar -cvfX MyArchive excl_file -C MYDIR .
    18  a ./ 0K
    19  a ./subdir1/ 0K
    20  a ./subdir1/File1_subdir1 1K
    21  a ./subdir1/File2_subdir1 1K
    22  a ./subdir2/ 0K
    23  a ./subdir2/File1_subdir2 1K
    24  a ./subdir2/File2_subdir2 1K
    25  a ./excldir1 excluded
    26  a ./excldir2 excluded
    37  sh-2.03$ tar -tvf MyArchive
    38  tar: blocksize = 13
    39  drwxr-xr-x 1007/3        0 Jun 27 18:28 2006 ./
    40  drwxr-xr-x 1007/3        0 Jun 27 18:31 2006 ./subdir1/
    41  -rw-r--r-- 1007/3       17 Jun 27 18:31 2006 ./subdir1/File1_subdir1
    42  -rw-r--r-- 1007/3       17 Jun 27 18:31 2006 ./subdir1/File2_subdir1
    43  drwxr-xr-x 1007/3        0 Jun 27 18:31 2006 ./subdir2/
    44  -rw-r--r-- 1007/3       17 Jun 27 18:31 2006 ./subdir2/File1_subdir2
    45  -rw-r--r-- 1007/3       17 Jun 27 18:31 2006 ./subdir2/File2_subdir2
Subject: Re: Exclude directory in tar and zip
From: arborrow-ga on 08 Oct 2006 12:06 PDT
 
I'm not sure what I am doing wrong:

tar cvf varlog.tgz /var/log --exclude "/var/log/mysql" --exclude "/var/log/apache2"

It creates the file fine; however, it does not exclude the
directories. What am I doing wrong?
Subject: Re: Exclude directory in tar and zip
From: arborrow-ga on 08 Oct 2006 12:22 PDT
 
OK - I figured it out. It turns out the GNU Tar which I have as I use
Ubuntu requires that the path to be tar'd come after the exclude
options. The example listed here had it before.

TAR:

tar -cvf httpdocs_06_20_2006.tar.gz 
/home/user/domain.com/site
--exclude "/home/user/domain.com/site/excluded-dir1" 
--exclude "/home/user/domain.com/site/excluded-dir2"

GNU TAR:

tar -cvf httpdocs_06_20_2006.tar.gz 
--exclude "/home/user/domain.com/site/excluded-dir1" 
--exclude "/home/user/domain.com/site/excluded-dir2"
/home/user/domain.com/site

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy