A bash script for backing up libvirt/KVM virtual machines using BorgBackup with automated snapshot management.
- Snapshot or suspend mode: Choose between creating external snapshots or suspending VMs during backup
- BorgBackup integration: Deduplicated, compressed backups with efficient storage
- Flexible retention: Configurable daily, weekly, and monthly backup retention
- Environment-based config: All settings in external
.envfiles - VM state handling: Works with both running and shut off VMs
- Atomic operations: Safe snapshot creation and automatic cleanup
- libvirt/KVM with virsh
- BorgBackup (
borgbackuppackage) - SSH access to backup storage
- SSH key for authentication
- Copy the example environment file:
cp .env.example .env- Edit
.envand configure:
# Borg backup configuration
BORG_REPO_BASE="ssh://user@host:port/./backups"
BORG_RSH="ssh -i ~/.ssh/backup"
BORG_RELOCATED_REPO_ACCESS_IS_OK="yes"
BORG_PASSPHRASE="your-secure-passphrase"
# Retention policy (leave empty to skip)
KEEP_DAILY="7"
KEEP_WEEKLY="4"
KEEP_MONTHLY="6"- Secure your
.envfile:
chmod 600 .env./vm-backup <domain> <mode> <env-file>Parameters:
domain: Name of the libvirt VM domainmode: Eithersnapshotorsuspendsnapshot: Creates external disk snapshots (VM keeps running)suspend: Suspends VM during backup (brief downtime)
env-file: Path to environment configuration file
Examples:
# Backup with snapshot (no downtime)
./vm-backup myvm snapshot .env
# Backup with suspend (brief downtime)
./vm-backup myvm suspend .env
# Use different env for specific VM
./vm-backup production snapshot /etc/vm-backup/prod.envYou can configure different retention strategies by setting the KEEP_* variables in your .env file. Leave any parameter empty to skip it.
Daily backups (default):
KEEP_DAILY="7"
KEEP_WEEKLY="4"
KEEP_MONTHLY="6"Weekly backups only:
KEEP_DAILY=""
KEEP_WEEKLY="8"
KEEP_MONTHLY=""Monthly backups only:
KEEP_DAILY=""
KEEP_WEEKLY=""
KEEP_MONTHLY="12"Mixed strategy:
KEEP_DAILY=""
KEEP_WEEKLY="4"
KEEP_MONTHLY="12"You can create separate .env files for different VMs with different retention policies:
./vm-backup webserver snapshot .env.daily
./vm-backup database snapshot .env.hourly
./vm-backup archive suspend .env.weekly- Loads configuration from specified
.envfile - Checks VM state (must be running or shut off)
- Creates snapshot or suspends VM (if running)
- Initializes Borg repository (first run only)
- Creates compressed backup archive of:
- All VM disk images
- VM XML definition
- Prunes old backups per configured retention policy
- Merges snapshots back or resumes VM
- Cleans up temporary files
Retention is fully configurable via .env file. Set only the intervals you need:
KEEP_DAILY: Number of daily backups to keepKEEP_WEEKLY: Number of weekly backups to keepKEEP_MONTHLY: Number of monthly backups to keep
To restore a backup:
# List available archives
borg list ssh://user@host:port/./backups/myvm
# Extract specific archive
borg extract ssh://user@host:port/./backups/myvm::myvm-2025-12-29_10-30-00