qvm-backup-restore 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from qubes.qubes import QubesException
  24. from qubes.backup import backup_restore_header
  25. from qubes.backup import backup_restore_prepare
  26. from qubes.backup import backup_restore_print_summary
  27. from qubes.backup import backup_restore_do
  28. import sys
  29. from optparse import OptionParser
  30. import os
  31. import sys
  32. import getpass
  33. def main():
  34. usage = "usage: %prog [options] <backup-dir> [vms-to-be-restored ...]"
  35. parser = OptionParser (usage)
  36. parser.add_option ("--skip-broken", action="store_true", dest="skip_broken", default=False,
  37. help="Do not restore VMs that have missing templates or netvms")
  38. parser.add_option ("--ignore-missing", action="store_true", dest="ignore_missing", default=False,
  39. help="Ignore missing templates or netvms, restore VMs anyway")
  40. parser.add_option ("--skip-conflicting", action="store_true", dest="skip_conflicting", default=False,
  41. help="Do not restore VMs that are already present on the host")
  42. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  43. help="Force to run, even with root privileges")
  44. parser.add_option ("--replace-template", action="append", dest="replace_template", default=[],
  45. help="Restore VMs using another template, syntax: "
  46. "old-template-name:new-template-name (may be "
  47. "repeated)")
  48. parser.add_option ("-x", "--exclude", action="append", dest="exclude", default=[],
  49. help="Skip restore of specified VM (may be repeated)")
  50. parser.add_option ("--skip-dom0-home", action="store_false", dest="dom0_home", default=True,
  51. help="Do not restore dom0 user home dir")
  52. parser.add_option ("--ignore-username-mismatch", action="store_true", dest="ignore_username_mismatch", default=False,
  53. help="Ignore dom0 username mismatch while restoring homedir")
  54. parser.add_option ("-d", "--dest-vm", action="store", dest="appvm",
  55. help="The AppVM to send backups to")
  56. parser.add_option ("-e", "--encrypted", action="store_true", dest="decrypt", default=False,
  57. help="The backup is encrypted")
  58. parser.add_option ("-z", "--compressed", action="store_true", dest="compressed", default=False,
  59. help="The backup is compressed")
  60. (options, args) = parser.parse_args ()
  61. if (len (args) < 1):
  62. print >> sys.stderr, "You must specify the backup directory (e.g. /mnt/backup/qubes-2010-12-01-235959)"
  63. exit (0)
  64. backup_dir = args[0]
  65. vmlist = args[1:]
  66. #if not os.path.exists (backup_dir):
  67. # print >> sys.stderr, "The backup directory doesn't exist!"
  68. # exit(1)
  69. host_collection = QubesVmCollection()
  70. host_collection.lock_db_for_writing()
  71. host_collection.load()
  72. restore_options = {}
  73. if options.ignore_missing:
  74. restore_options['use-default-template'] = True
  75. restore_options['use-default-netvm'] = True
  76. if options.replace_template:
  77. restore_options['replace-template'] = options.replace_template
  78. if not options.dom0_home:
  79. restore_options['dom0-home'] = False
  80. if options.ignore_username_mismatch:
  81. restore_options['ignore-username-mismatch'] = True
  82. if options.exclude:
  83. restore_options['exclude'] = options.exclude
  84. appvm = None
  85. if options.appvm is not None:
  86. appvm = host_collection.get_vm_by_name(options.appvm)
  87. if appvm is None:
  88. print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
  89. exit(1)
  90. passphrase = getpass.getpass("Please enter the pass phrase that will be used to decrypt/verify the backup: ")
  91. passphrase = passphrase.decode(sys.stdin.encoding)
  92. print >> sys.stderr, "Checking backup content..."
  93. restore_info = None
  94. try:
  95. restore_info = backup_restore_prepare(
  96. backup_dir,
  97. passphrase=passphrase,
  98. options=restore_options,
  99. host_collection=host_collection,
  100. encrypted=options.decrypt,
  101. compressed=options.compressed,
  102. appvm=appvm)
  103. except QubesException as e:
  104. print >> sys.stderr, "ERROR: %s" % str(e)
  105. exit(1)
  106. if len(vmlist) > 0:
  107. for vm in restore_info.keys():
  108. if vm.startswith('$'):
  109. continue
  110. if not vm in vmlist:
  111. restore_info.pop(vm)
  112. backup_restore_print_summary(restore_info)
  113. there_are_conflicting_vms = False
  114. there_are_missing_templates = False
  115. there_are_missing_netvms = False
  116. dom0_username_mismatch = False
  117. for vm_info in restore_info.values():
  118. if 'excluded' in vm_info and vm_info['excluded']:
  119. continue
  120. if 'missing-template' in vm_info.keys():
  121. there_are_missing_templates = True
  122. if 'missing-netvm' in vm_info.keys():
  123. there_are_missing_netvms = True
  124. if 'already-exists' in vm_info.keys():
  125. there_are_conflicting_vms = True
  126. if 'username-mismatch' in vm_info.keys():
  127. dom0_username_mismatch = True
  128. if os.geteuid() == 0:
  129. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  130. if options.force_root:
  131. print >> sys.stderr, "Continuing as commanded. You have been warned."
  132. else:
  133. print >> sys.stderr, "Retry as unprivileged user."
  134. print >> sys.stderr, "... or use --force-root to continue anyway."
  135. exit(1)
  136. if there_are_conflicting_vms:
  137. print >> sys.stderr, "*** There VMs with conflicting names on the host! ***"
  138. if options.skip_conflicting:
  139. print >> sys.stderr, "Those VMs will not be restored, the host VMs will not be overwritten!"
  140. else:
  141. print >> sys.stderr, "Remove VMs with conflicting names from the host before proceeding."
  142. print >> sys.stderr, "... or use --skip-conflicting to restore only those VMs that do not exist on the host."
  143. exit (1)
  144. print "The above VMs will be copied and added to your system."
  145. print "Exisiting VMs will not be removed."
  146. if there_are_missing_templates:
  147. print >> sys.stderr, "*** One or more template VM is missing on the host! ***"
  148. if not (options.skip_broken or options.ignore_missing):
  149. print >> sys.stderr, "Install it first, before proceeding with backup restore."
  150. print >> sys.stderr, "Or pass: --skip-broken or --ignore-missing switch."
  151. exit (1)
  152. elif options.skip_broken:
  153. print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
  154. elif options.ignore_missing:
  155. print >> sys.stderr, "... VMs that depend on it will be restored anyway (--ignore-missing used)"
  156. else:
  157. print >> sys.stderr, "INTERNAL ERROR?!"
  158. exit (1)
  159. if there_are_missing_netvms:
  160. print >> sys.stderr, "*** One or more network VM is missing on the host! ***"
  161. if not (options.skip_broken or options.ignore_missing):
  162. print >> sys.stderr, "Install it first, before proceeding with backup restore."
  163. print >> sys.stderr, "Or pass: --skip_broken or --ignore_missing switch."
  164. exit (1)
  165. elif options.skip_broken:
  166. print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
  167. elif options.ignore_missing:
  168. print >> sys.stderr, "... VMs that depend on it be restored anyway (--ignore-missing used)"
  169. else:
  170. print >> sys.stderr, "INTERNAL ERROR?!"
  171. exit (1)
  172. if 'dom0' in restore_info.keys() and options.dom0_home:
  173. if dom0_username_mismatch:
  174. print >> sys.stderr, "*** Dom0 username mismatch! This can break some settings ***"
  175. if not options.ignore_username_mismatch:
  176. print >> sys.stderr, "Skip dom0 home restore (--skip-dom0-home)"
  177. print >> sys.stderr, "Or pass: --ignore-username-mismatch to continue anyway"
  178. exit(1)
  179. else:
  180. print >> sys.stderr, "Continuing as directed"
  181. print >> sys.stderr, "While restoring user homedir, existing files/dirs will be backed up in 'home-pre-restore-<current-time>' dir"
  182. prompt = raw_input ("Do you want to proceed? [y/N] ")
  183. if not (prompt == "y" or prompt == "Y"):
  184. exit (0)
  185. backup_restore_do(restore_info,
  186. host_collection=host_collection)
  187. host_collection.unlock_db()
  188. print "-> Done."
  189. main()