2013-01-20 21:44:13 +01:00
|
|
|
#!/usr/bin/python2
|
2014-05-18 21:01:21 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
2010-06-26 15:00:19 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
2014-09-17 23:09:31 +02:00
|
|
|
from multiprocessing import Event
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from qubes.qubes import QubesException
|
2013-11-25 05:41:13 +01:00
|
|
|
from qubes.backup import backup_restore_header
|
|
|
|
from qubes.backup import backup_restore_prepare
|
|
|
|
from qubes.backup import backup_restore_print_summary
|
|
|
|
from qubes.backup import backup_restore_do
|
2014-09-17 23:09:31 +02:00
|
|
|
import qubes.backup
|
2014-02-05 06:53:07 +01:00
|
|
|
import sys
|
2010-06-26 15:00:19 +02:00
|
|
|
from optparse import OptionParser
|
2016-03-08 12:50:06 +01:00
|
|
|
from locale import getpreferredencoding
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2013-11-24 03:19:11 +01:00
|
|
|
import getpass
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
def main():
|
2014-02-05 02:49:26 +01:00
|
|
|
usage = "usage: %prog [options] <backup-dir> [vms-to-be-restored ...]"
|
2010-06-26 15:00:19 +02:00
|
|
|
parser = OptionParser (usage)
|
|
|
|
|
2014-09-17 13:54:33 +02:00
|
|
|
parser.add_option ("--verify-only", action="store_true",
|
|
|
|
dest="verify_only", default=False,
|
|
|
|
help="Do not restore the data, only verify backup "
|
|
|
|
"integrify.")
|
|
|
|
|
2010-06-26 15:00:19 +02:00
|
|
|
parser.add_option ("--skip-broken", action="store_true", dest="skip_broken", default=False,
|
|
|
|
help="Do not restore VMs that have missing templates or netvms")
|
|
|
|
|
|
|
|
parser.add_option ("--ignore-missing", action="store_true", dest="ignore_missing", default=False,
|
|
|
|
help="Ignore missing templates or netvms, restore VMs anyway")
|
2010-12-18 07:25:47 +01:00
|
|
|
|
|
|
|
parser.add_option ("--skip-conflicting", action="store_true", dest="skip_conflicting", default=False,
|
|
|
|
help="Do not restore VMs that are already present on the host")
|
|
|
|
|
2015-11-27 03:44:15 +01:00
|
|
|
parser.add_option ("--rename-conflicting", action="store_true",
|
|
|
|
dest="rename_conflicting", default=False,
|
|
|
|
help="Restore VMs that are already present on the host under different name")
|
|
|
|
|
2011-04-01 02:11:40 +02:00
|
|
|
parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
|
|
|
|
help="Force to run, even with root privileges")
|
|
|
|
|
2011-03-25 02:34:04 +01:00
|
|
|
parser.add_option ("--replace-template", action="append", dest="replace_template", default=[],
|
2014-01-23 04:50:14 +01:00
|
|
|
help="Restore VMs using another template, syntax: "
|
|
|
|
"old-template-name:new-template-name (may be "
|
|
|
|
"repeated)")
|
2011-03-25 02:34:04 +01:00
|
|
|
|
2011-10-11 01:52:11 +02:00
|
|
|
parser.add_option ("-x", "--exclude", action="append", dest="exclude", default=[],
|
2014-01-23 04:50:14 +01:00
|
|
|
help="Skip restore of specified VM (may be repeated)")
|
2011-10-11 01:52:11 +02:00
|
|
|
|
2011-10-11 01:42:05 +02:00
|
|
|
parser.add_option ("--skip-dom0-home", action="store_false", dest="dom0_home", default=True,
|
|
|
|
help="Do not restore dom0 user home dir")
|
|
|
|
|
|
|
|
parser.add_option ("--ignore-username-mismatch", action="store_true", dest="ignore_username_mismatch", default=False,
|
|
|
|
help="Ignore dom0 username mismatch while restoring homedir")
|
|
|
|
|
2013-08-19 16:48:29 +02:00
|
|
|
parser.add_option ("-d", "--dest-vm", action="store", dest="appvm",
|
|
|
|
help="The AppVM to send backups to")
|
|
|
|
|
|
|
|
parser.add_option ("-e", "--encrypted", action="store_true", dest="decrypt", default=False,
|
|
|
|
help="The backup is encrypted")
|
|
|
|
|
2016-03-08 12:50:06 +01:00
|
|
|
parser.add_option ("-p", "--passphrase-file", action="store",
|
|
|
|
dest="pass_file", default=None,
|
|
|
|
help="File containing the pass phrase to use, or '-' to read it from stdin")
|
|
|
|
|
2013-12-02 14:05:41 +01:00
|
|
|
parser.add_option ("-z", "--compressed", action="store_true", dest="compressed", default=False,
|
|
|
|
help="The backup is compressed")
|
|
|
|
|
2014-09-17 23:10:06 +02:00
|
|
|
parser.add_option ("--debug", action="store_true", dest="debug",
|
|
|
|
default=False, help="Enable (a lot of) debug output")
|
|
|
|
|
2010-06-26 15:00:19 +02:00
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
2014-01-19 04:52:13 +01:00
|
|
|
if (len (args) < 1):
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "You must specify the backup directory (e.g. /mnt/backup/qubes-2010-12-01-235959)"
|
2010-06-26 15:00:19 +02:00
|
|
|
exit (0)
|
|
|
|
|
|
|
|
backup_dir = args[0]
|
2014-01-19 04:52:13 +01:00
|
|
|
vmlist = args[1:]
|
2010-06-26 15:00:19 +02:00
|
|
|
|
2013-08-19 16:48:29 +02:00
|
|
|
#if not os.path.exists (backup_dir):
|
|
|
|
# print >> sys.stderr, "The backup directory doesn't exist!"
|
|
|
|
# exit(1)
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
host_collection = QubesVmCollection()
|
|
|
|
host_collection.lock_db_for_writing()
|
|
|
|
host_collection.load()
|
|
|
|
|
2012-02-10 10:56:03 +01:00
|
|
|
restore_options = {}
|
|
|
|
if options.ignore_missing:
|
|
|
|
restore_options['use-default-template'] = True
|
|
|
|
restore_options['use-default-netvm'] = True
|
|
|
|
if options.replace_template:
|
|
|
|
restore_options['replace-template'] = options.replace_template
|
|
|
|
if not options.dom0_home:
|
|
|
|
restore_options['dom0-home'] = False
|
|
|
|
if options.ignore_username_mismatch:
|
|
|
|
restore_options['ignore-username-mismatch'] = True
|
|
|
|
if options.exclude:
|
|
|
|
restore_options['exclude'] = options.exclude
|
2014-09-17 13:54:33 +02:00
|
|
|
if options.verify_only:
|
|
|
|
restore_options['verify-only'] = True
|
2014-09-17 23:10:06 +02:00
|
|
|
if options.debug:
|
|
|
|
qubes.backup.BACKUP_DEBUG = True
|
2012-02-10 10:56:03 +01:00
|
|
|
|
2013-11-25 05:41:13 +01:00
|
|
|
appvm = None
|
|
|
|
if options.appvm is not None:
|
2013-11-25 06:33:31 +01:00
|
|
|
appvm = host_collection.get_vm_by_name(options.appvm)
|
2013-11-25 05:41:13 +01:00
|
|
|
if appvm is None:
|
|
|
|
print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
|
|
|
|
exit(1)
|
2013-08-19 16:48:29 +02:00
|
|
|
|
2016-03-08 12:50:06 +01:00
|
|
|
if options.pass_file is not None:
|
|
|
|
f = open(options.pass_file) if options.pass_file != "-" else sys.stdin
|
|
|
|
passphrase = f.readline().rstrip()
|
|
|
|
if f is not sys.stdin:
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
passphrase = getpass.getpass("Please enter the pass phrase to decrypt/verify the backup: ")
|
|
|
|
|
|
|
|
encoding = sys.stdin.encoding or getpreferredencoding()
|
|
|
|
passphrase = passphrase.decode(encoding)
|
2013-09-25 09:51:17 +02:00
|
|
|
|
2013-08-19 16:48:29 +02:00
|
|
|
print >> sys.stderr, "Checking backup content..."
|
|
|
|
|
2014-09-17 23:09:31 +02:00
|
|
|
error_detected = Event()
|
|
|
|
def error_callback(message):
|
|
|
|
error_detected.set()
|
|
|
|
print >> sys.stderr, message
|
|
|
|
|
2012-02-10 10:56:03 +01:00
|
|
|
restore_info = None
|
|
|
|
try:
|
2013-11-25 05:41:13 +01:00
|
|
|
restore_info = backup_restore_prepare(
|
|
|
|
backup_dir,
|
2014-01-13 04:45:02 +01:00
|
|
|
passphrase=passphrase,
|
2013-11-25 05:41:13 +01:00
|
|
|
options=restore_options,
|
|
|
|
host_collection=host_collection,
|
2014-01-13 04:45:02 +01:00
|
|
|
encrypted=options.decrypt,
|
|
|
|
compressed=options.compressed,
|
2014-09-17 23:09:31 +02:00
|
|
|
appvm=appvm,
|
|
|
|
error_callback=error_callback)
|
2012-02-10 10:56:03 +01:00
|
|
|
except QubesException as e:
|
|
|
|
print >> sys.stderr, "ERROR: %s" % str(e)
|
|
|
|
exit(1)
|
2010-06-26 15:00:19 +02:00
|
|
|
|
2014-01-19 04:52:13 +01:00
|
|
|
if len(vmlist) > 0:
|
|
|
|
for vm in restore_info.keys():
|
|
|
|
if vm.startswith('$'):
|
|
|
|
continue
|
|
|
|
if not vm in vmlist:
|
|
|
|
restore_info.pop(vm)
|
|
|
|
|
2012-02-10 10:56:03 +01:00
|
|
|
backup_restore_print_summary(restore_info)
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
there_are_conflicting_vms = False
|
|
|
|
there_are_missing_templates = False
|
|
|
|
there_are_missing_netvms = False
|
2011-10-11 01:42:05 +02:00
|
|
|
dom0_username_mismatch = False
|
2010-06-26 15:00:19 +02:00
|
|
|
|
2012-02-10 10:56:03 +01:00
|
|
|
for vm_info in restore_info.values():
|
|
|
|
if 'excluded' in vm_info and vm_info['excluded']:
|
|
|
|
continue
|
|
|
|
if 'missing-template' in vm_info.keys():
|
|
|
|
there_are_missing_templates = True
|
|
|
|
if 'missing-netvm' in vm_info.keys():
|
|
|
|
there_are_missing_netvms = True
|
|
|
|
if 'already-exists' in vm_info.keys():
|
|
|
|
there_are_conflicting_vms = True
|
|
|
|
if 'username-mismatch' in vm_info.keys():
|
2011-10-11 01:42:05 +02:00
|
|
|
dom0_username_mismatch = True
|
|
|
|
|
2013-07-30 10:42:08 +02:00
|
|
|
print
|
|
|
|
|
|
|
|
if hasattr(os, "geteuid") and os.geteuid() == 0:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
|
2011-04-01 02:11:40 +02:00
|
|
|
if options.force_root:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Continuing as commanded. You have been warned."
|
2011-04-01 02:11:40 +02:00
|
|
|
else:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Retry as unprivileged user."
|
|
|
|
print >> sys.stderr, "... or use --force-root to continue anyway."
|
2011-04-01 02:11:40 +02:00
|
|
|
exit(1)
|
|
|
|
|
2010-06-26 15:00:19 +02:00
|
|
|
if there_are_conflicting_vms:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** There VMs with conflicting names on the host! ***"
|
2011-04-01 02:10:50 +02:00
|
|
|
if options.skip_conflicting:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Those VMs will not be restored, the host VMs will not be overwritten!"
|
2011-04-01 02:10:50 +02:00
|
|
|
else:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Remove VMs with conflicting names from the host before proceeding."
|
|
|
|
print >> sys.stderr, "... or use --skip-conflicting to restore only those VMs that do not exist on the host."
|
2015-11-27 03:44:15 +01:00
|
|
|
print >> sys.stderr, "... or use --rename-conflicting to " \
|
|
|
|
"restore those VMs under modified " \
|
|
|
|
"name (with number at the end)"
|
2011-04-01 02:10:50 +02:00
|
|
|
exit (1)
|
2010-06-26 15:00:19 +02:00
|
|
|
|
|
|
|
print "The above VMs will be copied and added to your system."
|
|
|
|
print "Exisiting VMs will not be removed."
|
2013-11-25 05:41:13 +01:00
|
|
|
|
2010-06-26 15:00:19 +02:00
|
|
|
if there_are_missing_templates:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** One or more template VM is missing on the host! ***"
|
2010-06-26 15:00:19 +02:00
|
|
|
if not (options.skip_broken or options.ignore_missing):
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Install it first, before proceeding with backup restore."
|
|
|
|
print >> sys.stderr, "Or pass: --skip-broken or --ignore-missing switch."
|
2010-06-26 15:00:19 +02:00
|
|
|
exit (1)
|
|
|
|
elif options.skip_broken:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
|
2010-06-26 15:00:19 +02:00
|
|
|
elif options.ignore_missing:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "... VMs that depend on it will be restored anyway (--ignore-missing used)"
|
2010-06-26 15:00:19 +02:00
|
|
|
else:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "INTERNAL ERROR?!"
|
2010-06-26 15:00:19 +02:00
|
|
|
exit (1)
|
|
|
|
|
|
|
|
if there_are_missing_netvms:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "*** One or more network VM is missing on the host! ***"
|
2010-06-26 15:00:19 +02:00
|
|
|
if not (options.skip_broken or options.ignore_missing):
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "Install it first, before proceeding with backup restore."
|
|
|
|
print >> sys.stderr, "Or pass: --skip_broken or --ignore_missing switch."
|
2010-06-26 15:00:19 +02:00
|
|
|
exit (1)
|
|
|
|
elif options.skip_broken:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "... VMs that depend on it will not be restored (--skip-broken used)"
|
2010-06-26 15:00:19 +02:00
|
|
|
elif options.ignore_missing:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "... VMs that depend on it be restored anyway (--ignore-missing used)"
|
2010-06-26 15:00:19 +02:00
|
|
|
else:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "INTERNAL ERROR?!"
|
2010-06-26 15:00:19 +02:00
|
|
|
exit (1)
|
|
|
|
|
2012-02-10 10:56:03 +01:00
|
|
|
if 'dom0' in restore_info.keys() and options.dom0_home:
|
2011-10-11 01:42:05 +02:00
|
|
|
if dom0_username_mismatch:
|
|
|
|
print >> sys.stderr, "*** Dom0 username mismatch! This can break some settings ***"
|
|
|
|
if not options.ignore_username_mismatch:
|
|
|
|
print >> sys.stderr, "Skip dom0 home restore (--skip-dom0-home)"
|
|
|
|
print >> sys.stderr, "Or pass: --ignore-username-mismatch to continue anyway"
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
print >> sys.stderr, "Continuing as directed"
|
2012-02-10 10:56:03 +01:00
|
|
|
print >> sys.stderr, "While restoring user homedir, existing files/dirs will be backed up in 'home-pre-restore-<current-time>' dir"
|
2011-10-11 01:42:05 +02:00
|
|
|
|
2016-03-08 12:50:06 +01:00
|
|
|
if options.pass_file is None:
|
|
|
|
if raw_input("Do you want to proceed? [y/N] ").upper() != "Y":
|
|
|
|
exit(0)
|
2013-08-19 16:48:29 +02:00
|
|
|
|
2014-09-17 14:31:20 +02:00
|
|
|
try:
|
|
|
|
backup_restore_do(restore_info,
|
2014-09-17 23:09:31 +02:00
|
|
|
host_collection=host_collection,
|
|
|
|
error_callback=error_callback)
|
2014-09-17 14:31:20 +02:00
|
|
|
except QubesException as e:
|
|
|
|
print >> sys.stderr, "ERROR: %s" % str(e)
|
2011-09-12 15:25:31 +02:00
|
|
|
|
2010-06-26 15:00:19 +02:00
|
|
|
host_collection.unlock_db()
|
2011-10-11 01:42:05 +02:00
|
|
|
|
2014-09-17 23:09:31 +02:00
|
|
|
if error_detected.is_set():
|
|
|
|
print "-> Completed with errors!"
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
print "-> Done."
|
2010-06-26 15:00:19 +02:00
|
|
|
main()
|