From 8275e828afb0ea857d7c16426e6c5f3b589a0819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 7 Nov 2015 03:05:41 +0100 Subject: [PATCH] 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 --- core/backup.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/backup.py b/core/backup.py index 2493d4a8..08190bb8 100644 --- a/core/backup.py +++ b/core/backup.py @@ -2104,6 +2104,20 @@ def backup_restore_do(restore_info, error_callback("Skipping...") 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 if vm.template is not None: template_name = restore_info[vm.name]['template']