Skip to content

Instantly share code, notes, and snippets.

@aerickson
Created October 13, 2011 05:09
Show Gist options
  • Save aerickson/1283442 to your computer and use it in GitHub Desktop.
Save aerickson/1283442 to your computer and use it in GitHub Desktop.
Get total rsync progress using Python
# from https://libbits.wordpress.com/2011/04/09/get-total-rsync-progress-using-python/
import subprocess
import re
import sys
print('Dry run:')
cmd = 'rsync -az --stats --dry-run ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
remainder = proc.communicate()[0]
mn = re.findall(r'Number of files: (\d+)', remainder)
total_files = int(mn[0])
print('Number of files: ' + str(total_files))
print('Real rsync:')
cmd = 'rsync -avz --progress ' + sys.argv[1] + ' ' + sys.argv[2]
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
while True:
output = proc.stdout.readline()
if 'to-check' in output:
m = re.findall(r'to-check=(\d+)/(\d+)', output)
progress = (100 * (int(m[0][1]) - int(m[0][0]))) / total_files
sys.stdout.write('\rDone: ' + str(progress) + '%')
sys.stdout.flush()
if int(m[0][0]) == 0:
break
print('\rFinished')
@ronnymajani
Copy link

import subprocess
import re
import sys

print('Dry run:')


# in_folder = sys.argv[1]
in_folder = "../Albums/"
# out_folder = sys.argv[2]
out_folder = "bin/"

cmd = 'rsync -az --stats --dry-run ' + in_folder + ' ' + out_folder

proc = subprocess.Popen(cmd,
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)

output, err = proc.communicate()

mn = re.findall(r'Number of files: (\d+)', output.decode('utf-8'))
total_files = int(mn[0])

print('Number of files: ' + str(total_files))

print('Real rsync:')

cmd = 'rsync -avz  --progress ' + in_folder + ' ' + out_folder
proc = subprocess.Popen(cmd,
    shell=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)

while True:
    output = proc.stdout.readline().decode('utf-8')
    if 'to-check' in output:
        m = re.findall(r'to-check=(\d+)/(\d+)', output)
        progress = (100 * (int(m[0][1]) - int(m[0][0]))) / total_files
        sys.stdout.write('\rDone: ' + str(progress) + '%')
        sys.stdout.flush()
        if int(m[0][0]) == 0:
            break
print('\rFinished')

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