Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. from time import strftime as date
  2. import os, glob
  3.  
  4. ##
  5. ##  FUNCTIONS
  6. ##
  7. def getFiles(rootdir):
  8.     fileList = []
  9.     for root, subFolders, files in os.walk(rootdir):
  10.         for file in files: fileList.append(os.path.join(root,file).replace("\\","/"))
  11.     return fileList
  12.  
  13. def cmpFile(file1,file2):
  14.     f1=open(file1,"rb");f2=open(file2,"rb")
  15.     val=(f1.read()==f2.read())
  16.     f1.close();f2.close()
  17.     return val
  18.  
  19. def cmpFolder(folder1,folder2):
  20.     set1=[i[len(folder1)+1:] for i in getFiles(folder1)]
  21.     set2=[i[len(folder2)+1:] for i in getFiles(folder2)]
  22.     if set1!=set2 or : return False
  23.     else:
  24.         matches=0
  25.         for i in set1:
  26.             for j in set2:
  27.                 if i==j:
  28.                     if cmpFile(folder1+"/"+i,folder2+"/"+j): matches+=1
  29.         if matches!=len(set1): return False
  30.     return True
  31.  
  32. def doDelete(dir):
  33.   os.system("RMDIR /S /Q \"%s\""%dir)
  34.  
  35. def doBackup(bkuploc,dir):
  36.   os.system("XCOPY /E /I /K /Q \"%s\" \"%s\""%(bkuploc,dir))
  37.  
  38. def getDrive():
  39.     x=input("Backup Array Drive Location: ")
  40.     if x in ("C","D","E","F"):
  41.         print("Invalid Drive")
  42.         return getDrive()
  43.     else:
  44.         return x
  45.    
  46. def restamp(drive):
  47.     f1=drive+":/Backup Contents *.txt"
  48.     f2=drive+":/Backup Contents "+date("%m-%d-%Y")+".txt"
  49.     os.system("RENAME %s %s"%(f1,f2))
  50.    
  51. ##
  52. ##  INIT
  53. ##  
  54. backedup=[]
  55. CoreLocation="C:/Documents and Settings/Tyler/Desktop/Core/"
  56. BkupDirs=(
  57.   "Downloads",
  58.   "Games/DEFCON",
  59.   "Games/Minecraft",
  60.   "Games/Katawa Shoujo",
  61.   "Media/Images", #porn
  62.   "Media/Documents",
  63.   "Media/Music",
  64.   "Internet/Firefox",
  65.           )
  66. x=getDrive():
  67. DriveLoc=getDrive()
  68.  
  69. print()
  70. print("Initiating Core Backup")
  71. print()
  72.    
  73. ##
  74. ##  ANALYSIS & BACKUP
  75. ##
  76. for i in BkupDirs:
  77.   print("-------")
  78.   print("Analyzing "+i.replace("/","\\"))
  79.   if cmpFolder(CoreLocation+i,DriveLoc+":/"+i):
  80.     print(" >%s does not require updating"%i.replace("/","\\"))
  81.   else:
  82.     print(" >%s will be updated"%i)
  83.     print("  >Destroying outdated %s"%i)
  84.     doDelete(DriveLoc+":/"+i)
  85.     print("  >Backing up %s"%i)
  86.     doBackup(CoreLocation,DriveLoc+":/"+i)
  87.     backedup.append(i)
  88.     print(" >%s is now up to date"%i)
  89. print()
  90.  
  91. ##
  92. ##  BACKUP CONTENTS UPDATE
  93. ##
  94. print("-------")
  95. print("The following directories were backed up:")
  96. for i in backedup:
  97.   print(" "+i)
  98. print("-------")
  99. print("Flushing new backup contents to record")
  100. restamp(DriveLoc)
  101. filepath=glob.glob(DriveLoc+":/Backup Contents *.txt")[0]
  102. bcont=open(filepath,"w+")
  103. bcont.write(
  104.   "\n".join(getFiles(DriveLoc+":/"))
  105.   )
  106. bcont.close()
  107. print("Backup completed")
  108.  
  109. input("Hit enter to exit"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement