File and Directory Backup, Part 1

Using Python and PyQt5

Photo by Jan Antonin Kolar from Unsplash
import osimport sysimport timeimport filecmpimport subprocess
dirs = (
(r”C:\Users\mount\source\repos”, r”M:\_BACKUP\REPOS”),
(r”D:\mount\Downloads”, r”M:\_BACKUP\DOWNLOADS”),
(r”D:\mount\Documents”, r”M:\_BACKUP\DOCUMENTS”),
(r”D:\Internet-Marketing”, r”M:\_BACKUP\IM”),
(r”D:\_HOLOSYNC”, r”M:\_BACKUP\HOLOSYNC”),
(r”D:\mount\Music”, r”M:\_BACKUP\MUSIC”),
(r”D:\_BIZ”, r”M:\_BACKUP\BIZ”),
(r”D:\_PWA”, r”M:\_BACKUP\PWA”),
(r”C:\data\db”, r”M:\_BACKUP\DB”),
(r”C:\ProgramData\MongoDB”, r”M:\_BACKUP\MONGODB “),
)
def ensure_directory( destination ):    directory = os.path.dirname( destination )
if not os.path.exists( directory ):
os.makedirs( directory )
def compare_directories( source, destination ):    try:
comp = filecmp.dircmp( source, destination )
common = sorted( comp.common )
except:
return False

left = sorted( comp.left_list )
right = sorted( comp.right_list )
if left != common or right != common:
return False

if len( comp.diff_files ):
return False

for subdir in comp.common_dirs:
left_subdir = os.path.join( source, subdir )
right_subdir = os.path.join( destination, subdir )
return compare_directories( left_subdir, right_subdir )

return True
def log_directory():    now = time.strftime(“%Y-%m-%d___%H-%M”)
direct = os.path.dirname(“C:\\Users\\mount\\source\\repos\\MyDashboard\\LOG\\”)
directory = direct + ‘\\’ + now + ‘\\’
if not os.path.exists( directory ):
os.makedirs( directory )
return directory
 — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — 
ROBOCOPY :: Robust File Copy for Windows
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Started : Monday, January 18, 2021 7:53:59 PM
Source : C:\Users\mount\source\repos\
Dest : M:\_BACKUP\REPOS\

Files : *.*

Options : *.* /S /DCOPY:DA /COPY:DAT /XX /XO /MT:128 /R:2 /W:5

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Newer 353 C:\Users\mount\source\repos\trading\get_symbols.py
100%
Newer 26307 C:\Users\mount\source\repos\trading\ubStock_Research.py
100%
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Total Copied Skipped Mismatch FAILED Extras
Dirs : 618 618 612 0 0 89
Files : 34096 9 34087 0 0 119
Bytes : 2.964g 76.6k 2.964g 0 0 108.21 m
Times : 0:01:09 0:00:16 0:00:00 0:00:15
count = 0
logg = log_directory()
lenn = len(dirs)
>robocopy source_directory destination_directory switches
 /xo = only newer versions of file,
/s = all occupied sub-directories,
/MT:nn = # of threads (maximum=128, default=8)
/xx = copy source file even when destination file does not exist
/LOG+ = a log is created for every d in dirs. /LOG+ appends all logs to one file
/r:n = number of times to retry, default = 1,000,000
/w:nn = number of seconds to wait before retrying
for dir in dirs:    count += 1
src, dst, swt = dir[0], dir[1], f”/XX /r:2 /xo /s /w:5 /MT:128 /LOG+:{logg}_BACKUP.log”
status = (compare_directories(src, dst))
    if status == False:

ensure_directory( dst )
compare_directories( src, dst )
cmnd = f”robocopy {src} {dst} {swt}”
copi = subprocess.Popen( cmnd, shell=False )
code = copi.wait()

codes = range(0,9)

if count == lenn and code in codes:
write_txt_file( “oo.txt”, “this file has changed!” )

sys.exit()

Retired, Self taught in python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store