4

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.

2

1 Answer 1

4

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.

You must log in to answer this question.

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