Skip to content

Instantly share code, notes, and snippets.

@mrpeardotnet
Created December 17, 2019 18:03
Show Gist options
  • Save mrpeardotnet/6bdc4b504f43ce57fa7eaee96d376edf to your computer and use it in GitHub Desktop.
Save mrpeardotnet/6bdc4b504f43ce57fa7eaee96d376edf to your computer and use it in GitHub Desktop.
Proxmox PVE Host Config Backup Script

Proxmox PVE Host Config Backup Script

This script can be used to backup essential configuration files from the Proxmox Virtual Enivronment (PVE) host.

The script will create backups using tar with specified backup prefix and date and time stamp in the file name. Script will also delete backups that are older then number of days specified.

Create backup script file

To create backup script that will be executed every day we can create backup script in /etc/cron.daily/ folder. We need to make it writeable by root (creator) only, but readable and executable by everyone:

touch /etc/cron.daily/pvehost-backup
chmod 755 /etc/cron.daily/pvehost-backup

Copy and paste following script into the created file (e.g. using vim.tiny):

#!/bin/sh
BACKUP_PATH="/var/tmp/"
BACKUP_FILE="pve-host"
KEEP_DAYS=7
PVE_BACKUP_SET="/etc/pve/ /etc/lvm/ /etc/modprobe.d/ /etc/network/interfaces /etc/vzdump.conf /etc/sysctl.conf /etc/resolv.conf /etc/ksmtuned.conf /etc/hosts /etc/hostname /etc/cron* /etc/aliases"
PVE_CUSTOM_BACKUP_SET="/etc/apcupsd/ /etc/multipath/ /etc/multipath.conf"

tar -czf $BACKUP_PATH$BACKUP_FILE-$(date +%Y_%m_%d-%H_%M_%S).tar.gz --absolute-names $PVE_BACKUP_SET $PVE_CUSTOM_BACKUP_SET
find $BACKUP_PATH$BACKUP_FILE-* -mindepth 0 -maxdepth 0 -depth -mtime +$KEEP_DAYS -delete

Note: Please modify the PVE_CUSTOM_BACKUP_SET variable to fit your PVE host needs. You can leave it as empty string ("") if no host specific configuration is needed.

Don't forget to change path where to store backups, the best way is to store backups outside physical host, e.g. on attached NAS storage.

Modify variables

You can modify variables to fit backups for your individual hosts:

  • BACKUP_PATH to specifiy where to store backups,
  • BACKUP_FILE to specify backups file prefix,
  • KEEP_DAYS to specify how many old backups to keep (in days)
  • PVE_CUSTOM_BACKUP_SET to add your installation specific folders and/or files,
  • PVE_BACKUP_SET defines standard set of folders and config files for generic PVE host.
@mrpeardotnet
Copy link
Author

I usually copy files from archive to fresh host by hand. Not all files can be copied out of the box and sometimes needs to be edited.

@Uuugh
Copy link

Uuugh commented Jan 6, 2021

Thank you for sharing this script. Are there particular files that need to be modified if restoring a host on a single-host homelab?

Thanks again!

@bttd
Copy link

bttd commented Feb 23, 2021

Hi,

My question is the same as @Uuugh

@pharpe
Copy link

pharpe commented Feb 28, 2021

I'm having an issue where my proxmox host lost network connectivity. I've been unable to resolve it so I'm considering restoring from backup. Can I boot from a usb drive and just copy the backup files back onto my hard drive?

@edobez
Copy link

edobez commented Mar 30, 2021

Just wanted to link this other script that has the same goal: https://github.com/DerDanilo/proxmox-stuff/blob/master/prox_config_backup.sh

@euro2
Copy link

euro2 commented Apr 5, 2021

I'm having an issue where my proxmox host lost network connectivity. I've been unable to resolve it so I'm considering restoring from backup. Can I boot from a usb drive and just copy the backup files back onto my hard drive?

Consider plugging in the network cable in another NIC if you have one. I lost connectivity too but on my motherboard when i change hardware the nic ports get served up in a different order so proxmox grabs the wrong one and thinks its the right one.

I went through a full reinstall only to find out i had to plug in the cable in the other nic :/

@chengkinhung
Copy link

chengkinhung commented Oct 10, 2021

