简体   繁体   English

根据日期时间同步文件夹

[英]synchronise folders according to datetime

I am very new to python and trying to work on this script which recieves data from multiple ftp sites and download yesterday data according to date directory to my local folder. 我是python的新手,尝试使用此脚本,该脚本从多个ftp站点接收数据,并根据日期目录将昨天的数据下载到我的本地文件夹中。 but if the receives fails on any day it would not update that day records and would go to the next day. 但是,如果在任何一天接收失败,它将不会更新该天的记录,而是转到第二天。 I want to sync the files that even if it is missed on particular it should complete sync teh new files to local folder I have tried looking at rsync but need your help to process it on the script.this is my script. 我想同步文件,即使它特别丢失,也应该将新文件同步到本地文件夹。我尝试查看rsync,但是需要您的帮助才能在脚本上处理它。这是我的脚本。

MAX_CHILDREN = 16
    ftp_site_prog = "/usr/local/bin/ftp_site.py"

    class SpawnQ:
        def __init__(self, max_pids):
            self.max_pids = max_pids 
            self.queue = []
            tmp = re.split("/", ftp_site_prog)
            self.my_name = tmp[-1]


        def addQ(self, site_id):
            self.queue.append(site_id)
            return

        def runQ(self):
            while (len(self.queue) != 0):
                # Check how many sessions are running
                cmd = """ps -ef | grep "%s" | grep -v grep""" % self.my_name
                num_pids = 0
                for line in os.popen(cmd).readlines():
                    num_pids = num_pids + 1

                if (num_pids < self.max_pids):
                    site_id = self.queue.pop()
                    # print site_id
                    # print "Forking........"
                    fpid = os.fork()
                    if fpid:
                        # print "Created child: ", fpid
                        os.waitpid(fpid, os.WNOHANG)
                    else:
                        # print "This is the Child"

                        # Exec the ftp_site
                        arg_string = "%s" % site_id
                        args = [arg_string]


           os.execvp(ftp_site_prog, (ftp_site_prog,) + tuple(args))
    how to  call rsync on my py script.//os.system("rsync -ftp_site_prog, (ftp_site_prog,)+ tuple(args))
        sys.exit(0)
                else:
                    # print "Waiting for a spare process...."
                    time.sleep(10)
            return

    # Get a list of the sites
    db_obj = nprint.celDb()
    site_list = db_obj.get_all_site_ids()

    myQ = SpawnQ(MAX_CHILDREN)

    for site_id in site_list:
        myQ.addQ(site_id)

    myQ.runQ()

    # Wait until we only have the parent left
    # Check how many sessions are running
    tmp = re.split("/",ftp_site_prog)
    ftp_name = tmp[-1]
    cmd = """ps -ef | grep "%s" | grep -v grep""" % ftp_name

    num_pids = MAX_CHILDREN
    while (num_pids > 0):
        num_pids = 0
        for line in os.popen(cmd).readlines():
            num_pids = num_pids + 1

        time.sleep(60)

    today = datetime.date.today()
    daydelta = datetime.timedelta(days=1)
    yesterday = today - daydelta

Much of this can be accomplished with the ftplib module for the retrieval of files from standard FTP servers. 可以使用ftplib模块从标准FTP服务器检索文件来完成许多工作。 If you are dealing with SFTP servers you can use the paramiko library. 如果要处理SFTP服务器,则可以使用paramiko库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM