No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

restor.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2019 Aiden Holmes (aidenholmes@teknik.io)
  3. # This program is licensed under the Apache License 2.0
  4. # You are free to copy, modify, and redistribute the code.
  5. # See LICENSE file.
  6. # backr restor tool in python
  7. import sys
  8. import os
  9. import hashlib
  10. import shutil
  11. from distutils.dir_util import copy_tree
  12. import pickle as pickle
  13. for q in range(len(sys.argv)):
  14. if "-h" in sys.argv or "--help" in sys.argv:
  15. print("restor tool for backr")
  16. print("usage: restor.py [-h|--help]")
  17. print("[-h|--help] - print this help")
  18. exit()
  19. def main():
  20. # set variables
  21. cwd = os.getcwd()
  22. basename = os.path.basename(cwd)
  23. # get backup location
  24. if os.path.isfile(".backr-location"):
  25. with open('.backr-location', 'r') as myfile:
  26. backup_location = myfile.read()
  27. else:
  28. print(".backr-location file not found")
  29. sys.exit(1)
  30. qhash = hashlib.sha1(cwd.encode("UTF-8")).hexdigest()[:7]
  31. backup_location += "/" + basename + "-" + qhash
  32. backbase = backup_location
  33. vc_file = backbase+"/backtrack.txt"
  34. print("listing backups...")
  35. print()
  36. if os.path.exists(vc_file):
  37. data = pickle.load(open(vc_file, "rb"))
  38. possible_backups = ["placeholder"]
  39. for i in range(0, len(data)):
  40. print("backup "+str(i)+" in "+data[i])
  41. if not os.path.exists(data[i]):
  42. print(" backup deleted or missing!")
  43. else:
  44. if possible_backups == ["placeholder"]:
  45. possible_backups = [i]
  46. else:
  47. possible_backups += [i]
  48. # print most recent available backup
  49. print()
  50. print("most recent available backup:")
  51. try:
  52. print(data[possible_backups[-1]])
  53. except:
  54. print("no possible backups")
  55. print()
  56. has_number = False
  57. while not has_number:
  58. try:
  59. backup_number = input("Choose a number to restore from: ")
  60. if backup_number in str(possible_backups):
  61. backup_number = int(backup_number)
  62. has_number = True
  63. else:
  64. print("That is not an option.")
  65. except ValueError:
  66. print("Enter a number.")
  67. else:
  68. print("backtrack file not found in "+backbase)
  69. sys.exit(1)
  70. restore_from = data[backup_number]
  71. has_restore_location = False
  72. while not has_restore_location:
  73. restore_location = input("Enter a location to restore to: ")
  74. if os.path.isdir(restore_location):
  75. print("Will restore to "+restore_location)
  76. has_restore_location = True
  77. else:
  78. print(restore_location+" does not exist")
  79. restore_location += "/"
  80. restore_location += basename
  81. if not os.path.exists(restore_location):
  82. os.makedirs(restore_location)
  83. if not ".tar.gz" in restore_from:
  84. copy_tree(restore_from, restore_location)
  85. else:
  86. shutil.copyfile(restore_from, restore_location+"/"+os.path.basename(restore_from))
  87. print("Restored to " + restore_location)
  88. if __name__ == "__main__":
  89. try:
  90. main()
  91. except KeyboardInterrupt:
  92. print()
  93. print("exit")
  94. sys.exit(0)