#!/bin/bash
# PBS setup script for OpenStack VM (Ubuntu 24.04)
# Run on the VM via: sudo bash /tmp/pbs-setup.sh
# Usage: PBS_ADMIN_PASSWORD="yourpass" sudo -E bash /tmp/pbs-setup.sh
#
# Prerequisites: /dev/sdb is the data volume (attached Cinder volume)
set -e

echo "=== Format data volume ==="
mke2fs -t ext4 /dev/sdb

echo "=== Mount ==="
mkdir -p /mnt/datastore/offsite
mount /dev/sdb /mnt/datastore/offsite
echo "/dev/sdb /mnt/datastore/offsite ext4 defaults,noatime 0 2" >> /etc/fstab
df -h /mnt/datastore/offsite

echo "=== Add Proxmox repo ==="
echo "deb http://download.proxmox.com/debian/pbs bookworm pbstest" > /etc/apt/sources.list.d/pbs.list
wget -q http://download.proxmox.com/debian/proxmox-release-bookworm.gpg -O /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg
apt-get update -qq 2>&1 | tail -5

echo "=== Install PBS ==="
DEBIAN_FRONTEND=noninteractive apt-get install -y proxmox-backup-server 2>&1 | tail -20

echo "=== Create datastore ==="
proxmox-backup-manager datastore create offsite /mnt/datastore/offsite

echo "=== Create admin user ==="
PBS_ADMIN_PASSWORD="${PBS_ADMIN_PASSWORD:-changeme}"
proxmox-backup-manager user create admin@pbs --password "$PBS_ADMIN_PASSWORD"

echo "=== Grant admin ACL ==="
proxmox-backup-manager acl update / Admin --auth-id admin@pbs

echo "=== Show fingerprint ==="
openssl x509 -in /etc/proxmox-backup/proxy.pem -noout -fingerprint -sha256

echo "=== Done ==="
