Skip to content
Snippets Groups Projects
Verified Commit fa17698b authored by Davide Gardenal's avatar Davide Gardenal
Browse files

upgrade_oniro: improve backup and restore


Now restore is able to find the latest backup without user
intervention.
Backup will not crash anymore if used without build_directory
set to a correct directory, default is now None.
Rename backup_archive_path to backup_archive_arg to better
represent its meaning.
Add TODOs.

Signed-off-by: Davide Gardenal's avatarDavide Gardenal <davide.gardenal@huawei.com>
parent 483139ee
No related branches found
No related tags found
No related merge requests found
...@@ -55,18 +55,18 @@ command = "" ...@@ -55,18 +55,18 @@ command = ""
# Upgrade options # Upgrade options
target_version = "" target_version = ""
build_directory = pathlib.Path() build_directory = None
machine = "" machine = ""
flavour = "" flavour = ""
image = "" image = ""
conf_directory = pathlib.Path() conf_directory = None
dry_run = False dry_run = False
store_backup = False store_backup = False
# Backup options # Backup options
# Restore options # Restore options
backup_archive_path = "" backup_archive_arg = ""
# Global vars # Global vars
backup_archive_name = "" backup_archive_name = ""
...@@ -104,7 +104,7 @@ def init_script_options(): ...@@ -104,7 +104,7 @@ def init_script_options():
# Restore subcommand # Restore subcommand
restore_parser = subparsers.add_parser("restore", help="Restore a local backup.") restore_parser = subparsers.add_parser("restore", help="Restore a local backup.")
restore_parser.add_argument("backup_archive_path", type=str, restore_parser.add_argument("backup_archive_arg", type=str,
help="Specify the path of a local backup archive to restore. Use 'latest' to use the latest backup found.") help="Specify the path of a local backup archive to restore. Use 'latest' to use the latest backup found.")
parsed_args = vars(parser.parse_args()) parsed_args = vars(parser.parse_args())
...@@ -126,8 +126,8 @@ def init_script_options(): ...@@ -126,8 +126,8 @@ def init_script_options():
# TODO # TODO
print("WIP") print("WIP")
elif command == "restore": elif command == "restore":
global backup_archive_path global backup_archive_arg
backup_archive_path = parsed_args["backup_archive_path"] backup_archive_arg = parsed_args["backup_archive_arg"]
def upgrade_tool(): def upgrade_tool():
# TODO # TODO
...@@ -144,12 +144,14 @@ def backup_oniro(): ...@@ -144,12 +144,14 @@ def backup_oniro():
# - If build_directory is not empty, backup its content excluding tmp* and tmp-newlib* directories # - If build_directory is not empty, backup its content excluding tmp* and tmp-newlib* directories
# TODO add function description # TODO add function description
# TODO add more verbose log # TODO add more verbose log
# TODO add an option to backup build directories / DV layers ?
from upgrade_oniro_utils import get_manifest_projects from upgrade_oniro_utils import get_manifest_projects
from datetime import datetime from datetime import datetime
backup_folders = [layer["path"] for layer in get_manifest_projects()] # add all manifest layers backup_folders = [layer["path"] for layer in get_manifest_projects()] # add all manifest layers
backup_folders.append(".repo") backup_folders.append(".repo")
if build_directory.exists():
if build_directory and build_directory.exists():
for child in build_directory.iterdir(): for child in build_directory.iterdir():
if child.is_dir() and "tmp" not in child.name and child.name != "downloads": if child.is_dir() and "tmp" not in child.name and child.name != "downloads":
backup_folders.append(f"{build_directory.name}/{child.name}") backup_folders.append(f"{build_directory.name}/{child.name}")
...@@ -169,9 +171,8 @@ def backup_oniro(): ...@@ -169,9 +171,8 @@ def backup_oniro():
# Remove the uncompressed copy of backup_folders # Remove the uncompressed copy of backup_folders
os.remove("backup_folders") os.remove("backup_folders")
# Save the name of the backup for later use in the script print(f"Backup created at {archive_name}")
global backup_archive_name
backup_archive_name = archive_name
return return
def restore_oniro(): def restore_oniro():
...@@ -180,6 +181,15 @@ def restore_oniro(): ...@@ -180,6 +181,15 @@ def restore_oniro():
# TODO add a subcommand to restore oniro from a backup # TODO add a subcommand to restore oniro from a backup
# TODO add an exception handling if the metadata file is not found # TODO add an exception handling if the metadata file is not found
import shutil import shutil
from upgrade_oniro_utils import find_latest_backup
if backup_archive_arg == "latest":
backup_archive_name = find_latest_backup()
if os.path.exists(backup_archive_arg):
# TODO add a check to be sure that the selected path is actually a Oniro backup
backup_archive_name = backup_archive_arg
print(f"Restoring {backup_archive_name}") print(f"Restoring {backup_archive_name}")
with tarfile.open(backup_archive_name) as backup_archive: with tarfile.open(backup_archive_name) as backup_archive:
# Extract the metadata file and delete all the old folders # Extract the metadata file and delete all the old folders
...@@ -203,7 +213,16 @@ def upgrade_oniro(): ...@@ -203,7 +213,16 @@ def upgrade_oniro():
if __name__ == "__main__": if __name__ == "__main__":
init_script_options() init_script_options()
upgrade_tool() upgrade_tool()
perform_pre_upgrade_checks()
backup_oniro() if command == "upgrade":
restore_oniro() perform_pre_upgrade_checks()
upgrade_oniro() backup_oniro()
\ No newline at end of file restore_oniro()
upgrade_oniro()
elif command == "backup":
backup_oniro()
elif command == "restore":
restore_oniro()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment