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 all commits
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
44 changes: 44 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,29 @@ def main(argv):

vm = vm[0]

# Get the Attachments Disk
disk_attachments = vms_service.vm_service(vm.id).disk_attachments_service().list()
disks_backup = []
bootable_only = config.get_bootable_only()
for disk_attachment in disk_attachments:
if bootable_only:
if disk_attachment.bootable == True:
disks_backup.append(disk_attachment)
break
elif config.get_with_disks_deactivated() or disk_attachment.active:
disks_backup.append(disk_attachment)

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

for disk_attachment in disks_backup:
if disk_attachment.bootable == True:
logger.info(f"Finding bootable disk: {disk_attachment.id}")
else:
logger.info(f"Finding disk: {disk_attachment.id}")

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

Expand All @@ -349,6 +392,7 @@ def main(argv):
types.Snapshot(
description=config.get_snapshot_description(),
persist_memorystate=config.get_persist_memorystate(),
disk_attachments=disks_backup,
),
)
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, example ["6d811aa25-52a1-45c3-9f40-1c7c868182c9","6e78d7c2-5786-4dz2-a966-fd308c23f08e"]
disks_id_exclude: []