qvm-backup-restore 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.qubesutils import backup_restore_prepare
  25. from qubes.qubesutils import backup_restore_print_summary
  26. from qubes.qubesutils import backup_restore_do
  27. from optparse import OptionParser
  28. import os
  29. import sys
  30. def main():
  31. usage = "usage: %prog [options] <backup-dir>"
  32. parser = OptionParser (usage)
  33. parser.add_option ("--skip-broken", action="store_true", dest="skip_broken", default=False,
  34. help="Do not restore VMs that have missing templates or netvms")
  35. parser.add_option ("--ignore-missing", action="store_true", dest="ignore_missing", default=False,
  36. help="Ignore missing templates or netvms, restore VMs anyway")
  37. parser.add_option ("--skip-conflicting", action="store_true", dest="skip_conflicting", default=False,
  38. help="Do not restore VMs that are already present on the host")
  39. parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
  40. help="Force to run, even with root privileges")
  41. parser.add_option ("--replace-template", action="append", dest="replace_template", default=[],
  42. help="Restore VMs using another template, syntax: old-template-name:new-template-name (might be repeated)")
  43. parser.add_option ("-x", "--exclude", action="append", dest="exclude", default=[],
  44. help="Skip restore of specified VM (might be repeated)")
  45. parser.add_option ("--skip-dom0-home", action="store_false", dest="dom0_home", default=True,
  46. help="Do not restore dom0 user home dir")
  47. parser.add_option ("--ignore-username-mismatch", action="store_true", dest="ignore_username_mismatch", default=False,
  48. help="Ignore dom0 username mismatch while restoring homedir")
  49. (options, args) = parser.parse_args ()
  50. if (len (args) != 1):
  51. print >> sys.stderr, "You must specify the backup directory (e.g. /mnt/backup/qubes-2010-12-01-235959)"
  52. exit (0)
  53. backup_dir = args[0]
  54. if not os.path.exists (backup_dir):
  55. print >> sys.stderr, "The backup directory doesn't exist!"
  56. exit(1)
  57. host_collection = QubesVmCollection()
  58. host_collection.lock_db_for_writing()
  59. host_collection.load()
  60. restore_options = {}
  61. if options.ignore_missing:
  62. restore_options['use-default-template'] = True
  63. restore_options['use-default-netvm'] = True
  64. if options.replace_template:
  65. restore_options['replace-template'] = options.replace_template
  66. if not options.dom0_home:
  67. restore_options['dom0-home'] = False
  68. if options.ignore_username_mismatch:
  69. restore_options['ignore-username-mismatch'] = True
  70. if options.exclude:
  71. restore_options['exclude'] = options.exclude
  72. restore_info = None
  73. try:
  74. restore_info = backup_restore_prepare(backup_dir, options=restore_options, host_collection=host_collection)
  75. except QubesException as e:
  76. print >> sys.stderr, "ERROR: %s" % str(e)
  77. exit(1)
  78. backup_restore_print_summary(restore_info)
  79. there_are_conflicting_vms = False
  80. there_are_missing_templates = False
  81. there_are_missing_netvms = False
  82. dom0_username_mismatch = False
  83. for vm_info in restore_info.values():
  84. if 'excluded' in vm_info and vm_info['excluded']:
  85. continue
  86. if 'missing-template' in vm_info.keys():
  87. there_are_missing_templates = True
  88. if 'missing-netvm' in vm_info.keys():
  89. there_are_missing_netvms = True
  90. if 'already-exists' in vm_info.keys():
  91. there_are_conflicting_vms = True
  92. if 'username-mismatch' in vm_info.keys():
  93. dom0_username_mismatch = True
  94. print
  95. if os.geteuid() == 0:
  96. print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
  97. if options.force_root:
  98. print >> sys.stderr, "Continuing as commanded. You have been warned."
  99. else:
  100. print >> sys.stderr, "Retry as unprivileged user."
  101. print >> sys.stderr, "... or use --force-root to continue anyway."
  102. exit(1)
  103. if there_are_conflicting_vms:
  104. print >> sys.stderr, "*** There VMs with conflicting names on the host! ***"
  105. if options.skip_conflicting:
  106. print >> sys.stderr, "Those VMs will not be restored, the host VMs will not be overwritten!"
  107. else:
  108. print >> sys.stderr, "Remove VMs with conflicting names from the host before proceeding."
  109. print >> sys.stderr, "... or use --skip-conflicting to restore only those VMs that do not exist on the host."
  110. exit (1)
  111. print "The above VMs will be copied and added to your system."
  112. print "Exisiting VMs will not be removed."
  113. if there_are_missing_templates:
  114. print >> sys.stderr, "*** One or more template VM is missing on the host! ***"
  115. if not (options.skip_broken or options.ignore_missing):
  116. print >> sys.stderr, "Install it first, before proceeding with backup restore."
  117. print >> sys.stderr, "Or pass: --skip-broken or --ignore-missing switch."
  118. exit (1)
  119. elif options.skip_broken:
  120. print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
  121. elif options.ignore_missing:
  122. print >> sys.stderr, "... VMs that depend on it will be restored anyway (--ignore-missing used)"
  123. else:
  124. print >> sys.stderr, "INTERNAL ERROR?!"
  125. exit (1)
  126. if there_are_missing_netvms:
  127. print >> sys.stderr, "*** One or more network VM is missing on the host! ***"
  128. if not (options.skip_broken or options.ignore_missing):
  129. print >> sys.stderr, "Install it first, before proceeding with backup restore."
  130. print >> sys.stderr, "Or pass: --skip_broken or --ignore_missing switch."
  131. exit (1)
  132. elif options.skip_broken:
  133. print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
  134. elif options.ignore_missing:
  135. print >> sys.stderr, "... VMs that depend on it be restored anyway (--ignore-missing used)"
  136. else:
  137. print >> sys.stderr, "INTERNAL ERROR?!"
  138. exit (1)
  139. if 'dom0' in restore_info.keys() and options.dom0_home:
  140. if dom0_username_mismatch:
  141. print >> sys.stderr, "*** Dom0 username mismatch! This can break some settings ***"
  142. if not options.ignore_username_mismatch:
  143. print >> sys.stderr, "Skip dom0 home restore (--skip-dom0-home)"
  144. print >> sys.stderr, "Or pass: --ignore-username-mismatch to continue anyway"
  145. exit(1)
  146. else:
  147. print >> sys.stderr, "Continuing as directed"
  148. print >> sys.stderr, "While restoring user homedir, existing files/dirs will be backed up in 'home-pre-restore-<current-time>' dir"
  149. prompt = raw_input ("Do you want to proceed? [y/N] ")
  150. if not (prompt == "y" or prompt == "Y"):
  151. exit (0)
  152. backup_restore_do(backup_dir, restore_info, host_collection=host_collection)
  153. host_collection.unlock_db()
  154. print "-> Done."
  155. main()