Force tar to ignore/skip errors while compressing files?

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.


Solution 1:

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.

Solution 2:

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.

Solution 3:

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