Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
Last active June 18, 2023 18:51
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JohannesBuchner/4d61eb5a42aeaad6ce90 to your computer and use it in GitHub Desktop.
Save JohannesBuchner/4d61eb5a42aeaad6ce90 to your computer and use it in GitHub Desktop.
Progress bar for rsync
"""
Progress bar for rsync
========================
Shows file progress and total progress as a progress bar.
Usage
---------
Run rsync with -P and pipe into this program. Example::
rsync -P -avz user@host:/onefolder otherfolder/ | python rsyncprogress.py
It will show something like this::
65%|30652/117251|################ |ETA: 0:04:20|File:100%|Illustris-3/...68/gas2_subhalo_5885.hdf5|0:00:00|156.48kB/s
^ File progress
^ File name ^ ETA ^ Speed
^ Overall progress bar ^ and ETA
^ total number of files
^ files to be checked
^ Overall progress
You need the progressbar-latest package installed (see PyPI).
License
-----------
Copyright (c) 2015-2022 Johannes Buchner
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import progressbar
import sys
def read_stdin():
line = ''
while sys.stdin:
try:
c = sys.stdin.read(1)
except IOError as e:
print(e)
continue
if c == '\r':
# line is being updated
yield line
line = ''
elif c == '\n':
# line is done
yield line
line = ''
elif c == '':
break
else:
line += c
first_update = None
widgets = [progressbar.Percentage(), None, progressbar.Bar(), progressbar.ETA(), None]
pbar = None
short_file_name = '???'
for line in read_stdin():
parts = line.split()
if len(parts) == 6 and parts[1].endswith('%') and (parts[-1].startswith('to-check=') or parts[-1].startswith('ir-chk=')):
# file progress -P
file_progress = parts[1]
file_speed = parts[2]
file_eta = parts[3]
istr, ntotalstr = parts[-1].split('=')[1].rstrip(')').split('/')
ntotal = int(ntotalstr)
i = int(istr)
j = ntotal - i
total_progress = j * 100. / int(ntotal)
widgets[1] = '|%s/%s' % (i, ntotal)
widgets[-1] = '|File:%s|%s|%s|%s' % (file_progress, short_file_name, file_eta, file_speed.rjust(10))
if pbar is None:
first_update = j
pbar = progressbar.ProgressBar(widgets=widgets,
maxval=ntotal - first_update).start()
pbar.maxval = ntotal - first_update
pbar.update(j - first_update)
#sys.stderr.write('Total:%.1f%%|File:%s|%s|%s|%s|\r' % (total_progress, file_progress,
# short_file_name, file_eta, file_speed))
#sys.stderr.flush()
elif not line.startswith(' ') and line.strip() != '':
# total progress
file_name = line
if len(parts) == 6:
print(parts[1].endswith('%'), parts[-1].startswith('to-check='),end='')
if len(file_name) > 40:
short_file_name = file_name[:12] + '...' + file_name[-(28-3):]
else:
short_file_name = file_name
if pbar is not None:
pbar.finish()
@jonnyhtw
Copy link

... aaand now using a different Python version I've reverted back to maxval! 🤣

@MuMaestro
Copy link

Is this python2 ? Seems to be broken, as progressbar is not being found for importing even after install. Is there a specific module to use ?

@JohannesBuchner
Copy link
Author

try progressbar-latest

@JohannesBuchner
Copy link
Author

And yes, it is written in python2, try 2to3 to convert.

@ioogithub
Copy link

ioogithub commented Apr 6, 2022

I have having the same problem as MuMaestro. I tried installing progressbar, progressbar2 and progressbar-latest. I always get 'ImportError: No module named progressbar'.

I installed 2to3 and ran it against the script but it gave me a different error:

if len(parts) == 6: print(parts[1].endswith('%'), parts[-1].startswith('to-check='), end=' ')
                                                                                            ^
SyntaxError: invalid syntax

Is there a current solution that gets this script to work?

@JohannesBuchner
Copy link
Author

I cannot reproduce your syntax error. progressbar-latest should give you the progressbar module (test with import progressbar), otherwise there are issues with your python setup.

@JohannesBuchner
Copy link
Author

Updated script to python3.

@JohannesBuchner
Copy link
Author

Updated again, works with progressbar-latest on python3. Some rsync outputs have ir-chk= instead of to-check= and this is now supported.

@hongbo-miao
Copy link

hongbo-miao commented Aug 15, 2022

Hi @JohannesBuchner thanks for the script! I tried this script on

- Ubuntu 22.04
- Python 3.8.10
- progressbar-latest 2.4

However, the data did copy, however, it shows nothing when copy:

$ rsync -P -avz -e "ssh -i ~/.ssh/id_ed25519" root@x.x.x.x:/data/logs/ /home/ubuntu/logs/ | python rsyncprogress.py
$

Any idea? Thanks!

I found in my case, as I got ['95,543', '100%', '91.12MB/s', '0:00:00', '(xfr#1,', 'to-chk=4/7)'], so

parts[-1].startswith('to-check=') or parts[-1].startswith('ir-chk=')

needs to be

parts[-1].startswith('to-check=') or parts[-1].startswith('to-chk=') or parts[-1].startswith('ir-chk=')

Maybe line 94 should do similar update (?)

😀

@hongbo-miao
Copy link

Maybe we can move this to a GitHub repo, so everyone can contribute? 😀

@Mka0001
Copy link

Mka0001 commented Jun 17, 2023

I have come up with another solution. Try it >>

@JohannesBuchner
Copy link
Author

This lives here now: https://github.com/JohannesBuchner/rsync-progress

Please open issues and pull requests there instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment