Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added disk selection and exclusion of deactivated disks #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ def create_argparser():
default=None,
)

dsk = p.add_argument_group("VM's disks arguments")
dsk.add_argument(
"--bootable_only",
help="Backup Bootable Disk Only",
dest="bootable_only",
default=None,
)
dsk.add_argument(
"--with_disks_deactivated",
help="Backup deactivated disks",
dest="with_disks_deactivated",
default=None,
)
dsk.add_argument(
"--disks_id_exclude",
help="an array of exclude disks id",
dest="disks_id_exclude",
default=None,
)

dcg = p.add_argument_group("Data Centre's related options")
dcg.add_argument(
"--export-domain",
Expand Down Expand Up @@ -333,6 +353,32 @@ def main(argv):

vm = vm[0]

# Get the Attachments Disk
disk_attachments = vms_service.vm_service(vm.id).disk_attachments_service().list()
disksBackup = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the python convention
https://peps.python.org/pep-0008/#function-and-variable-names
like:

Suggested change
disksBackup = []
disks_backup = []

BootableOnly = config.get_bootable_only()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here ^^

Suggested change
BootableOnly = config.get_bootable_only()
bootable_only = config.get_bootable_only()

for disk_attachment in disk_attachments:
if BootableOnly:
if disk_attachment.bootable == True:
disksBackup.append(disk_attachment)
break
elif config.get_with_disks_deactivated() or disk_attachment.active:
disksBackup.append(disk_attachment)

if not BootableOnly:
disks_id_exclude=config.get_disks_id_exclude()
if disks_id_exclude:
disksBackup = [disk for disk in disksBackup if disk.id not in disks_id_exclude]

for disk_attachment in disksBackup:
if disk_attachment.bootable == True:
logger.info("Finding bootable disk: %s", disk_attachment.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it will be better to use the new python3 format like:

Suggested change
logger.info("Finding bootable disk: %s", disk_attachment.id)
logger.info(f"Finding bootable disk: {disk_attachment.id}")

IMHO it looks better and more readable.

else:
logger.info("Finding disk: %s", disk_attachment.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same ^^

Suggested change
logger.info("Finding disk: %s", disk_attachment.id)
logger.info(f"Finding disk: {disk_attachment.id}")

# print disk_attachment.id
# break
# print disks_id[0].__dict__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this code is not necessary maybe you can remove it

Suggested change
# print disk_attachment.id
# break
# print disks_id[0].__dict__


# Delete old backup snapshots
VMTools.delete_snapshots(api, vm, config, vm_from_list)

Expand All @@ -349,6 +395,7 @@ def main(argv):
types.Snapshot(
description=config.get_snapshot_description(),
persist_memorystate=config.get_persist_memorystate(),
disk_attachments=disksBackup,
),
)
VMTools.wait_for_snapshot_operation(api, vm, config, "creation")
Expand Down
12 changes: 12 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __init__(self, fd, debug, arguments):
self.__backup_keep_count = config_parser.get(section, "backup_keep_count")
self.__backup_keep_count_by_number = config_parser.get(section, "backup_keep_count_by_number")
self.__dry_run = config_parser.getboolean(section, "dry_run")
self.__bootable_only = config_parser.getboolean(section, "bootable_only", fallback=False)
self.__with_disks_deactivated = config_parser.getboolean(section, "with_disks_deactivated", fallback=False)
self.__disks_id_exclude = json.loads(config_parser.get(section, "disks_id_exclude", fallback="[]"))
self.__debug = debug
self.__vm_name_max_length = config_parser.getint(section, "vm_name_max_length")
self.__use_short_suffix = config_parser.getboolean(section, "use_short_suffix")
Expand Down Expand Up @@ -126,6 +129,15 @@ def get_backup_keep_count_by_number(self):
def get_dry_run(self):
return self.__dry_run

def get_bootable_only(self):
return self.__bootable_only

def get_with_disks_deactivated(self):
return self.__with_disks_deactivated

def get_disks_id_exclude(self):
return self.__disks_id_exclude

def get_debug(self):
return self.__debug

Expand Down
9 changes: 9 additions & 0 deletions config_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ logger_file_path=

# If this value is True, the VM is being paused during snapshot creation.
persist_memorystate=False

# Backup Bootable Disk Only
bootable_only=False

# Backup deactivated disks, set to False to skip deactivated disks backup (Recommend).
# This True - causes an error deleting a snapshot of the VM when the state is on.
with_disks_deactivated=False
# an array of exclude disks id,exampel ["6d811aa25-52a1-45c3-9f40-1c7c868182c9","6e78d7c2-5786-4dz2-a966-fd308c23f08e"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Suggested change
# an array of exclude disks id,exampel ["6d811aa25-52a1-45c3-9f40-1c7c868182c9","6e78d7c2-5786-4dz2-a966-fd308c23f08e"]
# an array of exclude disks id, example ["6d811aa25-52a1-45c3-9f40-1c7c868182c9","6e78d7c2-5786-4dz2-a966-fd308c23f08e"]

disks_id_exclude: []