108

I have written a little script that tars and compresses a list of directories + files.

The script appears to run succesfully, in that a useable .tar.gz file is created after the script runs.

However, I get this annoying message after the script finishes:

tar: Exiting with failure status due to previous errors

I do not see any error messages whilst the script is working, and like I said, the produced file can be uncompressed with no warnings/errors. Since I am using this as part of my backup, I want to make sure that I am not ignoring something serious.

What are the possible reasons that this error/warning message is being produced - and can I safely ignore it?. If I cant ignore it, what are the steps to diagnose and resolve the error?

I am running on Ubuntu 10.0.4

8 Answers 8

138

You will get that message if, for any reason, tar can't add all of the specified files to the tar. One if the most common is not having read permission on one of the files. This could be a big problem since you are using this for backup. If you are using the -v flag, try leaving it off. This should reduce the output and let you see what is going on.

3
  • 13
    +1 for the suggestion (I was using the 'verbose' option previously). I found that there was a permission issue on at least one of the files. At least now I know how to resolve this. many thanks
    – morpheous
    Jul 29, 2010 at 14:26
  • 2
    yes using sudo fixed the issue
    – whizcreed
    Nov 17, 2015 at 12:44
  • 1
    ditching the -v flag is great advice and helped me solve my problem. 'verbose' obscures the cause of the failure behind a wall of text Jul 1, 2019 at 15:43
47

The problem is the f argument. It takes the next value as the filename, so it must be the last argument:

tar cvzf output.tgz folder

or:

tar -cvzf output.tgz folder

These are both the same and don't produce an error.

1
  • Solved my problem. I was trying tar -zcvfp giving error but when tried tar -zxvpf then all fine. Thanks.
    – drmaa
    Feb 27, 2018 at 10:28
11

Sometimes backing up files that might change during the backup like logfiles, you might find useful the tar option '--ignore-failed-read' (I'm on Debian Linux, not sure for non gnu tar).

Standard output and error can be redirected in 2 different files with something like:

LOGDIR='/var/log/mylogdir' 
LOG=${LOGDIR}/backup.log 
ERRLOG=${LOGDIR}/backup.error.log 
DATE=$(date +%Y-%m-%d)
HOSTNAME=$(hostname)
DATA_DIRS='/etc /home /root'

tar --ignore-failed-read -f ${BACKUP_DIR}/${HOSTNAME}-${DATE}.tgz -cvz ${DATA_DIRS} > $LOG 2> $ERRLOG

I find this to be generally safe, but please be careful though as tar won't stop ...

0
7

I was having the same issue and none of the above answers worked for me. However, I found that running the following command worked:

tar -cpzf /backups/fullbackup.tar.gz --exclude=backups --exclude=proc --exclude=tmp --exclude=mnt --exclude=sys --exclude=dev --exclude=run /

The errors that were being referred to in tar: Exiting with failure status due to previous errors can be identified by turning off the -v option. Upon review, the errors came from directories like /run and /sys.

By excluding these directories, it works just fine. Hope this helps anyone with a similar issue.

3

I had the same problem. All i did was to remove the dash ("-") from the command.

Instead of typing it as

tar -cvfz output.tar.gz folder/

try typing it as

tar cvfz output.tar.gz folder/

I am unaware of why the dash was causing problems in my case but at least it worked.

4
  • 7
    You probably tested this with GNU tar. Your confusion comes from the fact that it accepts two different styles of options - the "old-style" tar options without a dash and the "standard unix" options with a dash. The options with dash require an argument of an option to follow the option. So in this case the argument for -f should be output.tar.gz and there must not be z in between. Without re-ordering of the options it would correctly be: tar -cvf output.tar.gz -z folder/. The old style expects all the options in one place and all the arguments follow. Oct 19, 2013 at 12:40
  • See the GNU tar documentation and also for example Tru64 UNIX tar man page. Oct 19, 2013 at 12:42
  • 1
    @pabouk That comment seemed worthy of being an answer by itself. Please add the contents of that comment as an answer to this question so that credit goes where credit is due. May 29, 2014 at 13:46
  • I also had problems, but it was a file with the name -v so apparently tar has some bugs with regards to what files it can backup... this should not be a problem. my workaround since rm and mv would not work, was to use a file manager. so apparently mv and rm are buggy too. i tried mv '-v' v and rm '-v' but got error messages. tar mentioned it could not stat the tar file. was using -cfjv Mar 15, 2015 at 5:15
2

You have misunderstood an earlier answer. The problem is not the -, it is where the f is in your argument list.

tar cvfz target.tgz <files>

Will try to create an archive called "z", as that is the text after f. The error message is because tar can't find "target.gz" to add to archive "z".

tar cvzf target.tgz <files>

Will correctly create target.tgz and add files to it. This is because target.tgz is the first text after the f argument.

1
  • This should be added as a comment to the "confused" reply. Unfortunately you will be allowed to add such comments only after gaining 50 points of reputation. To the subject: Did you test it? I tested it with GNU tar 1.26 and the variants with and without dash are really different as described in the reply of jack. It behaves as it is written in the reply. Oct 19, 2013 at 12:14
0

Usually you can ignore that message. If there are any changes (such as file deletions/creations/modifications) to underlying directory tree during tar creation, it will throw that message. Also if there special files like device nodes, fifos and so on, they will cause that warning.

Are you sure you can't see any culprit files? Try with tar cvfz yourtarball.tgz /your/path

0

I had a similar issue untarring a file I had received. Turns out I didn't have permission to write the files in the archive owned by root. Using sudo fixed it.

You must log in to answer this question.

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