Nice script, but how can you backup /etc/pve with tar command as it is Cluster File System (pmxcfs) ? I am looking the way to restore the /etc/pve from backup but not success.

@larryl79
Copy link

Nice script. saved my life

@bandoleritoz
Copy link

GOOD :)

@cojarbi
Copy link

cojarbi commented Feb 23, 2022

Hey guys great script. Is there one for restore?

Edit, Untar and copy content to the root folder. I had to do it via rescue

@Fernando-Checa
Copy link

Since my NAS starts a bit after PVE, I can't mount the NFS share backup destination on boot.
I added the mount point to /etc/fstab like this:

192.168.1.xx:/mnt/Pool1/Proxmox/pve_backup /home/pve_backup nfs noauto,users 0 0

so doesn't fail on boot.

Then I added this to the script to check if it is mounted and mount it if not:

#!/bin/sh
BACKUP_PATH="/home/pve_backup/"
BACKUP_FILE="pve-host"
KEEP_DAYS=7
PVE_BACKUP_SET="/etc/pve/ /etc/lvm/ /etc/modprobe.d/ /etc/network/interfaces /etc/vzdump.conf /etc/sysctl.conf /etc/resolv.conf /etc/ksmtuned.conf /etc/hosts /etc/hostname /etc/cron* /etc/aliases"
PVE_CUSTOM_BACKUP_SET="/etc/apcupsd/ /etc/multipath/ /etc/multipath.conf"

mountpoint -q /home/pve_backup || mount /home/pve_backup

tar -czf $BACKUP_PATH$BACKUP_FILE-$(date +%Y_%m_%d-%H_%M_%S).tar.gz --absolute-names $PVE_BACKUP_SET $PVE_CUSTOM_BACKUP_SET
find $BACKUP_PATH$BACKUP_FILE-* -mindepth 0 -maxdepth 0 -depth -mtime +$KEEP_DAYS -delete

@yuriw
Copy link

yuriw commented Jul 8, 2023

I've messed up my pve7to8 upgrade and looking for ways to recover.
I do have a /etc dir backup. What would be the steps to restore the pve host configuration?
TIA

@MhzDev
Copy link

MhzDev commented Jul 18, 2023

@yuriw I had to do this recently, I installed proxmox from scratch and then copied over some files from the backup using the list here https://pve.proxmox.com/wiki/Proxmox_Cluster_File_System_(pmxcfs) as a reference, especially the VM and LXC folders and config. Not all the files in the backup have to be copied and looking inside the text file can help to understand if it's needed or not.

@yuriw
Copy link

yuriw commented Jul 18, 2023

@MhzDev Thx!!
I wonder if anybody has done a backup script for all these files :)

@yuriw
Copy link

yuriw commented Jul 18, 2023

I need to try this scrip tho!

@lifecorp-kris
Copy link

This script will backup your PVE configs, setup a cron to a server not in the cluster is how I run it.

https://gist.github.com/mrpeardotnet/6bdc4b504f43ce57fa7eaee96d376edf

@yuriw
Copy link

yuriw commented Jul 18, 2023

@lifecorp-kris thank you!

It'd be really good if this script could be running from a remote (not pve) host with args, e.g.
pvehost-backup --host=192.168.XX.YY --username=root --password=PASSWORD

@UncleBabyBilly
Copy link

How can we test this script independently? I've got a valid backup path and no backup files get generated.

@jaroslawhartman
Copy link

Good stuff, thanks for posting!

One note based on 6.5. Recovery:

If you have major problems with your Proxmox VE host, for example hardware issues, it could be helpful to copy the pmxcfs database file /var/lib/pve-cluster/config.db, and move it to a new Proxmox VE host. On the new host (with nothing running), you need to stop the pve-cluster service and replace the config.db file (required permissions 0600). Following this, adapt /etc/hostname and /etc/hosts according to the lost Proxmox VE host, then reboot and check (and don’t forget your VM/CT data).

So in my setup I backup /var/lib/pve-cluster/config.db as well.

@AJolly
Copy link

AJolly commented Apr 4, 2024

FY that config.db file is what actually holds most of the /etc/proxmox files

@jpaulickcz
Copy link

As someone who's just wiped the wrong disk, add /etc/pve/qemu-server to the paths (holds VM configs). Will save you quite a bit of work when restoring VMs that were not backed up and only have the raw disk left.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment