Backend/backup.ps1

46 lines
1.1 KiB
PowerShell
Raw Normal View History

param (
[string]$WorkDir = "./workdir",
[string]$BackupPath = "./backup",
[boolean]$OverWrite = $False
)
Write-Host "Démarrage de la sauvegarde..."
if (!(Test-Path -Path $WorkDir))
{
Write-Host "Le dossier de travail ($WorkDir) n'existe pas."
exit
}
if (!(Test-Path -Path $BackupPath))
{
Write-Host "Création du dossier des sauvegardes. ($BackupPath)"
New-Item -ItemType directory -Path $BackupPath | Out-Null
}
Write-Host "Début de la compression..."
$BackupFile = "$BackupPath/backup_$( Get-Date -f yyyyMMdd-HHmm ).zip"
$CompressArgs = @{
'Path' = $WorkDir
'DestinationPath' = $BackupFile
'Update' = $OverWrite
}
try
{
Compress-Archive @CompressArgs
Write-Host "Sauvegarde réussie! ($BackupFile)"
}
catch [System.IO.IOException]
{
Write-Output "Une erreur est survenue lors de la sauvegarde:"
if ($_.FullyQualifiedErrorId -like "ArchiveFileExists*")
{
Write-Host "L'archive de sauvegarde '$BackupFile' existe déjà. Ajoutez l'argument '-OverWrite `$true' pour écraser l'archive."
}
else
{
Write-Host $_
}
}