Skip to content

Instantly share code, notes, and snippets.

@husniadil
Last active September 6, 2024 13:35
Show Gist options
  • Save husniadil/877ae23f38ef92c2e15842bc00332d8f to your computer and use it in GitHub Desktop.
Save husniadil/877ae23f38ef92c2e15842bc00332d8f to your computer and use it in GitHub Desktop.
Proxmox LXC Docker Backup and Restore

Proxmox Docker Volume Backup and Restore Script

This script performs backup and restore operations for Docker volumes within Proxmox LXC containers.

Requirements

  • Ensure that sshpass is installed. For installation instructions, visit: Cyberciti: SSH Password Provider.
  • Root login with a password must be enabled on the Proxmox host. Follow these steps:
    1. Edit the /etc/ssh/sshd_config file and apply the following configuration settings:
      • ListenAddress 0.0.0.0
      • PasswordAuthentication yes
      • PermitRootLogin yes
    2. Restart the SSH service using the command:
      systemctl restart sshd

Usage

To use the script for backup and restore operations, execute the following commands:

  • To backup a volume:

    ./proxmox.sh backup --host <host> --user <user> --lxc <lxc_number> --volume <volume> --path <path>
  • To restore a volume:

    ./proxmox.sh restore --host <host> --user <user> --lxc <lxc_number> --volume <volume> --path <path>

Parameters

  • host: Proxmox host IP address or hostname
  • user: Proxmox host username
  • lxc: LXC container number
  • volume: Name of the Docker volume
  • path: Path for the backup location

Examples

  • To backup a volume:

    ./proxmox.sh backup --host 192.168.0.100 --user root --lxc 100 --volume searxng_data --path /Users/husni/backup

    The backup file will be saved to /Users/husni/backup/searxng_data-backup.tar.gz.

  • To restore a volume:

    ./proxmox.sh restore --host 192.168.0.100 --user root --lxc 100 --volume searxng_data --path /Users/husni/backup

    The backup file will be restored from /Users/husni/backup/searxng_data-backup.tar.gz, so please ensure that the file exists. This action will overwrite existing files within the Docker volume.

Disclaimer

This script is intended for experienced users. Ensure that you understand the implications of backing up and restoring Docker volumes in your environment before using this script.

#!/bin/bash
set -e
# Author: Husni Adil Makmur
# GitHub: https://github.com/husniadil
# This script performs backup and restore operations for Docker volumes within Proxmox LXC containers.
# Requirements:
# - Ensure that `sshpass` is installed. For installation instructions, visit: https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/
# - Root login with a password must be enabled on the Proxmox host. Follow these steps:
# 1. Edit the `/etc/ssh/sshd_config` file and apply the following configuration settings:
# - ListenAddress 0.0.0.0
# - PasswordAuthentication yes
# - PermitRootLogin yes
# 2. Restart the SSH service using the command: `systemctl restart sshd`
# Usage:
# - To backup a volume: `./proxmox.sh backup --host <host> --user <user> --lxc <lxc_number> --volume <volume> --path <path>`
# - To restore a volume: `./proxmox.sh restore --host <host> --user <user> --lxc <lxc_number> --volume <volume> --path <path>`
# - Parameters:
# - `host`: Proxmox host IP address or hostname
# - `user`: Proxmox host username
# - `lxc`: LXC container number
# - `volume`: Name of the Docker volume
# - `path`: Path for the backup location
# Example:
# - To backup: `./proxmox.sh backup --host 192.168.0.100 --user root --lxc 100 --volume searxng_data --path /Users/husni/backup`
# > The backup file will be saved to `/Users/husni/backup/searxng_data-backup.tar.gz`.
# - To restore: `./proxmox.sh restore --host 192.168.0.100 --user root --lxc 100 --volume searxng_data --path /Users/husni/backup`
# > The backup file will be restored from `/Users/husni/backup/searxng_data-backup.tar.gz`, so please ensure that the file exists. This action will overwrite existing files within the Docker volume.
# ----------------------------
# Script starts here
# ----------------------------
# Function to prompt for SSH password securely
prompt_password() {
read -sp "Enter SSH password: " SSH_PASS
echo
}
# Function to perform backup
backup() {
PROXMOX_HOST=$1
PROXMOX_USER=$2
LXC_NUMBER=$3
DOCKER_VOLUME=$4
BACKUP_PATH=$5
echo "Starting backup from Proxmox host: $PROXMOX_HOST, LXC: $LXC_NUMBER, Docker volume: $DOCKER_VOLUME"
prompt_password
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_PATH"
# Perform the backup
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "mkdir -p /backup"
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "pct exec $LXC_NUMBER -- docker run --rm -v $DOCKER_VOLUME:/mnt/data -v /backup:/backup alpine tar czf /backup/$DOCKER_VOLUME-backup.tar.gz -C /mnt/data ."
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "pct pull $LXC_NUMBER /backup/$DOCKER_VOLUME-backup.tar.gz /backup/$DOCKER_VOLUME-backup.tar.gz"
sshpass -p "$SSH_PASS" scp "$PROXMOX_USER@$PROXMOX_HOST:/backup/$DOCKER_VOLUME-backup.tar.gz" "$BACKUP_PATH/"
echo "Backup completed and saved to $BACKUP_PATH"
}
# Function to perform restore
restore() {
PROXMOX_HOST=$1
PROXMOX_USER=$2
LXC_NUMBER=$3
DOCKER_VOLUME=$4
BACKUP_PATH=$5
echo "Starting restore to Proxmox host: $PROXMOX_HOST, LXC: $LXC_NUMBER, Docker volume: $DOCKER_VOLUME"
prompt_password
# Perform the restore
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "mkdir -p /backup"
sshpass -p "$SSH_PASS" scp "$BACKUP_PATH/$DOCKER_VOLUME-backup.tar.gz" "$PROXMOX_USER@$PROXMOX_HOST:/backup/"
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "pct push $LXC_NUMBER /backup/$DOCKER_VOLUME-backup.tar.gz /backup/$DOCKER_VOLUME-backup.tar.gz"
sshpass -p "$SSH_PASS" ssh "$PROXMOX_USER@$PROXMOX_HOST" "pct exec $LXC_NUMBER -- docker run --rm -v $DOCKER_VOLUME:/mnt/data -v /backup:/backup alpine tar xzf /backup/$DOCKER_VOLUME-backup.tar.gz -C /mnt/data"
echo "Restore completed from $BACKUP_PATH"
}
# Main script logic
if [ $# -lt 1 ]; then
echo "Usage: $0 {backup|restore} --host <host> --user <user> --lxc <lxc_number> --volume <volume> --path <path>"
exit 1
fi
COMMAND=$1
shift
while [ $# -gt 0 ]; do
case "$1" in
--host)
PROXMOX_HOST=$2
shift 2
;;
--user)
PROXMOX_USER=$2
shift 2
;;
--lxc)
LXC_NUMBER=$2
shift 2
;;
--volume)
DOCKER_VOLUME=$2
shift 2
;;
--path)
BACKUP_PATH=$2
shift 2
;;
*)
echo "Invalid parameter $1"
exit 1
;;
esac
done
case "$COMMAND" in
backup)
backup "$PROXMOX_HOST" "$PROXMOX_USER" "$LXC_NUMBER" "$DOCKER_VOLUME" "$BACKUP_PATH"
;;
restore)
restore "$PROXMOX_HOST" "$PROXMOX_USER" "$LXC_NUMBER" "$DOCKER_VOLUME" "$BACKUP_PATH"
;;
*)
echo "Invalid command $COMMAND"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment