mirror of
https://github.com/netinvent/npbackup.git
synced 2025-09-05 20:44:51 +08:00
kvm_qemu: Keep VM xml files, also use new snapshot-delete with fallback
This commit is contained in:
parent
fe0efddaff
commit
5fd044a546
1 changed files with 60 additions and 49 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Script ver 2025082101
|
||||
# Script ver 2025082801
|
||||
|
||||
#TODO: blockcommit removes current snapshots, even if not done by cube
|
||||
# - it's interesting to make housekeeping, let's make this an option
|
||||
|
@ -79,20 +79,24 @@ function create_snapshot {
|
|||
|
||||
# Don't redirect direct virsh output or SELinux may complain that we cannot write with virsh context
|
||||
xml=$(virsh dumpxml --security-info $vm || log "Failed to create XML file" "ERROR")
|
||||
echo "${xml}" > "${ROOT_DIR}/${vm}.xml"
|
||||
echo "${ROOT_DIR}/${vm}.xml" >> "$BACKUP_FILE_LIST"
|
||||
vm_xml=""
|
||||
|
||||
# Get current disk paths to include into snapshot
|
||||
for disk_path in $(virsh domblklist $vm --details | grep file | grep disk | awk '{print $4}'); do
|
||||
if [ -f "${disk_path}" ]; then
|
||||
# Add current disk path and all necessary backing files for current disk to backup file list
|
||||
echo "${disk_path}" >> "$BACKUP_FILE_LIST"
|
||||
echo "${disk_path}" >> "$BACKUP_FILE_LIST" || log "Cannot add ${disk_path} to backup file list"
|
||||
qemu-img info --backing-chain -U "$disk_path" | grep "backing file:" | awk '{print $3}' >> "$BACKUP_FILE_LIST"
|
||||
log "Current disk path: $disk_path"
|
||||
if [ "${vm_xml}" == "" ]; then
|
||||
vm_xml="$(dirname "${disk_path}")/$vm.xml"
|
||||
echo "${xml}" > "${vm_xml}" || log "Cannot create XML"
|
||||
echo "${vm_xml}" >> "${BACKUP_FILE_LIST}" || log "Cannot add xml file to backup file list"
|
||||
fi
|
||||
else
|
||||
log "$vm has a non existent disk path: $disk_path. Cannot backup this disk"
|
||||
# Let's still include this file in the backup list so we are sure backup will be marked as failed
|
||||
echo "${disk_path}" >> "$BACKUP_FILE_LIST"
|
||||
echo "${disk_path}" >> "$BACKUP_FILE_LIST" || log "Cannot add non existing ${disk_path} to backup file list"
|
||||
fi
|
||||
done
|
||||
|
||||
|
@ -187,6 +191,13 @@ function remove_snapshot {
|
|||
local vm="${1}"
|
||||
local backup_identifier="${2}"
|
||||
|
||||
can_delete_metadata=false
|
||||
|
||||
log "Trying to properly delete snapshot for $disk_name: $disk_path"
|
||||
valgrind virsh snapshot-delete $vm --snapshotname "${backup_identifier}" >> "$LOG_FILE" 2>&1
|
||||
exit_code=$?
|
||||
if [ $exit_code -ne 0 ]; then
|
||||
log "Proper snapshot deletion failed with code $exit_code"
|
||||
can_delete_metadata=true
|
||||
for disk_name in $(virsh domblklist $vm --details | grep file | grep disk | grep "${backup_identifier}" | awk '{print $3}'); do
|
||||
disk_path=$(virsh domblklist $vm --details | grep file | grep disk | grep "${backup_identifier}" | grep "${disk_name}" | awk '{print $4}')
|
||||
|
@ -197,7 +208,7 @@ function remove_snapshot {
|
|||
# virsh blockcommit only works if machine is running, else we need to use qemu-img
|
||||
if [ "$(virsh domstate $vm)" == "running" ]; then
|
||||
log "Trying to online blockcommit for $disk_name: $disk_path"
|
||||
virsh blockcommit $vm "$disk_name" --active --pivot --verbose --delete >> "$LOG_FILE" 2>&1
|
||||
valgrind virsh blockcommit $vm "$disk_name" --active --pivot --verbose --delete >> "$LOG_FILE" 2>&1
|
||||
else
|
||||
log "Trying to offline blockcommit for $disk_name: $disk_path"
|
||||
# -p = progress, we actually don't need that hee
|
||||
|
@ -231,6 +242,7 @@ function remove_snapshot {
|
|||
CURRENT_VM_SNAPSHOT=""
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# delete snapshot metadata
|
||||
if [ $can_delete_metadata == true ]; then
|
||||
|
@ -265,8 +277,7 @@ function run {
|
|||
if [ "${CURRENT_VM_SNAPSHOT}" != "" ]; then
|
||||
remove_snapshot "${CURRENT_VM_SNAPSHOT}" "${BACKUP_IDENTIFIER}"
|
||||
fi
|
||||
log "Delete former XML file"
|
||||
rm -f "${ROOT_DIR}/${vm}.xml" > /dev/null 2>&1
|
||||
log "Delete temporary files"
|
||||
rm -f "${SNAPSHOT_FAILED_FILE}" > /dev/null 2>&1
|
||||
done
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue