backup: move existing directories out of the way during restore

In most cases it would be some leftover after failed restore, or even
the reason why the user is restoring a VM in the first place. Move it to
nearby directory, but do not remove - backup tool should _never_ remove
any data.

When the pre-existing directory would not be moved, restore utility
(`shutil.move`) would place the data inside of that directory, with
additional directory level (for example `/var/lib/qubes/appvms/work/work`),
which would be wrong and would later fail on `vm.verify_files`. And more
importantly - such VM would not work.

Fixes QubesOS/qubes-issues#1386
This commit is contained in:
Marek Marczykowski-Górecki 2015-11-07 03:05:41 +01:00
parent 0695e7ba78
commit 8275e828af
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -2104,6 +2104,20 @@ def backup_restore_do(restore_info,
error_callback("Skipping...") error_callback("Skipping...")
continue continue
if os.path.exists(vm.dir_path):
move_to_path = tempfile.mkdtemp('', os.path.basename(
vm.dir_path), os.path.dirname(vm.dir_path))
try:
os.rename(vm.dir_path, move_to_path)
error_callback("*** Directory {} already exists! It has "
"been moved to {}".format(vm.dir_path,
move_to_path))
except OSError:
error_callback("*** Directory {} already exists and "
"cannot be moved!".format(vm.dir_path))
error_callback("Skipping...")
continue
template = None template = None
if vm.template is not None: if vm.template is not None:
template_name = restore_info[vm.name]['template'] template_name = restore_info[vm.name]['template']