# Odoo Database Backup Script
# This script dumps the Postgres database from the Docker container

# 1. Create backup directory if it doesn't exist
$BackupDir = "d:\Odoo\backups"
if (!(Test-Path -Path $BackupDir)) {
    New-Item -ItemType Directory -Path $BackupDir
}

# 2. Define Backup Filename with Timestamp
$Date = Get-Date -f 'yyyy-MM-dd_HHmm'
$Filename = "db_backup_$Date.sql"
$FullPaths = Join-Path -Path $BackupDir -ChildPath $Filename

Write-Host "Starting backup to $FullPaths..." -ForegroundColor Cyan

# 3. Execute pg_dump inside the container
# We use --clean to include 'DROP TABLE' statements for easier restoration
docker exec odoo_client1_db pg_dump -U odoo -d postgres --clean > $FullPaths

if ($LASTEXITCODE -eq 0) {
    Write-Host "✅ Backup successfully created: $Filename" -ForegroundColor Green
} else {
    Write-Host "❌ Backup failed! Please ensure docker-compose is running." -ForegroundColor Red
}
