I am using this command to backup some important configuration files:

tar -czvf /var/backups/201102121202.tgz -C / etc/crontab etc/httpd/conf.d/* etc/httpd/conf/httpd.conf etc/httpd/passwords etc/httpd/vhost/* etc/php.ini etc/my.cnf etc/ssh/* etc/hosts etc/sysconfig/iptables var/spool/cron/* etc/passwd etc/group root/iptables_config etc/postfix/main.cf etc/postfix/master.cf etc/postfix/transport etc/postfix/valid_recipients etc/postfix/virtual etc/sysconfig/memcached etc/php.d/apc.ini etc/selinux/config etc/yum.repos.d/* etc/list_of_installed_packages.txt etc/safe-rm.conf home/*/.bash* home/*/.ssh usr/local/bin/* usr/local/sbin/* /etc/pki/tls/certs/mysite.com.crt /etc/pki/tls/private/mysite.key /etc/pki/tls/certs/gd_bundle.crt

When I run it, I get:

tar: etc/httpd/conf.d/*: Cannot stat: No such file or directory
tar: etc/httpd/vhost/*: Cannot stat: No such file or directory
tar: etc/ssh/*: Cannot stat: No such file or directory
tar: var/spool/cron/*: Cannot stat: No such file or directory
tar: etc/yum.repos.d/*: Cannot stat: No such file or directory
tar: home/*/.bash*: Cannot stat: No such file or directory
tar: home/*/.ssh: Cannot stat: No such file or directory
tar: usr/local/bin/*: Cannot stat: No such file or directory
tar: usr/local/sbin/*: Cannot stat: No such file or directory
tar: Removing leading `/' from member names
tar: Error exit delayed from previous errors

It seems I can't use wildcards in the paths.
How can I fix or workaround that?

Thanks,
Dan

link|improve this question

60% accept rate
What shell are you using? – Hello71 Feb 16 '11 at 1:45
I am using bash – Dan Feb 16 '11 at 1:47
feedback

3 Answers

up vote 2 down vote accepted

The likely problem is that you are not in the root directory (/), but your wildcards assume that you are. Remember that the shell expands wildcards on the command-line before envoking the program. So you etc/ssh/* is relative to your current working directory, which likely doesn't exist. Run (cd /; tar ... etc/ssh/* ...) instead.

link|improve this answer
feedback

You're missing a / before the beginning of the names. The shell will only glob names if they're absolute paths. Also, if you do that, there's no need for the use of -C.

link|improve this answer
Good point. I think I did it for a reason. Probably because if I use absolute paths, when I uncompress the package I may be in danger of overwriting the /etc directory of the host system. Did I make myself clear? – Dan Feb 16 '11 at 1:50
the shell will glob any filename that exists, relative or absolute. And adding a / to the paths will anchor them and force an overwrite on extraction of system files and will not allow the files to be extracted into, say, /tmp (not without using chroot anyway). – Arcege Feb 16 '11 at 1:53
@Arcege: The shell, however, will not magically figure out that -C causes tar to change directory. – Hello71 Feb 16 '11 at 1:55
Which is why alternatives from the -C option alone must be made, like (cd /; tar czf ...) or find /etc/httpd/conf.d /etc/ssh /home/*/.ssh ... -print | sed 's;^/;;' > /tmp/tar.lst && tar czfCI $tarfile / /tmp/tar.lst && rm /tmp/tar.lst. But patently saying that "the shell will only glob names if they're absolute paths" is incorrect. If it were correct, then echo * or even echo ./* would not work. – Arcege Feb 16 '11 at 15:48
feedback

Try dropping the * (or maybe /*).


Remainder Syntax ...

When file is the path name of a directory, the action applies to all of the files and (recursively) subdirectories of that directory.

http://www.computerhope.com/unix/utar.htm

link|improve this answer
-1 for linking to computerhope and not the manpage. – Hello71 Feb 16 '11 at 1:51
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.