Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make a script to backup each of my plesk hosts to individual files, I am having two problems:

  1. I would like to remove the folder structure from archive, the tar is 3 folders deep
  2. I am getting this error: tar: Removing leading `/' from member names
  3. I need my archive to ignore folders named "catch" because I don't need them in my archive.

The code:

FILES=/var/www/vhosts/*
FNAME=""
for f in $FILES
do
FNAME=`basename $f`
tar cfv "/root/backup/ftp/$FNAME.tar" $f
done

Sample output:

tar: Removing leading `/' from member names
/var/www/vhosts/mydomain.com/
/var/www/vhosts/mydomain.com/conf
/var/www/vhosts/mydomain.com/etc/
/var/www/vhosts/mydomain.com/etc/group
/var/www/vhosts/mydomain.com/etc/termcap
/var/www/vhosts/mydomain.com/etc/passwd
/var/www/vhosts/mydomain.com/usr/
share|improve this question
up vote 0 down vote accepted

As always, in Linux a single command will do:

for i in $(find /path/to/source/dir -type f -exec echo {} \;) ; do tar -c  -f path/to/dest/directory/${i##*/}.tar -C ${i%/*} ${i##*/} ; done

Make sure that the source directory is specified through its full path name, not a relative one; the rest is obvious, I have used heavily the shell substitution capacities.

EDIT: while I was writing my reply, an other answer stated that one cannot erase the directory structure. This is not correct, the -F option in tar does exactly that. With this command, one will find all files coming from the directory tree in /path/to/source/directory, all suitably tarred, and piled up neatly within the branchless directory /path/to/dest/directory.

Edit: the change requested, i.e., excluding files whose names begin with catch, can be accomplished by means of the following command:

for i in $(find /path/to/source/dir -type f ! -name 'catch*' -exec echo {} \;) ; do tar -c  -f path/to/dest/directory/${i##*/}.tar -C ${i%/*} ${i##*/} ; done

If you want to avoid files ending the with catch, susbstitute catch* with *catch, I am sure you get the drift.

share|improve this answer
    
@Michael: you should reconsider your acceptance of Michael Tablosky' answer. – MariusMatutiae Oct 19 '13 at 12:18
    
Sorry Marius, I have to disagree. You don't seem to understand what the OP wants and I don't think that '-F' does what you say. At least the man page of my tar states something different – Michael Tabolsky Oct 20 '13 at 15:32
    
Besides, -C in your one-liner just exactly what I've suggested in my answer. Though your one-liner is going to launch tar 1xN-of-files times. Going to the target path on the start saves the hassle. – Michael Tabolsky Oct 20 '13 at 15:39
    
Just try it, lol. Easier done than said. – MariusMatutiae Oct 20 '13 at 16:33
    
Great answer, but it's so complicated, because I just started to work with Linux commands. Moving the script to the wanted folder is easy. If you can help me more, can I make the tar command ignore and not include in archive the folders matching "catch" (I have some folders named "catch" and I don't want to have them in archive? I would reconsider my acceptance. – Michael Oct 20 '13 at 20:40

I would like to remove the folder structure from archive, the tar is 3 folders deep

you can't. cd to the target directory or one level higher, then run tar on ./$f

I am getting this error: tar: Removing leading `/' from member names

this is rather a warning and if you follow the first suggestion you will not get it again

share|improve this answer
1  
moved the script to target directory now it works :-) thank you! – Michael Oct 19 '13 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .