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.1KB

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