14

Let's say I do this tar cfzp home.tar.gz /home (takes a while) and a file changes during compression and tar fails, I get "file changed as we read it" and tar stops. I assume home.tar.gz is now incomplete, or was that just the "notice" and not really an error?

Is there some kind of "force" option to make tar finish its work and not abort on errors?

Edit/update: I found "--ignore-failed-read do not exit with nonzero on unreadable files" and at least I think it's working. But need to be careful with the order of the parameters because you can end up with a tar file called "--ignore-failed-read"

Do I need to ignore anything else?

Update: Without "--ignore-failed-read" tar will keep going if a file has been removed "File removed before we read it". However, I think it might be aborting on the "file changed as we read it" error but I don't really know. Hard to compare the archive to the "original" as I have cache files that come and go, etc.

Update: Upon closer observation "file changed as we read it" is more like a notice, it appears tar will keep going if files change while tar is doing its business. But I'll leave the answer open, maybe someone more experienced can add more insight.

PJ Brunet
  • 586
  • 1
  • 5
  • 15
  • What is the error message that you get when tar fails ? – user9517 Dec 11 '13 at 19:16
  • Which time? When compression fails or when the --ignore-failed-read fails? – PJ Brunet Dec 11 '13 at 19:20
  • All are relevant surely. – user9517 Dec 11 '13 at 19:20
  • what do you get in stderror when doing "tar cfzp home.tar.gz --ignore-failed-read /home" ? – Danila Ladner Dec 11 '13 at 19:24
  • I didn't save the error, it was something like "file changed during compression...aborting." Actually "tar cfzp home.tar.gz --ignore-failed-read /home" seems to be working now so I'm not sure what the (file not found) error was from :-/ I edited the question to clear up my confusion with --ignore-failed-read – PJ Brunet Dec 11 '13 at 19:25

4 Answers4

10

Your assumption is correct, "File changed as we read it" is a notice, usually related to files in use (i.e. written to during the creation process) while tar is creating the archive. If consistency is vital, you're better off rsyncing the contents elsewhere i.e.

rsync -avz /my/home/ /somebackupdir/my/home/  # initial sync, followed by 
rsync -avz /my/home/ /somebackupdir/my/home/  # any subsequent sync, repeated
                                              # as often as you feel necessary

This gives you the benefit of having a backup location that will only need to update the diffs before creating the tarball.

Stephan
  • 999
  • 7
  • 11
  • You have the only answer in 3 years, and it's a popular question, so I'm marking this as the accepted answer. Even though I wouldn't use rsync this particular way, it's an interesting answer ;-) – PJ Brunet Dec 11 '16 at 02:39
6

I think the correct answer to your question should be:

Use tar --warning=no-file-changed which will only suppress warnings of kind "%s: file changed as we read it". A general --ignore-failed-read may ignore failures you'd rather like to not ignore.

chwa
  • 61
  • 1
  • 1
1

To compress multiple files, while skipping any missing file:

# This file exists:
> existing_file 

# This file is missing:
rm -f deleted_file 

# Compress but don't fail if deleted_file is missing:
tar -cvzf archive.tar.gz $(ls deleted_file existing_file 2>/dev/null)
echo $? # exit code is 0

# List archive content:
tar tvf archive.tar.gz

  -rw-rw-r-- nmanos/nmanos     0 2020-05-06 10:00 existing_file
Noam Manos
  • 307
  • 1
  • 2
  • 8
0

This answer helped me, thanks! But I wasn't sure how to find a tar error in my log file consisting of many thousand of lines. The man page for tar was no help. However, belatedly, based on the info above about the error msg ("%s:") I realized the key was to grep my log for ':' which spit out my errors (permission problems).

  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://serverfault.com/help/whats-reputation), you will be able to [vote up questions and answers](https://serverfault.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/545190) – RalfFriedl Mar 05 '23 at 00:39