FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 01-23-2011, 07:51 PM
Johan Martinez
 
Default tar-ing subdirectories separately

I want to backup a directory using tar, but want separate tarballs for each subdirectory. For example:*# ls dir1
subdir1 subdir2 subdir3
Will it possible to do it using only tar command? Or will I need another separate piece of logic/control? I thought of writing a shell script with three tar commands for each subdirectory, but that's not elegant way of doing it. Also, it may not scale as number of subdirectories change. Any other solutions or tips for doing this will be really helpful.*

Thank youjM
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 01-23-2011, 08:23 PM
Michel van Deventer
 
Default tar-ing subdirectories separately

Hi,

> I want to backup a directory using tar, but want separate tarballs for
> each subdirectory. For example:
> # ls dir1
> subdir1 subdir2 subdir3
>
>
> Will it possible to do it using only tar command? Or will I need
> another separate piece of logic/control? I thought of writing a shell
> script with three tar commands for each subdirectory, but that's not
> elegant way of doing it. Also, it may not scale as number of
> subdirectories change. Any other solutions or tips for doing this will
> be really helpful.
You can try something like :
find /dir1 -type d -print -maxdepth 0 | while read DIR ; do tar cfv
$DIR.tar $DIR/; done

not tested, just off the top of my head and it's late, so if it breaks,
you can keep and be happy with all the pieces

Regards,

Michel




_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 01-23-2011, 08:24 PM
Les Mikesell
 
Default tar-ing subdirectories separately

On 1/23/11 2:51 PM, Johan Martinez wrote:
>
> I want to backup a directory using tar, but want separate tarballs for each
> subdirectory. For example:
> # ls dir1
> subdir1 subdir2 subdir3
>
> Will it possible to do it using only tar command?

No, tar only generates a single output stream.

> Or will I need another
> separate piece of logic/control? I thought of writing a shell script with three
> tar commands for each subdirectory, but that's not elegant way of doing it.

What's not elegant about a script that does exactly what you want?

> Also, it may not scale as number of subdirectories change. Any other solutions
> or tips for doing this will be really helpful.

Decide how you want to specify the list (in the script, in something the script
can read, or a wildcard name expansion) and use a 'for' shell loop with the list
that also expands the name into the output filename.

--
Les Mikesell
lesmikesell@gmail.com
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 01-24-2011, 12:00 PM
Leonard den Ottolander
 
Default tar-ing subdirectories separately

Hello Johan,

On Sun, 2011-01-23 at 14:51 -0600, Johan Martinez wrote:
> I want to backup a directory using tar, but want separate tarballs for
> each subdirectory. For example: # ls dir1
> subdir1 subdir2 subdir3

Use find(1) for such cases.

$ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;

Regards,
Leonard.

--
mount -t life -o ro /dev/dna /genetic/research


_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 01-24-2011, 11:21 PM
Nico Kadel-Garcia
 
Default tar-ing subdirectories separately

On Mon, Jan 24, 2011 at 8:00 AM, Leonard den Ottolander
<leonard@den.ottolander.nl> wrote:
> Hello Johan,
>
> On Sun, 2011-01-23 at 14:51 -0600, Johan Martinez wrote:
>> I want to backup a directory using tar, but want separate tarballs for
>> each subdirectory. For example: *# ls dir1
>> subdir1 subdir2 subdir3
>
> Use find(1) for such cases.
>
> $ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;
>
> Regards,
> Leonard.

Ahh-ahh-ahh! You forgot some subdirectories, especially generated from
projects served to Windows systems, may have spaces in their names,
and you'll want parentheses around those "{}" bits. Without those,
chaos can ensue.

And don't get me *started* on what happens if some smart aleck starts
slipping "$" into directory names. This is particularly common in
Samba mounts of CIFS shares where the share creators cleverly left "$"
on the names of the shares to keep them from being browsed.
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 01-25-2011, 12:10 PM
Leonard den Ottolander
 
Default tar-ing subdirectories separately

Hello Nico,

On Mon, 2011-01-24 at 19:21 -0500, Nico Kadel-Garcia wrote:
> On Mon, Jan 24, 2011 at 8:00 AM, Leonard den Ottolander
> > $ find <dir> -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;

> Ahh-ahh-ahh! You forgot some subdirectories, especially generated from
> projects served to Windows systems, may have spaces in their names,
> and you'll want parentheses around those "{}" bits. Without those,
> chaos can ensue.
>
> And don't get me *started* on what happens if some smart aleck starts
> slipping "$" into directory names.

I don't know what implementation of find you use, but the stock find on
CentOS 4 and 5 does not do any shell expansions, despite what the man
page might suggest. And why should it, find should be perfectly capable
to quote its results before injecting them back into a shell.

See for yourself:
$ mkdir testdir; cd testdir
$ mkdir foo; touch foo/bar; mkdir foo bar; mkdir $PATH; mkdir .;rm -rf foo;; ls -1
$ find . -mindepth 1 -maxdepth 1 -type d -exec tar cz {} -f {}.tgz ;
$ ls foo

This is not a perl or shell script, it's a command that substitutes its
own results back into a shell. Afacit it does this safely (compare
printquoted.c). No user interaction in the form of the quoting of "{}"
required. If your version of find does I would consider that a bug .

Regards,
Leonard.

--
mount -t life -o ro /dev/dna /genetic/research


_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 

Thread Tools




All times are GMT. The time now is 09:44 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org