From 3505c636dad4b20f070661f9428cd509c13a5e23 Mon Sep 17 00:00:00 2001 From: hhf Date: Mon, 30 Sep 2024 09:14:54 +0530 Subject: [PATCH] Add proxmox.sh --- proxmox.sh | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 proxmox.sh diff --git a/proxmox.sh b/proxmox.sh new file mode 100644 index 0000000..2316a58 --- /dev/null +++ b/proxmox.sh @@ -0,0 +1,127 @@ +#!/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 --user --lxc --volume --path ` +# - To restore a volume: `./proxmox.sh restore --host --user --lxc --volume --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 --user --lxc --volume --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 \ No newline at end of file