Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. # Backup files through git and local
  2. # Setup git to push to a remote or local network to secure backups outside of local machine if hardware failure occurs
  3. # Setup backup folder to a seperate drive
  4.  
  5. # Original: Jan 5 2015
  6. # Update1: Feb 13 2018
  7. # Update2: Mar 16 2019
  8.  
  9. """ Example of txt file
  10.  
  11. ---- Git Folder ----
  12. D:\Projects\...\SomeProject
  13. ---- Copy From -----
  14. C:\Projects\...\SomeProject
  15. ---- Copy To -------
  16. D:\Projects\...\SomeProject
  17.  
  18. """
  19.  
  20. import errno
  21. import shutil
  22. import os
  23. import sys
  24. import git
  25. import datetime
  26.  
  27. ignore_pattern = ['.git','tmp']
  28.  
  29. def copytree(src, dst, symlinks=False, ignore=shutil.ignore_patterns(ignore_pattern)):
  30. # print("Dst : " + str(dst))
  31. if (ignore_pattern[0] == str(dst[-4:])):
  32. pass
  33. else:
  34. if not os.path.exists(dst):
  35. os.makedirs(dst)
  36. for item in os.listdir(src):
  37. s = os.path.join(src, item)
  38. d = os.path.join(dst, item)
  39. if os.path.isdir(s):
  40. copytree(s, d, symlinks, ignore)
  41. else:
  42. if not os.path.exists(d) or os.stat(s).st_mtime - os.stat(d).st_mtime > 1:
  43. shutil.copy2(s, d)
  44. print("Copying: " + str(item))
  45.  
  46. def opentext(name, parameter):
  47. content2 = []
  48. file = open(name, parameter)
  49. content = file.readlines()
  50. file.close()
  51. for file in content:
  52. file = file.split("\n")
  53. content2.append(file[0])
  54. return content2
  55.  
  56. def gitcommands(file_location):
  57. repo = git.Repo(file_location[1])
  58. repo.git.add(".")
  59. t = datetime.datetime.today()
  60. commitMsg = str(datetime.date.today()) + " @ " + str(t.hour) + ":" + str(t.minute)
  61. repo.git.commit('-m', commitMsg)
  62. print("Successfully commited files to backup drive")
  63.  
  64. def backup_files_to_directory(file_location):
  65. isSourceLocationExist = False
  66. isBackupLcationExist = False
  67.  
  68. originalSource = file_location[3]
  69. backupLocation1 = file_location[5]
  70.  
  71. if os.path.isdir(originalSource):
  72. isSourceLocationExist = True
  73.  
  74. if os.path.isdir(backupLocation1):
  75. isBackupLcationExist = True
  76.  
  77. if (isSourceLocationExist == True and isBackupLcationExist == True):
  78. copytree(originalSource, backupLocation1)
  79. print("\nSuccessfuly backed up to " + backupLocation1)
  80. return True
  81. else:
  82. reason = ""
  83. if (isSourceLocationExist == False):
  84. reason += "\n Source folder does not exist"
  85. if (isBackupLcationExist == False):
  86. reason += "\n Backup folder does not exist"
  87. print("\nNot able to backup files: " + reason)
  88. return False
  89.  
  90. def shutdown():
  91. t = datetime.datetime.today()
  92. # Shutdown between the hours of 10pm to 12am
  93. if (t.hour >= 22 and t.hour <= 24):
  94. os.system('shutdown -s -t 10')
  95. # Shutdown between the hours of 12am to 4am
  96. if (t.hour >= 0 and t.hour < 4):
  97. os.system('shutdown -s -t 10')
  98.  
  99. def main():
  100. didFileBackupSuccessfuly = False
  101. location = opentext("directoryToBackup.txt", "r")
  102. print("Starting backup...")
  103. didFileBackupSuccessfuly = backup_files_to_directory(location)
  104.  
  105. if (didFileBackupSuccessfuly == True):
  106. try:
  107. gitcommands(location)
  108. except Exception as e:
  109. print("Could not backup with git\n" + str(e))
  110. shutdown()
  111.  
  112. if __name__ == "__main__":
  113. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement