From 78f8c8d039a19b64e93f1aa212eaedf6e42f9d0f Mon Sep 17 00:00:00 2001 From: Cryptoval2 Date: Thu, 26 Jun 2025 12:28:07 -0400 Subject: [PATCH] initial --- .gitignore | 0 backup_vm.sh | 39 ++++++++++++++++++++++++ compress_qcow2.sh | 68 ++++++++++++++++++++++++++++++++++++++++++ compress_qcow2_file.sh | 42 ++++++++++++++++++++++++++ crontab.txt | 0 crontab_root.txt | 0 6 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100755 backup_vm.sh create mode 100755 compress_qcow2.sh create mode 100755 compress_qcow2_file.sh create mode 100644 crontab.txt create mode 100644 crontab_root.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/backup_vm.sh b/backup_vm.sh new file mode 100755 index 0000000..1ad62c9 --- /dev/null +++ b/backup_vm.sh @@ -0,0 +1,39 @@ +#!/bin/bash +echo Starting $0 $* + +VmName=${1} + +# --------------------------------------------------- +# Cronjob requires explicit connection to local server +# --------------------------------------------------- +VirshCmd="/usr/bin/virsh -c qemu:///system" + +if [ "${VmName}" == "" ] +then + echo "Usage: $0 " + exit 1 +fi + +Today=$(date +%Y%m%d) + +BackupDir=/localdisk/backup/qcow2 +GuestQcow2Files=$(${VirshCmd} domblklist ${VmName} | grep qcow2 | awk '{print $2}') + +Cmd="mkdir -p ${BackupDir}" +echo ${Cmd} && eval ${Cmd} + +Cmd="${VirshCmd} suspend ${VmName}" +echo ${Cmd} && eval ${Cmd} + +for qcow2_file in ${GuestQcow2Files} +do + Cmd="cp ${qcow2_file} ${BackupDir}/${Today}.$(basename ${qcow2_file})" + echo ${Cmd} && eval ${Cmd} +done + +Cmd="${VirshCmd} resume ${VmName}" +echo ${Cmd} && eval ${Cmd} + +echo "$0 Done." + + diff --git a/compress_qcow2.sh b/compress_qcow2.sh new file mode 100755 index 0000000..eb033ff --- /dev/null +++ b/compress_qcow2.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +if [ "$(id -u)" -ne 0 ]; then + echo "This script must be run as root." >&2 + exit 1 +fi + +# ------ Settings +Qcow2Dir=/localdisk/backup/qcow2 +ShrunkDir=/opt/vm_drive/shrunk +TmpDir=/localdisk/tmp + +mkdir -p ${ShrunkDir} +mkdir -p ${TmpDir} +# ------ Settings + + +Qcow2Files=$(ls -t ${Qcow2Dir}/*.qcow2) +echo "Files to compress: ${Qcow2Files}" + +for file in ${Qcow2Files} +do + echo "Compressing file ${file}..." + SparseFile="${ShrunkDir}/$(basename ${file}).sparse" + ShrunkFile="${ShrunkDir}/$(basename ${file}).shrunk" + if [ -f ${ShrunkFile} ] + then + echo Shrunk file ${ShrunkFile} exists. Skipping... + continue + fi + Cmd="/usr/bin/virt-sparsify" + Cmd="${Cmd} --check-tmpdir=continue" + Cmd="${Cmd} --tmp=${TmpDir}" + Cmd="${Cmd} -q" + Cmd="${Cmd} ${file} ${SparseFile}" + Cmd="${Cmd} && /usr/bin/qemu-img convert" + Cmd="${Cmd} -c" + Cmd="${Cmd} -O qcow2" + Cmd="${Cmd} ${SparseFile} ${ShrunkFile}" + echo ${Cmd} + eval ${Cmd} + + if [ ${?} -ne 0 ] + then + echo "Compression failed. Removing ${ShrunkFile}" + rm ${ShrunkFile} + else + filesz=$(stat -c %s ${file}) + cfilesz=$(stat -c %s ${ShrunkFile}) + + ratio=$((cfilesz * 100 / filesz)) + echo "Compression is successful. Compression ratio is ${ratio}%" + rm ${SparseFile} + + Cmd="chown oleg:oleg ${ShrunkFile}" + echo ${Cmd} + eval ${Cmd} + ## + GuestFile="${ShrunkFile#*.}" + GuestFile="${GuestFile%.shrunk}" + Cmd="ln -snf ${ShrunkFile} /opt/vm_drive/${GuestFile}" + echo ${Cmd} + eval ${Cmd} + fi +done + +echo "Done $0 $*" + diff --git a/compress_qcow2_file.sh b/compress_qcow2_file.sh new file mode 100755 index 0000000..f75fc8d --- /dev/null +++ b/compress_qcow2_file.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# ------ Settings +TmpDir=/localdisk/tmp + +# ------ Settings + +mkdir -p ${TmpDir} +file=${1} +out_file=${2} + +SparseFile="${out_file}.sparse" +ShrunkFile="${out_file}.shrunk" + +echo "Compressing file ${file}..." +Cmd="/usr/bin/virt-sparsify" +Cmd="${Cmd} --check-tmpdir=continue" +Cmd="${Cmd} --tmp=${TmpDir}" +Cmd="${Cmd} -q" +Cmd="${Cmd} ${file} ${SparseFile}" +Cmd="${Cmd} && /usr/bin/qemu-img convert" +Cmd="${Cmd} -c" +Cmd="${Cmd} -O qcow2" +Cmd="${Cmd} ${SparseFile} ${ShrunkFile}" +echo ${Cmd} +eval ${Cmd} + +if [ ${?} -ne 0 ] +then + echo "Compression failed. Removing ${ShrunkFile}" + rm ${ShrunkFile} +else + filesz=$(stat -c %s ${file}) + cfilesz=$(stat -c %s ${ShrunkFile}) + + ratio=$((cfilesz * 100 / filesz)) + echo "Compression is successful. Compression ratio is ${ratio}%" + rm ${SparseFile} +fi + +echo "Done $0 $*" + diff --git a/crontab.txt b/crontab.txt new file mode 100644 index 0000000..e69de29 diff --git a/crontab_root.txt b/crontab_root.txt new file mode 100644 index 0000000..e69de29