Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to add a progress bar to my tar backup command. I found out that the bar shell script should be able to do this, but I am unable to figure out the correct command.

sudo tar pzcf - /media/data | bar > /media/backups/backup.tar.gz

With the above command I was able to display some information, but no progress bar yet.

share|improve this question
    
possible duplicate of Is there a way to see any tar progress per file? –  Daniel Andersson May 25 '12 at 6:38

1 Answer 1

up vote 3 down vote accepted

Sorry, but this isn't possible. From the file bar itself:

Synopsis:
  'bar' works just like 'cat', but shows a progress bar in ASCII art on stderr.
  The script's main function is meant to be usable in any Bourne shell to be
  suitable for install scripts without the need for any additional tool.

The problem is that, in order to display the progress bar, bar has to know the file's size first.

For extracting an archieve, that's no problem: If backup.tar is 100 MiB big and 50 MiB have been processed so far, we're at 50 %.

However, for creating an archieve, bar reads from stdin, which is infinite for all practical purposes. As the documentation says:

Infinite streams are not nice: the bar is only displayed at 0% and at 100%. [...]


The only way to display progress bars for backup purposes I can think of (besides compiling a custom version of tar with bar) is:

  1. Calculate the accumulated size of the files /media/data.

  2. Create backup.tar and append the files from /media/data one by one.

  3. After each file, calculate the current percentage and display it.

  4. gzip backup.tar (you can use tar for this).

Example:

TOTAL=$(du -b /media/data | grep -oP "^\d+")
CURRENT=0
echo "Archiving:"
IFS="
"
for FILE in $(find /media/data -type f); do 
    tar rf backup.tar $FILE
    CURRENT=$(($CURRENT+$(du -b $FILE | grep -oP "^\d+")))
    echo -en "\r"$((100*($CURRENT-1)/$TOTAL+1))"%"
done
echo
echo "Compressing:"
bar backup.tar | gzip > backup.tar.gz
rm backup.tar

Caution:

  • This will probably slow down your backup progress.
  • File names may not contain newline characters.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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