check updates on each subfolder of subfolders dcmp python

25 Views Asked by At

I am trying to build a back-up script in order to back-up all my pc to an external ssd(lost all my data 3 times by now).

It works great for the files and main folders but until now I am not able to check if there were any updates on the subfolders.

The logic is this: We have 1 source folder and 1 destination folder. If we have a new folder or a new file created in the source folder, copy it to the destination folder. If we have a new or modified file or folder inside any subfolder, copy what is new or modified to the same path in the destination folder.

Example:

dir_src = r"C:\Users\UNIX\Desktop\backup\x"
dir_dst = r"C:\Users\UNIX\Desktop\backup\y"

This work perfect, if I have a new or modified file or folder in source folder.

But, here is the tricky part: if I have a new or modified file or folder in C:\Users\UNIX\Desktop\backup\x\w\a or C:\Users\UNIX\Desktop\backup\x\w\a\new.txt the code won't get it, it just stuck to the main hierarchy. I do not want to copy the entire folder (100gb+) for a modified file or folder that has maybe 7kb because it will take more time than needed.

Attaching the entire code below:


import os, shutil
import os.path, time
import filecmp
from filecmp import dircmp

fob = open(r"C:\Users\UNIX\Desktop\backup\log.txt", "a")
dir_src = r"C:\Users\UNIX\Desktop\backup\x"
dir_dst = r"C:\Users\UNIX\Desktop\backup\y"

left_folder = []
right_folder =[]

def start_log():
    fob.write("\n")
    fob.write("===============")
    fob.write("Started at:      %s" % time.strftime("%c"))
    fob.write("\n")

def write_log():
    fob.write("File name:       %s" % os.path.basename(pathname))
    fob.write("     Last modified date:     %s" %time.ctime(os.path.getmtime(pathname)))
    fob.write("     Copied on:      %s" % time.strftime("%c"))
    fob.write("\n")

def print_diff_files(dcmp):
    global name, left_folder, right_folder, left_file, right_file
    for name in dcmp.diff_files:
        left_file = f"{dcmp.left}\{name}"
        right_file = f"{dcmp.right}\{name}"
        left_folder.append(left_file)
        right_folder.append(right_file)
    for sub_dcmp in dcmp.subdirs.values():
        print_diff_files(sub_dcmp)

start_log()

for w in os.listdir(dir_src):
    name = ""
    pathname = os.path.join(dir_src, w)
    try:
        spathname = os.path.join(dir_dst, w)
        if os.path.isdir(spathname):
            dcmp = dircmp(pathname, spathname)
            print_diff_files(dcmp)
            if name != "":
                for file in left_folder:
                    shutil.copy2(left_file, right_file)
                    write_log()
                    print("New folder version added: " + str(pathname))
                    print(f"Modified files: {right_file}")
        elif not filecmp.cmp(pathname, spathname):
            shutil.copy(pathname, dir_dst)
            write_log()
            print("New file version added: " + str(spathname))
    except:
        if os.path.isdir(pathname):
            shutil.copytree(pathname, spathname, dirs_exist_ok=True)
            write_log()
            print("New folder added: " + str(spathname))
        else:
            spathname = os.path.join(dir_dst, w)
            shutil.copy(pathname, dir_dst)
            write_log()
            print("New file added: " + str(spathname))
fob.close()

print("Done")
0

There are 0 best solutions below