Created
May 23, 2023 20:03
-
-
Save falendary/1c97e8bb511c3bb2934e55f9efada222 to your computer and use it in GitHub Desktop.
Linux VM virsh do backup of qcow2 file and gzip it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
send_telegram_message() { | |
local token="[token_id]" | |
local chat_id="[chat_id]" | |
local text="$1" | |
curl -s -X POST "https://api.telegram.org/bot${token}/sendMessage" -d chat_id="${chat_id}" -d text="${text}" | |
} | |
# Define source and target directories | |
src_dir="/var/lib/libvirt/images/" | |
target_dir="/mnt/storage-box/backups/" | |
# Check if the correct number of parameters was provided | |
if [ "$#" -ne 1 ]; then | |
echo "You must enter exactly 1 command line argument (the VM name)" | |
exit 1 | |
fi | |
send_telegram_message "Going to backup ${1}" | |
# Define the file to copy | |
date_suffix=$(date +%Y-%m-%d) | |
#file="${src_dir}${1}-disk" | |
target_file="${target_dir}${1}-disk_${date_suffix}" | |
# Try with '-disk' suffix | |
file="${src_dir}${1}-disk" | |
# If the file doesn't exist, try without '-disk' suffix | |
if [[ ! -f "$file" ]]; then | |
file="${src_dir}${1}" | |
fi | |
# Check if the target directory exists | |
if [ ! -d "$target_dir" ] | |
then | |
echo "Target directory ${target_dir} does not exist." | |
send_telegram_message "⚠️ Box Storage is Unuavailable for ${1}" | |
exit 1 | |
fi | |
# Check if the file exists | |
if [ ! -f "$file" ] | |
then | |
echo "File ${file} does not exist." | |
send_telegram_message "⚠️ File does not exists for ${1}" | |
exit 1 | |
fi | |
# Check the integrity of the disk file | |
qemu-img check "$file" | |
# Check the exit status of the last command | |
if [ $? -ne 0 ]; then | |
send_telegram_message "⚠️ The disk file is corrupted: ${file}" | |
exit 1 | |
fi | |
# Get available space in target directory and file size in KB | |
avail_space=$(sudo df "$target_dir" | awk 'NR==2 {print $4}') | |
file_size=$(sudo du -k "$file" | cut -f1) | |
echo "Available space ${avail_space}" | |
echo "File size ${file_size}" | |
# Compare available space and file size | |
if (( file_size > avail_space )); then | |
send_telegram_message "⚠️ Not enough space in ${target_dir} for backup. Aborting." | |
echo "Not enough space in ${target_dir} for backup. Aborting." | |
exit 1 | |
fi | |
# Check if the VM is running | |
if sudo virsh domstate "${1}" | grep -q running ; then | |
# Shut down the VM before copying the disk file | |
echo "Shutting down the VM ${1}..." | |
sudo virsh shutdown "${1}" | |
if [ $? -eq 0 ] | |
then | |
echo "Successfully shut down the VM." | |
else | |
echo "Error occurred while shutting down the VM." | |
send_telegram_message "⚠️ Error occured while shutting down vm ${1}" | |
exit 1 | |
fi | |
# Wait for VM to completely shut down | |
while sudo virsh domstate "${1}" | grep -q running ; do sleep 5; done | |
fi | |
# Copy the file from the source directory to the target directory with progress | |
echo "Copying ${file} to ${target_dir}" | |
rsync -avz --progress "$file" "${target_file}" | stdbuf -oL tr '\r' '\n' | |
if [ $? -eq 0 ] | |
then | |
echo "Successfully copied ${file} to ${target_dir}" | |
else | |
echo "Error occurred while copying ${file}" | |
send_telegram_message "⚠️ Error occured while copying ${1}" | |
exit 1 | |
fi | |
echo "Compressing ${target_file} with gzip..." | |
dd if="${target_file}" bs=512K | pv | gzip > "${target_file}.gz" | |
if [ $? -eq 0 ] | |
then | |
echo "Successfully compressed ${target_file} to ${target_file}.gz" | |
echo "Size of ${target_file}.gz is: $(du -h ${target_file}.gz | cut -f1)" | |
# Remove the original .qcow2 file after successful compression | |
rm "${target_file}" | |
else | |
echo "Error occurred while compressing ${target_file}" | |
send_telegram_message "⚠️ Error occured while compressing ${1}" | |
exit 1 | |
fi | |
# Start the VM after the copy operation, but only if it was running before | |
if sudo virsh domstate "${1}" | grep -qv running ; then | |
echo "Starting the VM ${1}..." | |
sudo virsh start "${1}" | |
if [ $? -eq 0 ] | |
then | |
echo "Successfully started the VM." | |
else | |
echo "Error occurred while starting the VM." | |
send_telegram_message "⚠️ Error ocurred while starting vm ${1}" | |
exit 1 | |
fi | |
fi | |
echo "Finished copying and compressing files." | |
send_telegram_message "💚 Backup Finished ${1}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment