# Proxmox LXC Docker Backup and Restore

**URL:** <https://forum.hhf.technology/t/proxmox-lxc-docker-backup-and-restore/173>\
**Category:** Tutorials\
**Tags:** proxmox, backup, lxc-containers\
**Created:** [October 11, 2024, 6:07am UTC](https://forum.hhf.technology/t/proxmox-lxc-docker-backup-and-restore/173 "2024-10-11T06:07:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhf.technoloy](https://forum.hhf.technology/user_avatar/forum.hhf.technology/hhf.technoloy/32/68_2.png) [@hhf.technoloy](https://forum.hhf.technology/u/hhf.technoloy)\
**Post date:** [October 11, 2024, 6:07am UTC](https://forum.hhf.technology/t/proxmox-lxc-docker-backup-and-restore/173/1 "2024-10-11T06:07:37Z")

</div>

# Proxmox Docker Volume Backup and Restore Script

This script facilitates the backup and restoration of Docker volumes within Proxmox LXC containers.

## Requirements

- Ensure `sshpass` is installed. For installation guidance, refer to [Cyberciti: SSH Password Provider](https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/).
- Enable root login with a password on the Proxmox host by following these steps:
  1. Modify the `/etc/ssh/sshd_config` file with the following settings:
    - `ListenAddress 0.0.0.0`
    - `PasswordAuthentication yes`
    - `PermitRootLogin yes`

  2. Restart the SSH service using:

```bash
systemctl restart sshd

```

## Usage

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

- **Backup a volume:**

- **Restore a volume:**

### Parameters

- `host`: IP address or hostname of the Proxmox host
- `user`: Username for the Proxmox host
- `lxc`: LXC container number
- `volume`: Name of the Docker volume
- `path`: Backup location path

## Examples

- **Backing up a volume:**

- **Restoring a volume:**

## Disclaimer

This script is designed for advanced users. Ensure you fully understand the consequences of backing up and restoring Docker volumes in your environment before proceeding with its use.

Ref:  
[1] [How can I backup a Docker-container with its data-volumes? - Stack Overflow](https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes)  
[2] [Question about backing up docker and restoring tests? - General - Docker Community Forums](https://forums.docker.com/t/question-about-backing-up-docker-and-restoring-tests/126743)  
[3] [CT: how to backup only root volume but... | Proxmox Support Forum](https://forum.proxmox.com/threads/ct-how-to-backup-only-root-volume-but.124063/)  
[4] [Restore for backup, bare minimum - General - Docker Community Forums](https://forums.docker.com/t/restore-for-backup-bare-minimum/128772)  
[5] [Restoring using proxmox-backup-client within a docker image | Proxmox Support Forum](https://forum.proxmox.com/threads/restoring-using-proxmox-backup-client-within-a-docker-image.104802/)  
[6] [Nginx Proxy Manager Restore Backup from Docker · tteck/Proxmox · Discussion #2451 · GitHub](https://github.com/tteck/Proxmox/discussions/2451)  
[7] [Reddit - The heart of the internet](https://www.reddit.com/r/selfhosted/comments/uhidun/docker_backup_migration_to_another_server_proxmox/)  
[8] [Best practize full backup and restore ( image + volumes + config ) - General - Docker Community Forums](https://forums.docker.com/t/best-practize-full-backup-and-restore-image-volumes-config/142331)

```bash
#!/bin/bash
set -e
#!/bin/bash
# =========================================================================== #
# Description: Proxmox LXC Docker Backup and Restore.
# Details: Proxmox LXC Docker Backup and Restore.
# Made for: Linux, Proxmox (Debian).
# Requirements: ssh-keygen - ssh-copy-id root@127.0.0.1 (replace IP)
# Version: 0.1
# Make executable: chmod +x fproxmox.sh
# =========================================================================== #

# 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

```
