This commit is contained in:
Cryptoval2 2025-06-26 12:28:07 -04:00
commit 78f8c8d039
6 changed files with 149 additions and 0 deletions

0
.gitignore vendored Normal file
View File

39
backup_vm.sh Executable file
View File

@ -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 <VM Domain Name>"
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."

68
compress_qcow2.sh Executable file
View File

@ -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 $*"

42
compress_qcow2_file.sh Executable file
View File

@ -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 $*"

0
crontab.txt Normal file
View File

0
crontab_root.txt Normal file
View File