Skip to content

Commit 6c8526e

Browse files
jmsperuclaude
andcommitted
nasbackup.sh: add bandwidth throttle via -b flag
Add -b/--bandwidth flag (MiB/s) to limit backup I/O impact on production workloads. For running VMs: uses virsh blockjob --bandwidth to throttle the QEMU push backup job per disk. For stopped VMs: uses qemu-img convert -r rate limit and ionice -c 3 (idle I/O class) to minimize impact on other VMs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 61afb4c commit 6c8526e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

scripts/vm/hypervisor/kvm/nasbackup.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NAS_ADDRESS=""
3131
MOUNT_OPTS=""
3232
BACKUP_DIR=""
3333
DISK_PATHS=""
34+
BANDWIDTH=""
3435
logFile="/var/log/cloudstack/agent/agent.log"
3536

3637
log() {
@@ -102,6 +103,14 @@ backup_running_vm() {
102103
# Start push backup
103104
virsh -c qemu:///system backup-begin --domain $VM --backupxml $dest/backup.xml > /dev/null 2>/dev/null
104105

106+
# Throttle backup bandwidth if requested (MiB/s per disk)
107+
if [[ -n "$BANDWIDTH" ]]; then
108+
for disk in $(virsh -c qemu:///system domblklist $VM --details 2>/dev/null | awk '/disk/{print$3}'); do
109+
virsh -c qemu:///system blockjob $VM $disk --bandwidth "${BANDWIDTH}" 2>/dev/null || true
110+
done
111+
log -ne "Backup bandwidth limited to ${BANDWIDTH} MiB/s per disk for $VM"
112+
fi
113+
105114
# Backup domain information
106115
virsh -c qemu:///system dumpxml $VM > $dest/domain-config.xml 2>/dev/null
107116
virsh -c qemu:///system dominfo $VM > $dest/dominfo.xml 2>/dev/null
@@ -131,7 +140,7 @@ backup_stopped_vm() {
131140
name="root"
132141
for disk in $DISK_PATHS; do
133142
volUuid="${disk##*/}"
134-
qemu-img convert -O qcow2 $disk $dest/$name.$volUuid.qcow2 | tee -a "$logFile"
143+
ionice -c 3 qemu-img convert $([[ -n "$BANDWIDTH" ]] && echo "-r" "${BANDWIDTH}M") -O qcow2 $disk $dest/$name.$volUuid.qcow2 | tee -a "$logFile"
135144
name="datadisk"
136145
done
137146
sync
@@ -165,7 +174,7 @@ mount_operation() {
165174

166175
function usage {
167176
echo ""
168-
echo "Usage: $0 -o <operation> -v|--vm <domain name> -t <storage type> -s <storage address> -m <mount options> -p <backup path> -d <disks path>"
177+
echo "Usage: $0 -o <operation> -v|--vm <domain name> -t <storage type> -s <storage address> -m <mount options> -p <backup path> -d <disks path> [-b <MiB/s>]"
169178
echo ""
170179
exit 1
171180
}
@@ -207,6 +216,11 @@ while [[ $# -gt 0 ]]; do
207216
shift
208217
shift
209218
;;
219+
-b|--bandwidth)
220+
BANDWIDTH="$2"
221+
shift
222+
shift
223+
;;
210224
-h|--help)
211225
usage
212226
shift

0 commit comments

Comments
 (0)