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 destination.
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 it's not already copied and then only copy the delta if source gets updated.
Note: The Info_month = datetime.datetime.now().strftime("%B")
is mandatory to keep as this determines the current directory by month name.
Edit:
Just having another raw idea if we can use filecmp
with shutil.copyfile
module to compare files and directories but i'm not getting how to fit that into the code.
import os
import glob
import filecmp
import shutil
import datetime
def Copy_Logs():
Info_month = datetime.datetime.now().strftime("%B")
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)) or not filecmp.cmp("/data2/logs/" + os.path.basename(filename), "/data2/logs/"):
shutil.copyfile(filename, "/data2/logs/")
if __name__ == '__main__':
Copy_Logs()