-1

I am a beginner at Linux and a big time beginner at bash scripting. I am trying to write a bash script that will backup my home directory AND show me the progress. I found a script called bar that allows me to show a progress bar when deflating a tar.bz2 file, but not when creating one. I found your code and am trying it, but it gives me an error: lsof: no process ID specified lsof 4.81

I go your code from this page: How to add a progress bar to a shell script? Here is the code:

#! /bin/bash
tar -jcf userhomeBU$(date +%Y&m&d)".tar.bz2" /home/user
lsof -o0 -o -p $PID |
awk '
            BEGIN { CONVFMT = "%.2f" }
            $4 ~ /^[0-9]+r$/ && $7 ~ /^0t/ {
                    offset = substr($7, 3)
                    fname = $9
                    "stat -f %z '\''" fname "'\''" | getline
                    len = $0
                    print fname, offset / len * 100 "%"
            }
    '

My home directory is backed up just fine, but I get no progress bar due to the error.

It looks like this will use the PID to watch to see when it is done. I am thinking it uses the PID and not the name of the file because the name will be unknown at the very start of the script. Can you please explain what this script is doing exactly, and why I am not getting a PID for it to watch. I have put my backup code in front and at the end but it does not show a bar, and I get the same error just at a different time.

Help!

Community
  • 1
  • 1
Newbie
  • 1

3 Answers3

0

Here is an adaptation of one of the other answers from your original post. I tested it and it works on my machine:

tar -Ocf - /home/user | pv -i 1 -w 50 -berps `du -bs /home/user | awk '{print $1}'` | bzip2 - >  userhomeBU$(date +%Y%m%d)".tar.bz2"
Brian Clements
  • 3,787
  • 1
  • 25
  • 26
0

Try assigning the entire tar command to a variable, passing the variable to pidof:

...
TAR_CMD='tar -jcf userhomeBU$(date +%Y&m&d)".tar.bz2" /home/user'  
lsof -o0 -o -p `pidof -s $TAR_CMD` | \   
awk ...
Wesley Rice
  • 2,711
  • 19
  • 8
0

It is not a very big home dir, but I have a few big files in it. It takes about 45 seconds to backup due to a few iso's and it is in a VM. Also, I know it is not working because there is no backup file created. There is no & in the script. When I run it I get the following output:

[1] stopped       tar -Ocf - /home/user | pv -i 1 -w 50 -berps 'du -bs /home/user | awk '{print $1}'' | bzip2 - > userBU(date +%Y%m%d)".tar.bz2" 
which is just a copy of my code. The number in the [ ] goes up by one with each run. The first ' and last ' in the code are really the single ' under the tilda, but the ` was not showing up in this post.
newbie
  • 1
  • 1