# PBS Setup Completo - Referencia Detalhada

## Fase 1: PBS Installation

### 1.1 Verificar se PBS ja esta instalado
```bash
dpkg -l | grep proxmox-backup-server

# Se nao instalado:
apt update
apt install proxmox-backup-server
```

### 1.2 Aceder PBS Web UI
```
https://SERVER_IP:8007

User: root
Password: (root password do servidor)
```

### 1.3 Configuracao Inicial
- Hostname
- DNS servers
- Time zone (Europe/Lisbon)

## Fase 2: Datastore Creation

### 2.1 Preparar Storage

**Para ZFS (recomendado):**
```bash
# Ja criado em /proxmox-setup:
# zfs create rpool/pbs-datastore

# Verificar
zfs list | grep pbs-datastore

# Optimizar para backup workload
zfs set compression=lz4 rpool/pbs-datastore
zfs set dedup=off rpool/pbs-datastore  # Dedup no PBS, nao no ZFS
zfs set recordsize=1M rpool/pbs-datastore  # Large files
```

**Para ext4 (HDD 16TB):**
```bash
# Particionar HDD
parted /dev/sda mklabel gpt
parted /dev/sda mkpart primary ext4 0% 100%

# Formatar
mkfs.ext4 /dev/sda1

# Montar
mkdir -p /mnt/pbs-datastore
echo "/dev/sda1 /mnt/pbs-datastore ext4 defaults 0 2" >> /etc/fstab
mount -a
```

### 2.2 Criar Datastore via CLI
```bash
proxmox-backup-manager datastore create main-store /mnt/pbs-datastore

# Verificar
proxmox-backup-manager datastore list
```

### 2.3 Configurar Retention Policy
```bash
# 7 daily, 4 weekly, 6 monthly (padrao)
proxmox-backup-manager datastore update main-store \
  --keep-daily 7 \
  --keep-weekly 4 \
  --keep-monthly 6 \
  --keep-yearly 3
```

**Explicacao Retention:**
- `keep-daily 7`: Mantem 7 backups diarios
- `keep-weekly 4`: Mantem 4 backups semanais (1 por semana)
- `keep-monthly 6`: Mantem 6 backups mensais (1 por mes)
- `keep-yearly 3`: Mantem 3 backups anuais

**Gestao automatica:** PBS elimina backups antigos baseado nestas regras.

## Fase 3: PBS Users e Permissions

### 3.1 Criar User para PVE Backups
```bash
# User dedicado para Proxmox enviar backups
proxmox-backup-manager user create pve-backup@pbs \
  --email admin@descomplicar.pt

# Password
proxmox-backup-manager user update pve-backup@pbs --password

# Atribuir permissoes no datastore
proxmox-backup-manager acl update /datastore/main-store \
  --auth-id pve-backup@pbs \
  --role DatastoreBackup
```

### 3.2 Criar API Token (para automacao)
```bash
# Token para scripts/Terraform
proxmox-backup-manager user token create pve-backup@pbs automation-token \
  --output-format json

# Guardar token de forma segura
# Formato: pve-backup@pbs!automation-token=<secret>
```

## Fase 4: Configure PVE to Use PBS

### 4.1 Adicionar PBS Storage em Proxmox VE

Via Web UI (Datacenter -> Storage -> Add -> Proxmox Backup Server):
- ID: `pbs-main`
- Server: `SERVER_IP` (ou hostname se cluster)
- Datastore: `main-store`
- Username: `pve-backup@pbs`
- Password: (password criado)
- Fingerprint: (auto-detect)

Via CLI:
```bash
pvesm add pbs pbs-main \
  --server SERVER_IP \
  --datastore main-store \
  --username pve-backup@pbs \
  --password <password>
```

### 4.2 Verificar Conectividade
```bash
pvesm status | grep pbs-main
```

## Fase 5: Backup Jobs (PVE)

### 5.1 Criar Backup Job para VMs Criticas

Via Web UI (Datacenter -> Backup -> Add):
- Storage: `pbs-main`
- Schedule: Daily 01:00
- Mode: Snapshot (live backup)
- Compression: zstd
- Notification: email admin@descomplicar.pt

Via CLI:
```bash
# Backup diario de todas VMs as 01:00
vzdump --storage pbs-main --mode snapshot --compress zstd --all 1
```

### 5.2 Agendar via cron (alternativa)
```bash
# /etc/cron.d/pve-backup-critical
0 1 * * * root vzdump --storage pbs-main --vmid 100,101,102 --mode snapshot --compress zstd
```

### 5.3 Backup Selectivo
```bash
# VMs criticas: diario
# VMs secundarias: 3x semana (Seg, Qua, Sex)
0 1 * * 1,3,5 root vzdump --storage pbs-main --vmid 200,201,202 --mode snapshot --compress zstd
```

## Fase 6: Remote Sync (2-Node Cluster)

**Setup para cluster:** PBS em Node B (primary) + PBS em Node A (secondary)

### 6.1 Configurar Remote em PBS Secondary (Node A)

Via Web UI PBS Node A (Configuration -> Remote):
- Name: `pbs-node-b`
- Host: `<node-b-ip>` ou `cluster.descomplicar.pt`
- Port: 8007
- Auth ID: `pve-backup@pbs`
- Password: (password)
- Fingerprint: (auto-detect)

### 6.2 Criar Sync Job

Via Web UI PBS Node A (Configuration -> Sync Jobs -> Add):
- Remote: `pbs-node-b`
- Remote Datastore: `main-store`
- Local Datastore: `secondary-store`
- Schedule: Daily 03:00 (apos backups)
- Remove vanished: Yes (sync deletes)

Via CLI em Node A:
```bash
proxmox-backup-manager sync-job create sync-from-node-b \
  --remote pbs-node-b \
  --remote-store main-store \
  --store secondary-store \
  --schedule "0 3 * * *" \
  --remove-vanished true
```

### 6.3 Testar Sync Manual
```bash
proxmox-backup-manager sync-job run sync-from-node-b
```

## Fase 7: Restore Procedures (Testing)

### 7.1 Restore VM Teste

Via Web UI PVE:
- Storage -> pbs-main -> Backups
- Seleccionar VM backup
- Restore -> New VM ID (999)
- Start apos restore

### 7.2 Restore via CLI
```bash
# Listar backups disponiveis
pbs-client list --repository pve-backup@pbs@SERVER_IP:main-store

# Restore VM 100
qmrestore pbs-main:backup/vm/100/YYYY-MM-DD... 999
```

### 7.3 Validar Restore
```bash
qm start 999
# Verificar VM boota correctamente
# Teste servico critical
# Shutdown e remover VM teste
qm stop 999 && qm destroy 999
```

**Critico:** Testar restore ANTES de considerar backup strategy operacional.
