- Shell 100%
| .gitignore | ||
| readme.md | ||
| vm_backup.sh | ||
KVM/QEMU VM Backup & Snapshot Rotator
A robust Bash script designed to automate the lifecycle of QEMU/KVM virtual machine backups. It manages internal snapshots, performs sparse disk copies to save space, and synchronizes the final compressed backup to Google Drive (or any cloud provider) using rclone.
🚀 Features
- Automated Snapshot Rotation: Keeps a user-defined number of internal snapshots (e.g., keep the last 7 days) and deletes the oldest.
- Space Efficient: Uses
--sparse=alwaysduring disk copying to ensure the backup size only reflects actual data used, not the full virtual disk size. - Full VM Portability: Backs up both the
.qcow2disk image and the VM's XML configuration. - Cloud Integration: Seamlessly uploads to Google Drive via
rclone. - Cron-Ready: Designed to run via a user's crontab without requiring a manual password entry, thanks to specific
sudoersintegration.
📋 Prerequisites
Before using this script, ensure the following tools are installed on your host system:
- libvirt-clients (for
virsh) - rclone (configured with a remote, e.g.,
gdrive) - zip
- sudo
🛠️ Setup & Installation
1. Configure Rclone
Ensure your Google Drive remote is set up:
rclone config
# Verify it works
rclone ls gdrive:
2. Install the Script
Clone this repository and move the script to a secure location:
sudo mkdir -p /usr/local/bin/scripts
sudo cp vm_backup.sh /usr/local/bin/scripts/
sudo chmod +x /usr/local/bin/scripts/vm_backup.sh
sudo chown root:root /usr/local/bin/scripts/vm_backup.sh
3. Grant Passwordless Sudo Permissions
Since the script needs to access /var/lib/libvirt/images, it must run with root privileges. To allow your user to run this specific script via cron without a password:
- Open the sudoers file:
sudo visudo - Add the following lines at the end:
# Allow user to run the VM backup script without a password your_username ALL=(ALL) NOPASSWD: /usr/local/bin/scripts/vm_backup.sh # Prevent environment variable errors (like TERMINFO) Defaults env_keep += "TERMINFO"
⚙️ Configuration
Open vm_backup.sh and update the variables in the CONFIGURATION section:
BACKUP_ROOT: Where the script will temporarily store the ZIP file before uploading.USER_NAME: Your Linux username (used to find therclone.confpath).GDRIVE_REMOTE: The name of your rclone remote and the destination folder.
📖 Usage
Manual Execution
sudo /usr/local/bin/scripts/vm_backup.sh <VM_NAME> <SNAPSHOT_RETENTION_COUNT>
Example: Back up 'ubuntu-vm' and keep the last 7 daily snapshots:
sudo /usr/local/bin/scripts/vm_backup.sh ubuntu-vm 7
Automation via Crontab
To schedule a daily backup at 2:00 AM, edit your user's crontab:
crontab -e
Add the following line:
0 2 * * * sudo /usr/local/bin/scripts/vm_backup.sh ubuntu-vm 7 >> ~/vm_backup.log 2>&1
⚠️ Security Note
This script is intended to be owned by root to prevent unauthorized users from modifying the backup logic and gaining elevated privileges through the NOPASSWD sudo entry. Always ensure the script's write permissions are restricted.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.