Wednesday, May 3, 2017

restart rsync when there's no more data receive

after a certain period of connection problem, rsync will not resume its downloading, so the following script will check on rsync data transfer and restart it , when it's necessary.

**** pyrsync.py ****

import subprocess
import re
import os

def killBynName(names):
    com1 = ["ps", "aux"]
    pn = subprocess.Popen(com1 ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pnr = pn.stdout.read()   
    pnl = pnr.split("\n")
    for i in pnl:
        if re.search(names, i):
            # print i
            il = i.split()
            os.system("kill " + il[1])
            break


def rsync(src,dst):
    com1 = "rsync -a -v " + src + " " + dst
    print com1
    os.system(com1)
    killBynName("rsyncc.py")


# src = "rsync://192.168.58.22/ftp"
src = "rsync://10.0.0.2/ftp"
dst = "/home/tun/test/rsync"

srcpool = src + "pool/"
dstpool = dst + "/pool"

#rsync(srcpool,dstpool)
rsync(src,dst)




****** pyrsyncc.py **********
import subprocess
import re
import time
import os

def killByName(names):
    com1 = ["ps", "aux"]
    pn = subprocess.Popen(com1 ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pnr = pn.stdout.read()   
    pnl = pnr.split("\n")
    for i in pnl:
        if re.search(names, i):
            # print i
            il = i.split()
            os.system("kill " + il[1])
            break

def getstatistic():
    com1 = ["ss", "-i"]
    pn = subprocess.Popen(com1 ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    pnr = pn.stdout.read()   
    pnl = pnr.split("\n")
    n = 0
    oldb = 0
    nolink = 1
    currentb = 0
    for i in pnl:
        if re.search(":rsync", i):
            # onlink =
            if re.search("ESTAB", i):
                # print i
                toproc = pnl[n + 1].split()
                # print toproc
                for j in toproc:
                    if re.search("bytes_received", j):
                        jl = j.split(":")
                        currentb = int(jl[1])
                        # if oldb == currentb:
                        #     # print "fuckup"
                        #     nolink = 0
                        # oldb = currentb
                        # print currentb
                        break
           
                nolink = 0

        # else:
        #     print "rsync not up"
        #     nolink = 1
        n +=1
    return (nolink,currentb)
 

os.system("python ./pyrsync.py &")
oldb = 0
count = 0
while True:
    rdata = getstatistic()
    nolink = int(rdata[0])
    bit = int(rdata[1])
    print "bit", bit
    print "nolink", nolink
    if nolink == 1:
        killByName("pyrsync.py")
        os.system("killall rsync")
        time.sleep(1)
        os.system("python ./pyrsync.py &")
        continue
    if bit ==oldb:
        count += 1
        time.sleep(30)
        if count == 3:
            killByName("pyrsync.py")
            os.system("killall rsync")
            os.system("python ./pyrsync.py &")
            count = 0
        print "count", str(count)
    else:
        count = 0

    time.sleep(3)
    oldb = bit





1- save those files in the same directory
- edit the url and dst directory in pyrsync.py
- then run pyrsyncc.py to start them both
$ python pyrsyncc.py

No comments:

Post a Comment