Monday, 21 January 2019

How to Copy only the changed file-contents on the already existed destination file python3

I have a script which i'm using for copy purpose from one location to another location and the file beneath the Directory structure are all txt files.

This script just evaluates the file size on the source and only copy if the file-size is not zero byte. However, I need to run this script in a cron after a certain Intervals to copy the any incremented data.

So, I need to Know how to copy only the file content which are updated on the source file and then update the destination only for the new-contents and not Just overwrite if its already present at Dest.

code:

#!/bin/python3
import os
import glob
import shutil
import datetime

def Copy_Logs():
    Info_month = datetime.datetime.now().strftime("%B")
    # The result of the below glob _is_ a full path
    for filename in glob.glob("/data1/logs/{0}/*/*.txt".format(Info_month)):
        if os.path.getsize(filename) > 0:
            if not os.path.exists("/data2/logs/" + os.path.basename(filename)):
                shutil.copy(filename, "/data2/logs/")

if __name__ == '__main__':
    Copy_Logs()

I'm looking if there is way to use shutil() in way rsync works or if there is an alternative way to the code i have.

In a nutshell i need to copy only files ones if its not already copied and then only copy the delta if source gets updated.

Note: This Info_month = datetime.datetime.now().strftime("%B") is mandatory to Keep as this determines the current Dir by month name.

Many thanks.



from How to Copy only the changed file-contents on the already existed destination file python3

No comments:

Post a Comment