backups: move backup code to separate file

Also some major cleanups: Reduce some more code duplication
(verify_hmac, simplify backup_restore_prepare). Rename
backup_dir/backup_tmpdir variables to better match its purpose. Rename
backup_do_copy back to backup_do.  Require QubesVm object (instead of VM
name) as appvm param.
This commit is contained in:
Marek Marczykowski-Górecki 2013-11-25 05:41:13 +01:00
parent 657beaf655
commit c781a522d8
5 changed files with 1322 additions and 1463 deletions

1279
core/backup.py Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,8 @@
from qubes.qubes import QubesVmCollection from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesException from qubes.qubes import QubesException
from qubes.qubesutils import backup_prepare, backup_do_copy, size_to_human from qubes.backup import backup_prepare, backup_do
from qubes.qubesutils import size_to_human
from optparse import OptionParser from optparse import OptionParser
import os import os
import sys import sys
@ -80,6 +81,8 @@ def main():
files_to_backup, 0) files_to_backup, 0)
if not options.appvm: if not options.appvm:
appvm = None
stat = os.statvfs(base_backup_dir) stat = os.statvfs(base_backup_dir)
backup_fs_free_sz = stat.f_bsize * stat.f_bavail backup_fs_free_sz = stat.f_bsize * stat.f_bavail
print print
@ -88,6 +91,11 @@ def main():
exit(1) exit(1)
print "-> Available space: {0}".format(size_to_human(backup_fs_free_sz)) print "-> Available space: {0}".format(size_to_human(backup_fs_free_sz))
else:
appvm = qvm_collection.get_vm_by_name(options.appvm)
if appvm is None:
print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
exit(1)
prompt = raw_input ("Do you want to proceed? [y/N] ") prompt = raw_input ("Do you want to proceed? [y/N] ")
if not (prompt == "y" or prompt == "Y"): if not (prompt == "y" or prompt == "Y"):
@ -96,10 +104,10 @@ def main():
passphrase = getpass.getpass("Please enter the pass phrase that will be used to encrypt/verify the backup: ") passphrase = getpass.getpass("Please enter the pass phrase that will be used to encrypt/verify the backup: ")
try: try:
backup_do_copy(base_backup_dir, files_to_backup, passphrase, backup_do(base_backup_dir, files_to_backup, passphrase,
progress_callback=print_progress, progress_callback=print_progress,
encrypt=options.encrypt, encrypt=options.encrypt,
appvm=options.appvm) appvm=appvm)
except QubesException as e: except QubesException as e:
print >>sys.stderr, "ERROR: %s" % str(e) print >>sys.stderr, "ERROR: %s" % str(e)
exit(1) exit(1)

View File

@ -22,10 +22,10 @@
from qubes.qubes import QubesVmCollection from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesException from qubes.qubes import QubesException
from qubes.qubesutils import backup_restore_header from qubes.backup import backup_restore_header
from qubes.qubesutils import backup_restore_prepare from qubes.backup import backup_restore_prepare
from qubes.qubesutils import backup_restore_print_summary from qubes.backup import backup_restore_print_summary
from qubes.qubesutils import backup_restore_do from qubes.backup import backup_restore_do
from optparse import OptionParser from optparse import OptionParser
import os import os
@ -95,15 +95,28 @@ def main():
if options.exclude: if options.exclude:
restore_options['exclude'] = options.exclude restore_options['exclude'] = options.exclude
appvm = None
if options.appvm is not None:
appvm = qvm_collection.get_vm_by_name(options.appvm)
if appvm is None:
print >>sys.stderr, "ERROR: VM {0} does not exist".format(options.appvm)
exit(1)
passphrase = getpass.getpass("Please enter the pass phrase that will be used to decrypt/verify the backup: ") passphrase = getpass.getpass("Please enter the pass phrase that will be used to decrypt/verify the backup: ")
print >> sys.stderr, "Checking backup content..." print >> sys.stderr, "Checking backup content..."
restore_tmpdir,qubes_xml = backup_restore_header(backup_dir, passphrase, options.decrypt, appvm=options.appvm) restore_tmpdir,qubes_xml = backup_restore_header(backup_dir, passphrase, encrypted=options.decrypt, appvm=options.appvm)
restore_info = None restore_info = None
try: try:
restore_info = backup_restore_prepare(backup_dir,os.path.join(restore_tmpdir, qubes_xml), passphrase, options=restore_options, host_collection=host_collection, encrypt=options.decrypt, appvm=options.appvm) restore_info = backup_restore_prepare(
backup_dir,
os.path.join(restore_tmpdir, qubes_xml),
passphrase,
options=restore_options,
host_collection=host_collection,
encrypt=options.decrypt,
appvm=appvm)
except QubesException as e: except QubesException as e:
print >> sys.stderr, "ERROR: %s" % str(e) print >> sys.stderr, "ERROR: %s" % str(e)
exit(1) exit(1)
@ -147,7 +160,7 @@ def main():
print "The above VMs will be copied and added to your system." print "The above VMs will be copied and added to your system."
print "Exisiting VMs will not be removed." print "Exisiting VMs will not be removed."
if there_are_missing_templates: if there_are_missing_templates:
print >> sys.stderr, "*** One or more template VM is missing on the host! ***" print >> sys.stderr, "*** One or more template VM is missing on the host! ***"
if not (options.skip_broken or options.ignore_missing): if not (options.skip_broken or options.ignore_missing):
@ -192,7 +205,13 @@ def main():
exit (0) exit (0)
backup_restore_do(backup_dir,restore_tmpdir, passphrase, restore_info, host_collection=host_collection, encrypted=options.decrypt, appvm=options.appvm) backup_restore_do(backup_dir,
restore_tmpdir,
passphrase,
restore_info,
host_collection=host_collection,
encrypted=options.decrypt,
appvm=appvm)
host_collection.unlock_db() host_collection.unlock_db()

View File

@ -109,6 +109,8 @@ cp core/qubesutils.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/qubesutils.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/qubesutils.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/guihelpers.py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/guihelpers.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/guihelpers.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/guihelpers.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/backup.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/backup.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/__init__.py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/__init__.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/__init__.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/__init__.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp qmemman/qmemman*py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp qmemman/qmemman*py $RPM_BUILD_ROOT%{python_sitearch}/qubes
@ -272,6 +274,9 @@ fi
%{python_sitearch}/qubes/guihelpers.py %{python_sitearch}/qubes/guihelpers.py
%{python_sitearch}/qubes/guihelpers.pyc %{python_sitearch}/qubes/guihelpers.pyc
%{python_sitearch}/qubes/guihelpers.pyo %{python_sitearch}/qubes/guihelpers.pyo
%{python_sitearch}/qubes/backup.py
%{python_sitearch}/qubes/backup.pyc
%{python_sitearch}/qubes/backup.pyo
%{python_sitearch}/qubes/__init__.py %{python_sitearch}/qubes/__init__.py
%{python_sitearch}/qubes/__init__.pyc %{python_sitearch}/qubes/__init__.pyc
%{python_sitearch}/qubes/__init__.pyo %{python_sitearch}/qubes/__init__.pyo