Help with last modified backup

Welcome Forums General PowerShell Q&A Help with last modified backup

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #108524

    Participant
    Points: 0
    Rank: Member

    This is my first post, so please go easy on me....  🙂

    I was a network admin back in the Windows NT4 days (and had my MSCE) but transitioned into business and leadership roles (in technical capacities). While I have remained technical during this time, I have not had to do any large scale system administration and never learned things like powershell. I am getting back to my roots and recently started doing some network system administration again, so have been diving into powershell.

    I have a server with a number of files I need to move from local storage to a backup SAN when they reach a last written time of 1 year. Both source and destination have drive letters mapped to the server. The source folder has both files and folders (these folders also may have files as well as their own sub-folders).

    I wrote a one line powershell command that technically works, but not how I want it to. It is indeed copying the correct files, but its not creating the directory structure and its not moving folders (based on their last modified date). Here is my script:

    $SourceFolder = "C:\AudioRecordings"
    $DestinationFolder = "C:\AudioRecordingsBackup"

    get-childitem -path $SourceFolder -Recurse | where {$_.LastWriteTime -lt (Get-Date).AddDays(-365)} | % { move-item $_.FullName -Destination $DestinationFolder -Force}

    I created a test directory structure with files and folders with manipulated timestamps:

    Volume in drive C has no label.
    Volume Serial Number is 20D7-02AF

    Directory of C:\AudioRecordings

    08/14/2018  01:17 PM              .
    08/14/2018  01:17 PM              ..
    01/01/2016  09:30 AM                 0 1-1-2016.txt
    01/01/2017  09:30 AM                 0 1-1-2017.txt
    01/01/2018  09:30 AM                 0 1-1-2018.txt
    08/14/2018  01:17 PM              6-10-2016
    08/14/2018  01:17 PM              6-11-2017
    08/14/2018  01:17 PM              7-14-2018
    08/14/2018  01:17 PM              8-14-2018
    3 File(s)              0 bytes

    Directory of C:\AudioRecordings\6-10-2016

    08/14/2018  01:17 PM              .
    08/14/2018  01:17 PM              ..
    06/10/2016  10:58 AM                 0 6-10-2016.txt
    1 File(s)              0 bytes

    Directory of C:\AudioRecordings\6-11-2017

    08/14/2018  01:17 PM              .
    08/14/2018  01:17 PM              ..
    06/11/2017  10:59 AM                 0 6-11-2017.txt
    1 File(s)              0 bytes

    Directory of C:\AudioRecordings\7-14-2018

    08/14/2018  01:17 PM              .
    08/14/2018  01:17 PM              ..
    07/14/2018  10:59 AM                 0 7-14-2018.txt
    1 File(s)              0 bytes

    Directory of C:\AudioRecordings\8-14-2018

    08/14/2018  01:17 PM              .
    08/14/2018  01:17 PM              ..
    08/14/2018  10:59 AM                 0 8-14-2018.txt
    1 File(s)              0 bytes

    Total Files Listed:
    7 File(s)              0 bytes
    14 Dir(s)  146,092,548,096 bytes free

    And here is the destination folder contents:

    Volume in drive C has no label.
    Volume Serial Number is 20D7-02AF

    Directory of C:\AudioRecordingsBackup

    08/14/2018  02:02 PM              .
    08/14/2018  02:02 PM              ..
    01/01/2016  09:30 AM                 0 1-1-2016.txt
    01/01/2017  09:30 AM                 0 1-1-2017.txt
    06/10/2016  10:58 AM                 0 6-10-2016.txt
    06/11/2017  10:59 AM                 0 6-11-2017.txt
    4 File(s)              0 bytes

    Total Files Listed:
    4 File(s)              0 bytes
    2 Dir(s)  146,092,052,480 bytes free

    I know its something easy... Thanks in advance for your help!

  • #108538

    Participant
    Points: 7
    Rank: Member

    Try using the copy-item cmdlet instead of move-item.  Something along these lines.

    
    get-childitem -path $SourceFolder -Recurse | where {$_.LastWriteTime -lt (Get-Date).AddDays(-365)} |
    foreach { copy-item $_.FullName -Destination $DestinationFolder -Recurse -Container -Force}
    
    
  • #108541

    Participant
    Points: 43
    Rank: Member

    Hi im3cpo,

    I found this post a while back when I had to do something similar, you should be able to adapt it to your needs.

    https://mcpmag.com/articles/2015/10/22/maintain-folder-structure.aspx

    It explains about how to copy with the folder structure, you could add your date filter before the foreach to filter just the files you want.

    • #108550

      Participant
      Points: 0
      Rank: Member

      Thanks for the help. Ironically, I kept plugging away and came up with something that works!

      Get-ChildItem -Path $SourceFolder -Recurse | where {$_.LastWriteTime -lt (Get-Date).AddDays(-365)} | Move-Item -Destination $DestinationFolder -Force

      Feel free to make any suggestions on improvements and I will also check out the info others have provided.

      Thanks everyone!

The topic ‘Help with last modified backup’ is closed to new replies.