# ============================================================================== # SCRIPT DE SAUVEGARDE POWERSHELL - TÂCHE 1 (SAUVEGARDE COMPLÈTE - OPTIMISÉE) # ============================================================================== # Description : Sauvegarde complète du répertoire C:\DONNEES vers la destination # réseau \\TRUENAS\SAVE-client\ps1\Tache1. # Planification : Hebdomadaire, tous les samedis à 22h00. # ============================================================================== # ------------------------------------------------------------------------------ # CHAPITRE 1 : INITIALISATION ET MONTAGE DU LECTEUR RÉSEAU # ------------------------------------------------------------------------------ $Source = "C:\DONNEES" $PartageReseau = "\\TRUENAS\SAVE-client\ps1\Tache1" $LettreLecteur = "S" $DossierLog = "C:\Logs_Sauvegarde" $FichierLog = "$DossierLog\Tache1_Complete.log" Write-Host "--- CHAPITRE 1 : INITIALISATION ---" -ForegroundColor Cyan # Création du dossier de logs s'il n'existe pas if (-not (Test-Path -Path $DossierLog)) { New-Item -ItemType Directory -Path $DossierLog -Force | Out-Null } # Nettoyage préalable : si le lecteur S: existe déjà, on le déconnecte proprement if (Get-PSDrive -Name $LettreLecteur -ErrorAction SilentlyContinue) { Write-Host "Démonstration d'une ancienne session du lecteur ${LettreLecteur}:..." -ForegroundColor Yellow Remove-PSDrive -Name $LettreLecteur -Force -ErrorAction SilentlyContinue } # Montage du partage réseau sur la lettre S: Write-Host "Connexion au partage réseau $PartageReseau sur le lecteur ${LettreLecteur}:..." -ForegroundColor Cyan New-PSDrive -Name $LettreLecteur -PSProvider FileSystem -Root $PartageReseau -Persist -ErrorAction Stop | Out-Null $Destination = "${LettreLecteur}:\" # ------------------------------------------------------------------------------ # CHAPITRE 2 : TRAITEMENT DE LA SAUVEGARDE COMPLÈTE # ------------------------------------------------------------------------------ Write-Host "`n--- CHAPITRE 2 : EXECUTION DE LA SAUVEGARDE COMPLÈTE ---" -ForegroundColor Cyan Write-Host "Début de la sauvegarde : $Source --> $Destination" -ForegroundColor Green # Paramètres Robocopy optimisés (issus du rapport de recherche) : # /MIR : Miroir complet (copie intégrale et synchronisation) # /FFT : Tolérance de 2 secondes sur les horodatages (indispensable pour NAS TrueNAS/SAMBA) # /XJ : Exclusion des points de jonction (évite les boucles infinies) # /R:3 : 3 essais maximum en cas de fichier verrouillé # /W:5 : 5 secondes d'attente entre deux essais # /NP : Supprime le pourcentage de progression pour des logs clairs # /LOG+ : Journalisation cumulative des résultats robocopy $Source $Destination /MIR /FFT /XJ /R:3 /W:5 /NP /LOG+:$FichierLog Write-Host "Sauvegarde complète terminée avec succès. Consultez le log : $FichierLog" -ForegroundColor Green # ------------------------------------------------------------------------------ # CHAPITRE 3 : NETTOYAGE ET DÉCONNEXION DU LECTEUR RÉSEAU # ------------------------------------------------------------------------------ Write-Host "`n--- CHAPITRE 3 : NETTOYAGE ---" -ForegroundColor Cyan Write-Host "Déconnexion du lecteur réseau ${LettreLecteur}:..." -ForegroundColor Cyan # Suppression et déconnexion du lecteur S: Remove-PSDrive -Name $LettreLecteur -Force -ErrorAction Stop Write-Host "Le lecteur ${LettreLecteur}: a été démonté avec succès. Opération terminée." -ForegroundColor Green