Skip to content

Commit 03c8e1e

Browse files
jmsperuclaude
authored andcommitted
nasbackup.sh: add optional backup compression via -c flag
Add -c/--compress flag that produces compressed qcow2 backup files, reducing storage usage on the NAS backup target. For stopped VMs: passes -c to qemu-img convert directly. For running VMs: re-compresses push backup output with qemu-img convert -c after the backup job completes. Compression is off by default to preserve existing behavior and avoid increased CPU usage on hosts that don't need it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 61afb4c commit 03c8e1e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

scripts/vm/hypervisor/kvm/nasbackup.sh

Lines changed: 22 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+
COMPRESS=""
3435
logFile="/var/log/cloudstack/agent/agent.log"
3536

3637
log() {
@@ -112,6 +113,21 @@ backup_running_vm() {
112113
sleep 5
113114
done
114115
rm -f $dest/backup.xml
116+
117+
# Compress backup files if requested
118+
if [[ "$COMPRESS" == "true" ]]; then
119+
log -ne "Compressing backup files for $VM"
120+
for img in "$dest"/*.qcow2; do
121+
[[ -f "$img" ]] || continue
122+
local tmp_img="${img}.tmp"
123+
if qemu-img convert -c -O qcow2 "$img" "$tmp_img" 2>&1 | tee -a "$logFile"; then
124+
mv "$tmp_img" "$img"
125+
else
126+
log -ne "Warning: compression failed for $img, keeping uncompressed"
127+
rm -f "$tmp_img"
128+
fi
129+
done
130+
fi
115131
sync
116132

117133
# Print statistics
@@ -131,7 +147,7 @@ backup_stopped_vm() {
131147
name="root"
132148
for disk in $DISK_PATHS; do
133149
volUuid="${disk##*/}"
134-
qemu-img convert -O qcow2 $disk $dest/$name.$volUuid.qcow2 | tee -a "$logFile"
150+
qemu-img convert $([[ "$COMPRESS" == "true" ]] && echo "-c") -O qcow2 $disk $dest/$name.$volUuid.qcow2 | tee -a "$logFile"
135151
name="datadisk"
136152
done
137153
sync
@@ -165,7 +181,7 @@ mount_operation() {
165181

166182
function usage {
167183
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>"
184+
echo "Usage: $0 -o <operation> -v|--vm <domain name> -t <storage type> -s <storage address> -m <mount options> -p <backup path> -d <disks path> [-c]"
169185
echo ""
170186
exit 1
171187
}
@@ -207,6 +223,10 @@ while [[ $# -gt 0 ]]; do
207223
shift
208224
shift
209225
;;
226+
-c|--compress)
227+
COMPRESS="true"
228+
shift
229+
;;
210230
-h|--help)
211231
usage
212232
shift

0 commit comments

Comments
 (0)