summaryrefslogtreecommitdiff
path: root/sync_windows_to_minio.ps1
blob: 43e087108023477b67f73618748a3e95d8ae64f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
param(
    [Parameter(Mandatory = $true)]
    [string]$SourceRoot,

    [Parameter(Mandatory = $true)]
    [string]$EndpointUrl,

    [Parameter(Mandatory = $true)]
    [string]$Bucket,

    [Parameter(Mandatory = $true)]
    [string]$AccessKey,

    [Parameter(Mandatory = $true)]
    [string]$SecretKey,

    [string]$ProfileName = "minio-sync",
    [string]$PrefixRoot = "photos",
    [switch]$WhatIf
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

if (-not (Test-Path -Path $SourceRoot)) {
    throw "Le chemin source '$SourceRoot' est introuvable."
}

$aws = Get-Command aws -ErrorAction SilentlyContinue
if (-not $aws) {
    throw "AWS CLI est requis. Installer AWS CLI puis relancer."
}

Write-Host "Configuration du profil AWS CLI '$ProfileName'..."
aws configure set aws_access_key_id $AccessKey --profile $ProfileName | Out-Null
aws configure set aws_secret_access_key $SecretKey --profile $ProfileName | Out-Null
aws configure set default.region us-east-1 --profile $ProfileName | Out-Null

$normalizedSource = (Resolve-Path $SourceRoot).Path
$folders = Get-ChildItem -Path $normalizedSource -Directory

if ($folders.Count -eq 0) {
    Write-Warning "Aucun sous-dossier detecte. Synchronisation du dossier source complet."
    $target = "s3://$Bucket/$PrefixRoot/"
    $cmd = "aws s3 sync `"$normalizedSource`" `"$target`" --endpoint-url `"$EndpointUrl`" --profile `"$ProfileName`" --delete"
    if ($WhatIf) {
        Write-Host "[WhatIf] $cmd"
    } else {
        Invoke-Expression $cmd
    }
    exit 0
}

foreach ($folder in $folders) {
    $logicalPrefix = "$PrefixRoot/$($folder.Name)/"
    $target = "s3://$Bucket/$logicalPrefix"
    $cmd = "aws s3 sync `"$($folder.FullName)`" `"$target`" --endpoint-url `"$EndpointUrl`" --profile `"$ProfileName`" --delete"
    if ($WhatIf) {
        Write-Host "[WhatIf] $cmd"
    } else {
        Write-Host "Sync '$($folder.FullName)' -> '$target'"
        Invoke-Expression $cmd
    }
}

Write-Host "Synchronisation terminee."