UNC path universal backup script

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Anthony Anthony 3 months, 2 weeks ago.

  • Author
    Posts
  • #50539
    Profile photo of Ben Ledermayer
    Ben Ledermayer
    Participant

    Dear All,

    I am trying to create a script in order to clean-up UNC path directories. I am working with hundreds of shares where the folder structure is identical so I would like to create a script to move older than 90 days items, to a backup folder to the root of the individual shares. My only problem that I can't exclude specific directories which content should not be moved ever (audit,reminder).

    Here is my script so far:

    $SourceDir = "\\SERVER\SHARE$\export\APP\%FOLDER%\" ### The %FOLDER% is not in the actual script, just put there to indicate.
    $DestinationDir = "\\SERVER\SHARE$\export\_BACKUP!\APP\"

    $files = get-childitem $SourceDir *.* -Recurse
    Foreach ($file in $files)
    {
    $Directory = $DestinationDir + "" + $file.CreationTime.Date.ToString('MM-yyyy')
    if (!(Test-Path $Directory))
    {
    New-Item $directory -type directory
    }
    If($file.LastWriteTime -lt (Get-Date).adddays(-90).date)
    {
    Move-Item $file.fullname $Directory -Exclude \\SERVER\SHARE$\export\APP\[*]\audit\*.*",\\SERVER\SHARE$\export\APP\[*]\reminder\*.*" -WhatIf
    }
    }

    I have one issue and one enhancement request.

    The issue that it does not matters if i put the -Exclude after the " $files = get-childitem $SourceDir *.* -Recurse " part or after Move-Item it does not works and moving the audit, etc. folders as well. I get that the issue is the part in the $SourceDir – %FOLDERS%. In every UNC path within the APP folder there are hundreds of subfolders, which i would like to wildcard, so exclude all "audit" folder within %FOLDERS%, and move everything else which is older than 90 days.

    To summ here is a tree view:

    APP
    ├───folder1
    │ ├───audit
    │ │ ├───folders
    │ │ ├───folders
    │ │ ├───folders
    ├───folder2
    │ ├───audit
    │ │ ├───folders
    │ │ ├───folders
    │ │ ├───folders
    ├───folder3
    │ ├───audit
    │ │ ├───folders
    │ │ ├───folders
    │ │ ├───folders

    So everything within the audit folders (and some else) should stay and everything not there and older than 90 days should moved to a backup folder.

    And here is the enhancement request. I tried to do it to create the exact same architecture as you see above within the _BACKUP!\APP folder, but I could not figure out how to. Currently it is creating folders based on Month and Year and moves everything there based on item creation date, however it would be really nice if I could maintain the same folder structure within the backup as well. I tried to pass the below command to "New-Item -Directory", but it is not really working out, creating the folder named of all folders with APP dir, not individual ones, and also does not know how to match up source and destination so make sure that Folder1 content will got to _BACKUP!\APP\Folder1\DateandTime\

    $Files = Get-ChildItem $SourceDir *.* -Directory |
    ForEach-Object
    {
    $Property = "\\10.31.1.50\marriott2_g_opera$\export\OPERA\$($_.Name)"
    ######## MISSING CODE ############
    }

    Tried to filter like this as well, which works, however cannot create directories by every entry, just one with all entry name:

    $Property = Get-ChildItem "\\SERVER\SHARE$\export\APP\*\" | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.Name}

    Off course this is just an enhancement which would be nice to have, but the main priority is to make the -Exclude parameter working.

    Thanks for the Community help in advance.

  • #50557
    Profile photo of Anthony
    Anthony
    Participant

    So assuming I am reading this correctly (It is 4 P.M my time so my brain has partly shutdown!) you basically don't want the audit folders to be returned in any of your results?

    I think you just need to filter it down a bit more granularly. I had the same issue as you doing a recurse on the top level folder. You can get around it by doing your get-childitem on the root of the sourcedir then doing a foreach on that similar to this

    $SourceDir = 'C:\Temp\App'
    $DestinationDir = 'C:\Temp\Moving'
    
    $items = get-childitem -LiteralPath $SourceDir
    foreach ($item in $items) {
    Get-ChildItem -Path $item.fullname -Exclude 'Audit'
    }

    You just need to have your recurse at a level below that of where the audit folder exists then you can use $Files.directory.name and $files.parent to build your folder structure to match on the UNC share. Your basically going to need multiple foreaches to achieve the replicated folder structure on the backup location to take in to account both the parent folder and the folder where the item actually sits.

You must be logged in to reply to this topic.