How to backup Proxmox 6.0

kamzata

Renowned Member
Jan 21, 2011
219
9
83
Italy
I created this script in order to backup and restore Proxmox 5.x version. Is there something that I had to change in order to backup and restore Proxmox 6.x version? Furthermore, can I restore this Proxmox 5.4.11 backup to Proxmox 6.x?

Code:
#!/bin/bash

case "$1" in

    backup)

                if [ -z "$2" ]; then
                        echo "No backup path specify. Use: ./proxmoxBnR backup /your/backup/path/ 20"
                        exit 1
                else
            backupPath=$2
            if ! mkdir -p "$backupPath"; then
                echo "I can't create directory $backupPath" >&2
                exit 8
            fi
                fi

        cd $backupPath

        dt=$(date '+%d-%m-%Y_%H-%M-%S');
        mkdir host-backup_"$dt"
        cd host-backup_"$dt"

        # Backup /var/lib/pve-cluster/
        tar -czf pve-cluster-backup.tar.gz /var/lib/pve-cluster

        # Backup /root/.ssh/ , there are two symlinks here to the shared pve config authorized_keys and authorized_keys.orig, don t worry about these two yet as they're stored in /var/lib/pve-cluster/
        tar -czf ssh-backup.tar.gz /root/.ssh

        # Backup /etc/corosync/
        tar -czf corosync-backup.tar.gz /etc/corosync

        # Backup /etc/cron*
        tar -czf cron-backup.tar.gz /etc/cron*
       
        # Backup /etc/hosts/
        cp /etc/hosts .

        # Backup /etc/network/interfaces
        cp /etc/network/interfaces .

                # Backup /etc/rsnapshot.conf
                cp /etc/rsnapshot.conf .

        cd ..

        # Tar the entire folder
        tar -czf host-backup_"$dt".tar.gz host-backup_"$dt"

        # Delete temporary folder
        rm -rf host-backup_"$dt"

        # Delete old backups
        if [ ! -z "$3" ] && [ "$3" -eq "$3" ]; then
            find $backupPath -mmin +$((60*24*"$3")) -exec rm {} \;
        fi
        ;;

    restore)

        if [ $2 -eq 0 ]; then
            echo "No backup archive passed! Select an archive to restore!"
            exit 1
        else
            if ! `/bin/tar tf $2 &> /dev/null`; then
                echo $2
                echo "The downloaded file is not a valid tar file"
                exit 1
            fi
        fi

        echo
        echo "##### RESTORING HOST #####"
        echo

        # Decompressing the archive
        tar xzvf $2

        f=$2
        path=${f%.tar.gz}
        base=$(basename "$path")
        cd ${base}

        ### Restore node and cluster configuration

        # Restore /etc/hosts/
        cp hosts /etc/hosts

        # Restore /etc/network/interfaces
        cp interfaces /etc/network/interfaces

                # Restore /etc/rsnapshot.conf
                cp rsnapshot.conf /etc/rsnapshot.conf

        # Stop all services
        systemctl stop pvestatd.service
        systemctl stop pvedaemon.service
        systemctl stop pve-cluster.service

        # Restore the files in /root/.ssh/
        mv ssh-backup.tar.gz /
        cd / ; tar -xzf ssh-backup.tar.gz
        rm ssh-backup.tar.gz

        # Replace /var/lib/pve-cluster/ with your backup copy
        cd -
        mv pve-cluster-backup.tar.gz /
        rm -rf /var/lib/pve-cluster
        cd / ; tar -xzf pve-cluster-backup.tar.gz
        rm pve-cluster-backup.tar.gz

        # Replace /etc/corosync/ with your backup copy
        cd -
        mv corosync-backup.tar.gz /
        rm -rf /etc/corosync
        cd / ; tar -xzf corosync-backup.tar.gz
        rm corosync-backup.tar.gz

        # Replace /etc/cron* with your backup copy
        cd -
        mv cron-backup.tar.gz /
        cd / ; tar -xzf cron-backup.tar.gz
        rm cron-backup.tar.gz

        # Start pve-cluster
        systemctl start pve-cluster.service

        # Restore the two ssh symlinks:
        ln -sf /etc/pve/priv/authorized_keys /root/.ssh/authorized_keys
        ln -sf /etc/pve/priv/authorized_keys /root/.ssh/authorized_keys.orig

        # Start the rest of the services:
        systemctl start pvestatd.service
        systemctl start pvedaemon.service

        # Delete backup folder
        cd -; cd ..
        rm -rf ${base}

        echo
        echo "---------------- YOU NEED TO REBOOT NOW! -----------------"
        echo "Then you probably want to run './proxmoxBnR.sh installAll'"
        echo "in order to install all packages not included by Proxmox. "
        echo
        ;;

    installAll)
       
        apt-get update && apt-get install rsnapshot ncdu htop curlftpfs 
        ;;

    *)
        echo
        echo "#####################################################################"
        echo "How to use:"
        echo
        echo "To backup ---> ./proxmoxBeR backup /your/backup/path/ 20"
        echo "To restore ---> ./proxmoxBnR.sh restore path/to/my-backup.tar.gz"
        echo "To install all packages not included by Proxmox ---> ./proxmoxBnR.sh installAll"
        echo "#####################################################################"
        echo
        ;;
esac


_proxmoxBnR_complete()
{
    local options="backup restore"

    local current_word="${COMP_WORDS[COMP_CWORD]}"

    COMPREPLY=($(compgen -W "${options}" "$current_word"))
}
complete -F _proxmoxBnR_complete proxmoxBnR.sh
 
Last edited:
I do wish there was better *system* backup built-in for node configuration. Not everybody uses ZFS/Ceph, and sometimes those of us who don't can feel a little left out (I know it isn't intentional, but that's still how it feels).

Kamzata, I like the look of your script.

I just think that an official script, maybe linked into the GUI, would be useful. Imagine it: Download config via GUI (or cause it to be saved every day and copied off-server). Node blows up and needs replacing/re-installing. After you have access to the GUI again, just upload the config backup and boom - you are up and running again and joined to the cluster! (of course those of us with locally stored VMs may need to restore them too).

Of course Kamzata's script can do this (I hope -- not looked into the details) just without the GUI.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!