Partage
  • Partager sur Facebook
  • Partager sur Twitter

Déplacement de la donnée avec Symlinks

Déplacement de la donnée avec critères et création lien symbolique

    1 mars 2020 à 10:40:44

    Bonjour

    Je viens de créer un script qui permet de faire un déplacement de la donnée répertoires/fichiers d'un serveur source à serveur destination avec comme condition uniquement les fichiers qui n'ont pas été modifié de plus X jours. Lorsque le fichier est déplacé à sa destination, il crée automatiquement un lien symbolique de ce fichier à la source. Le script doit aussi permettre de cloner les permissions source à destination afin que ceux-ci soient toujours identique. Si le répertoire source change de permissions celui ci doit doit-être adapté à la destination. 2 fichiers logs sont générés (logs erreur) et (logs de la donnée déplacée source et destination)

    Ce qui ne fonctionne pas :

    1) La création des liens symboliques fonctionne bien en générale, cependant certain fichiers avec une extension particulière (nom.docs *) fichier de type McAfee FRP encryption ne fonctionne pas.

    Question : Comment forcer la création des liens symboliques sur tous type de fichiers?

    2) Le clonage des permissions source/destination ne fonctionne pas toujours. Les permissions destination garde les anciennes permissions.

    Question : Comment écrasé les permissions destinations et cloner celles de la source continuellement?

    Merci d'avance pour votre aide (ci-dessous le script powershell)

    # INIT VARIABLES
    ###############################################################################################
    clear
     
    $DATE = Get-Date -Format "yyyy-MM-dd_HHmm_"
    $OUTPUT_CSV = "c:\temp\" + $DATE + "NameDataMove.csv"
    $CSV_CONTENT = @()
     
    $ERRORS_CSV = "c:\temp\" + $DATE + "NameDataMoveError.csv"
    $CSV_CONTENT_ERROR = @()
     
    # Be careful with the format of the text and the '\' !
    $SOURCE_FOLDER_PATH      = "\\serveur1\Share\" #folder not inherited
    $SOURCE_FOLDER_NAME      = "Share" # NO SLASH, ONLY THE NAME
    $DESTINATION_FOLDER_PATH = "\\serveur2\Share\"
    $DATE_REFERENCE = (Get-Date).AddDays(-545)
     
    # FUNCTIONS
    ###############################################################################################
    function Get-AclInheritanceIsEnable {
        param($Acl)
        $expandedAclAccess = $Acl | Select-Object -ExpandProperty Access
        $inheritanceEnabled = $false
     
        foreach ($access in $expandedAclAccess) {
            if ($access.IsInherited) {
                $inheritanceEnabled = $true
                break
            }
        }
        $inheritanceEnabled
    }
     
    # MAIN
    ###############################################################################################
     
    Write-Host("######################################################")
    Write-Host("CREATING FOLDERS STRUCTURE")
    Write-Host("------------------------------------------------------")
     
     
    # Creating MAIN folder with right ACL
    $check = Test-Path -Path ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME) -PathType Container
    if($check -eq $false) {
        New-Item -Path ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME) -ItemType Directory
    }
    $sourceACL = Get-Acl -Path $SOURCE_FOLDER_PATH
    $sourceACL | Set-Acl ($DESTINATION_FOLDER_PATH + $SOURCE_FOLDER_NAME)
     
     
    # Get all folders and sub-folders in MAIN folder
    $folders = Get-ChildItem $SOURCE_FOLDER_PATH -Recurse | Where-Object { $_.PSIsContainer -eq $true } 
     
    # Create directories structure
    foreach ($folder in $folders) {
        # Get the path of the folder to create but only the right part (we delete the left part of the path until the name of the source folder)
        $pos = $folder.FullName.IndexOf($SOURCE_FOLDER_NAME)
        $folderPathWithoutRoot = $folder.FullName.Substring($pos) + "\"
     
        # Create destination path by merging the destination folder with the tree of the folder to create
        $destination = Join-Path -Path $DESTINATION_FOLDER_PATH -ChildPath $folderPathWithoutRoot
     
        # Check if existing folder
        if(Test-Path -Path $destination -PathType Container) {
            # Nothing to do, already created
        } else {
            # Create the folder
            New-Item -Path $destination -ItemType Directory
            #$destination # display created folder
        }  
     
        # Clone ACL
        #Get-Acl -Path $folder.FullName | Set-Acl -Path $destination 
    }
     
    Write-Host("######################################################")
    Write-Host("MOVING OLD FILES")
    Write-Host("------------------------------------------------------")
     
     
    # Get all files and sub-files BUT NO .LNK FILES
    $items = Get-ChildItem $SOURCE_FOLDER_PATH -Recurse -Force | Where-Object { $_.PSIsContainer -eq $false -and $_.LastWriteTime -lt $DATE_REFERENCE -and $_.Extension -notlike ".symlink" -and $_.Extension -notlike ".lnk"  } 
     
    # Move each old modified file and create a shortcut
    foreach ($item in $items) {
        # Get the path of the file to move but only the right part (we delete the left part of the path until the name of the source folder)
        $pos = $item.FullName.IndexOf($SOURCE_FOLDER_NAME)
        $filePathWithoutRoot = $item.FullName.Substring($pos)
     
        # Create destination path by merging the destination folder with the tree of the file to move 
        $destination = Join-Path -Path $DESTINATION_FOLDER_PATH -ChildPath $filePathWithoutRoot
     
        # Move all file in the destination folder
        Move-Item -Path $item.FullName -Destination $destination
     
        # Create the link
        try { 
            New-Item -ItemType SymbolicLink -Name $item.Name -Path $item.PSParentPath -Target $destination -Force
            #$destination # display moved file     
        } catch {
            # Add error log to CSV
            $CSV_CONTENT_ERROR += [PSCustomObject]@{
            'filePath' = $item.FullName
            'messageError' = $_
            }
        }
     
     
        # Add log to CSV
        $CSV_CONTENT += [PSCustomObject]@{
        'Source' = $item.FullName
        'Destination' = $destination
        }
        #($item.FullName + ";" + $destination) >> $OUTPUT_CSV
     
    }
     
    # Create the CSV
    $CSV_CONTENT | Export-Csv $OUTPUT_CSV -Delimiter ";" -NoTypeInformation
    $CSV_CONTENT_ERROR | Export-Csv $ERRORS_CSV -Delimiter ";" -NoTypeInformation



    • Partager sur Facebook
    • Partager sur Twitter

    Déplacement de la donnée avec Symlinks

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown