11

Any ideas how to back up a directory structure for which there are some files and/or directories for which you do not have permission to read? I'd like to just ignore those, without backup (tar? jar?) crashing.

2 Answers 2

11

Gnu tar has an option --ignore-failed-read which does just that.

If you aren't using GNU tar, you can either generate a list of files to back up, or a list of files to exclude. GNU find has a -readable predicate that tests whether you have read permission on the file. With other versions of find, to stay with find alone, you'll need to approximate this with file permissions (-perm) and ownership (-user, group), which makes it difficult to cope with groups, ACLs, etc. You can get a precise test by calling the external program or shell builtin test. Here's an example assuming your tar has the option -X to read a newline-separated list of file names to exclude (file names containing newlines are skipped, to avoid parsing problems):

find . ! -name '*
*' -exec sh -c 'for x; do [ -r "$x" ] || printf "%s\\n" "$x"; done' _ {} |
tar -cf backup.tar -X - .
1
  • Worked perfectly first time with --ignore-failed-read, many thanks. System was Red Hat so GNU tar was in place. Commented Jun 3, 2014 at 8:40
2

If you don't have newlines in the paths then you can do this:

find . -type f -readable -print | tar cf test.tar -T -

This may be limited to GNU find and tar and may not work with certain file systems (NFS, FUSE). If there are or may be newlines then use -print0 instead of -print for find and add --null for tar.

You must log in to answer this question.

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