Windows Vista Forums

Powershell and sync data
  1. #1


    Cyber-Guy Guest

    Powershell and sync data

    Greetings,
    Is there anyone that has tried to use powershell as a simple sync of data
    for backup?
    For example from PC to and external HDD



    many thanks



      My System SpecsSystem Spec

  2. #2


    Brandon Shell Guest

    Re: Powershell and sync data

    This should be pretty simple. What is your goal?

    Master/Slave?
    Master/Master?
    Conflict resolution?

    "Cyber-Guy" <cyber-guy@xxxxxx> wrote in message
    news:%23oC5sf%236HHA.4348@xxxxxx

    > Greetings,
    > Is there anyone that has tried to use powershell as a simple sync of data
    > for backup?
    > For example from PC to and external HDD
    >
    > many thanks
    >

      My System SpecsSystem Spec

  3. #3


    Hal Rottenberg Guest

    Re: Powershell and sync data

    Brandon Shell wrote:

    > This should be pretty simple. What is your goal?
    >
    > Master/Slave?
    > Master/Master?
    > Conflict resolution?
    Yes, yes, and yes.

    I just tried using foldershare.com the other day to sync my PS $profiledir and
    some other things for work<->home syncing. And I've been using SyncToy from MS
    for some local<->\\network syncing as well. Those work fine, but it's not
    Powershell. And if it's not Powershell, it's CRAP! [read in a Scottish accent.]



    --

    Hal Rottenberg
    blog: http://halr9000.com
    powershell category:
    http://halr9000.com/article/category...ng/powershell/

      My System SpecsSystem Spec

  4. #4


    Brandon Shell Guest

    Re: Powershell and sync data

    Hmmm... lemme see what I can put together.
    Master/Slave is a no brainer
    Master/Master we can use file Hash and date

    "Hal Rottenberg" <hal@xxxxxx> wrote in message
    news:%23eFlbN$6HHA.5184@xxxxxx

    > Brandon Shell wrote:

    >> This should be pretty simple. What is your goal?
    >>
    >> Master/Slave?
    >> Master/Master?
    >> Conflict resolution?
    >
    > Yes, yes, and yes.
    >
    > I just tried using foldershare.com the other day to sync my PS $profiledir
    > and some other things for work<->home syncing. And I've been using
    > SyncToy from MS for some local<->\\network syncing as well. Those work
    > fine, but it's not Powershell. And if it's not Powershell, it's CRAP!
    > [read in a Scottish accent.]
    >
    >
    >
    > --
    >
    > Hal Rottenberg
    > blog: http://halr9000.com
    > powershell category:
    > http://halr9000.com/article/category...ng/powershell/

      My System SpecsSystem Spec

  5. #5


    Brandon Shell Guest

    Re: Powershell and sync data

    Any thoughts on the best way to compare the source and destination? Anyone?
    I have a function to get a MD5 of the files, but I'm just wonder what others
    think is a good idea for the compare.

    "Brandon Shell" <tshell.mask@xxxxxx> wrote in message
    news:OqxxF6$6HHA.536@xxxxxx

    > Hmmm... lemme see what I can put together.
    > Master/Slave is a no brainer
    > Master/Master we can use file Hash and date
    >
    > "Hal Rottenberg" <hal@xxxxxx> wrote in message
    > news:%23eFlbN$6HHA.5184@xxxxxx

    >> Brandon Shell wrote:

    >>> This should be pretty simple. What is your goal?
    >>>
    >>> Master/Slave?
    >>> Master/Master?
    >>> Conflict resolution?
    >>
    >> Yes, yes, and yes.
    >>
    >> I just tried using foldershare.com the other day to sync my PS
    >> $profiledir and some other things for work<->home syncing. And I've been
    >> using SyncToy from MS for some local<->\\network syncing as well. Those
    >> work fine, but it's not Powershell. And if it's not Powershell, it's
    >> CRAP! [read in a Scottish accent.]
    >>
    >>
    >>
    >> --
    >>
    >> Hal Rottenberg
    >> blog: http://halr9000.com
    >> powershell category:
    >> http://halr9000.com/article/category...ng/powershell/
    >

      My System SpecsSystem Spec

  6. #6


    Brandon Shell Guest

    Re: Powershell and sync data

    Here is what I got... recommendations for performance are welcome.

    Basically it gets all the files/folders from both the Source and
    Destination.
    - If first checks for folders and creates them if they are missing.
    - It Checks the files from the Source in the Destination
    1) If they exists it copies which ever is newer.
    2) If they are missing it copies them to Destination
    - It Checks the files from the Destination in the Source
    1) If they are missing it copies them to Destination

    ###################################################
    ###################################################
    Param($Source,$Destination)
    #$Source = "C:\temp\Source"
    #$Destination = "C:\temp\Destination"
    function Get-FileMD5 {
    Param([string]$file)
    $mode = [System.IO.FileMode]("open")
    $access = [System.IO.FileAccess]("Read")
    $md5 = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
    $fs = New-Object System.IO.FileStream($file,$mode,$access)
    $Hash = $md5.ComputeHash($fs)
    $fs.Close()
    [string]$Hash = $Hash
    Return $Hash
    }
    function Copy-LatestFile{
    Param($File1,$File2,[switch]$whatif)
    $File1Date = get-Item $File1 | foreach-Object{$_.LastWriteTimeUTC}
    $File2Date = get-Item $File2 | foreach-Object{$_.LastWriteTimeUTC}
    if($File1Date -gt $File2Date)
    {
    Write-Host "$File1 is Newer... Copying..."
    if($whatif){Copy-Item -path $File1 -dest $File2 -force -whatif}
    else{Copy-Item -path $File1 -dest $File2 -force}
    }
    else
    {
    Write-Host "$File2 is Newer... Copying..."
    if($whatif){Copy-Item -path $File2 -dest $File1 -force -whatif}
    else{Copy-Item -path $File2 -dest $File1 -force}
    }
    Write-Host
    }

    # Getting Files/Folders from Source and Destination
    $SrcEntries = Get-ChildItem $Source -Recurse
    $DesEntries = Get-ChildItem $Destination -Recurse

    # Parsing the folders and Files from Collections
    $Srcfolders = $SrcEntries | Where-Object{$_.PSIsContainer}
    $SrcFiles = $SrcEntries | Where-Object{!$_.PSIsContainer}
    $Desfolders = $DesEntries | Where-Object{$_.PSIsContainer}
    $DesFiles = $DesEntries | Where-Object{!$_.PSIsContainer}

    # Checking for Folders that are in Source, but not in Destination
    foreach($folder in $Srcfolders)
    {
    $SrcFolderPath = $source -replace "\\","\\" -replace "\:","\:"
    $DesFolder = $folder.Fullname -replace $SrcFolderPath,$Destination
    if(!(test-path $DesFolder))
    {
    Write-Host "Folder $DesFolder Missing. Creating it!"
    new-Item $DesFolder -type Directory | out-Null
    }
    }

    # Checking for Folders that are in Destinatino, but not in Source
    foreach($folder in $Desfolders)
    {
    $DesFilePath = $Destination -replace "\\","\\" -replace "\:","\:"
    $SrcFolder = $folder.Fullname -replace $DesFilePath,$Source
    if(!(test-path $SrcFolder))
    {
    Write-Host "Folder $SrcFolder Missing. Creating it!"
    new-Item $SrcFolder -type Directory | out-Null
    }
    }

    # Checking for Files that are in the Source, but not in Destination
    foreach($entry in $SrcFiles)
    {
    $SrcFullname = $entry.fullname
    $SrcName = $entry.Name
    $SrcFilePath = $Source -replace "\\","\\" -replace "\:","\:"
    $DesFile = $SrcFullname -replace $SrcFilePath,$Destination
    if(test-Path $Desfile)
    {
    $SrcMD5 = Get-FileMD5 $SrcFullname
    $DesMD5 = Get-FileMD5 $DesFile
    If(Compare-Object $srcMD5 $desMD5)
    {
    Write-Host "The Files MD5's are Different... Checking Write
    Dates"
    Write-Host $SrcMD5
    Write-Host $DesMD5
    Copy-LatestFile $SrcFullname $DesFile
    }
    }
    else
    {
    Write-Host "$Desfile Missing... Copying from $SrcFullname"
    copy-Item -path $SrcFullName -dest $DesFile -force
    }
    }

    # Checking for Files that are in the Destinatino, but not in Source
    foreach($entry in $DesFiles)
    {
    $DesFullname = $entry.fullname
    $DesName = $entry.Name
    $DesFilePath = $Destination -replace "\\","\\" -replace "\:","\:"
    $SrcFile = $DesFullname -replace $DesFilePath,$Source
    if(!(test-Path $SrcFile))
    {
    Write-Host "$SrcFile Missing... Copying from $DesFullname"
    copy-Item -path $DesFullname -dest $SrcFile -force
    }
    }



    "Brandon Shell" <tshell.mask@xxxxxx> wrote in message
    news:uC8XtFR7HHA.4880@xxxxxx

    > Any thoughts on the best way to compare the source and destination?
    > Anyone?
    > I have a function to get a MD5 of the files, but I'm just wonder what
    > others think is a good idea for the compare.
    >
    > "Brandon Shell" <tshell.mask@xxxxxx> wrote in message
    > news:OqxxF6$6HHA.536@xxxxxx

    >> Hmmm... lemme see what I can put together.
    >> Master/Slave is a no brainer
    >> Master/Master we can use file Hash and date
    >>
    >> "Hal Rottenberg" <hal@xxxxxx> wrote in message
    >> news:%23eFlbN$6HHA.5184@xxxxxx

    >>> Brandon Shell wrote:
    >>>> This should be pretty simple. What is your goal?
    >>>>
    >>>> Master/Slave?
    >>>> Master/Master?
    >>>> Conflict resolution?
    >>>
    >>> Yes, yes, and yes.
    >>>
    >>> I just tried using foldershare.com the other day to sync my PS
    >>> $profiledir and some other things for work<->home syncing. And I've
    >>> been using SyncToy from MS for some local<->\\network syncing as well.
    >>> Those work fine, but it's not Powershell. And if it's not Powershell,
    >>> it's CRAP! [read in a Scottish accent.]
    >>>
    >>>
    >>>
    >>> --
    >>>
    >>> Hal Rottenberg
    >>> blog: http://halr9000.com
    >>> powershell category:
    >>> http://halr9000.com/article/category...ng/powershell/
    >>
    >

      My System SpecsSystem Spec

  7. #7


    Flowering Weeds Guest

    Re: Powershell and sync data


    "Brandon Shell"

    > Any thoughts

    > I have a function to get a MD5 of the files,

    Habit #6: Don't Write Insecure Code
    http://msdn.microsoft.com/msdnmag/is...cureHabits/#S6





      My System SpecsSystem Spec

  8. #8


    Hal Rottenberg Guest

    Re: Powershell and sync data

    Flowering Weeds wrote:

    >> I have a function to get a MD5 of the files,
    >
    > Habit #6: Don't Write Insecure Code
    > http://msdn.microsoft.com/msdnmag/is...cureHabits/#S6
    The article is talking about using MD5 for encryption, isn't it? As far as I
    can tell, this has no impact on security to use MD5 to compare files.

    --

    Hal Rottenberg
    blog: http://halr9000.com
    powershell category:
    http://halr9000.com/article/category...ng/powershell/

      My System SpecsSystem Spec

  9. #9


    Brandon Shell Guest

    Re: Powershell and sync data

    How is this insecure?

    "Flowering Weeds" <floweringnoweedsno@xxxxxx> wrote in message
    news:eQ3iMxZ7HHA.4712@xxxxxx

    >
    > "Brandon Shell"
    >

    >> Any thoughts
    >

    >> I have a function to get a MD5 of the files,
    >
    >
    > Habit #6: Don't Write Insecure Code
    > http://msdn.microsoft.com/msdnmag/is...cureHabits/#S6
    >
    >
    >
    >

      My System SpecsSystem Spec

  10. #10


    Flowering Weeds Guest

    Re: Powershell and sync data


    "Hal Rottenberg"

    >
    > The article is talking about using
    > MD5 for encryption, isn't it? As
    > far as I can tell, this has no impact
    > on security to use MD5 to compare files.
    >
    System.Security.Cryptography.HashAlgorithm Class

    "Represents the base class from which all
    implementations of cryptographic hash
    algorithms must derive."

    So to most, concerned with security,
    "with regard to cryptography" seems
    to apply to "compare files" too.







      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Powershell and sync data problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Flash drive and sync center with data files JAM Vista hardware & devices 0 31 Mar 2008
how do i get locales in sync between powershell and dotnet Harald Ums \(2179630\) PowerShell 4 22 Jun 2007
PowerShell hangs while getting data from Web Service. Please Help. Raj Tripathi PowerShell 3 05 Apr 2007
Can PowerShell help - Data Error (CRC) ? PowerShell 0 06 Aug 2006
PowerShell configuration data Greg Borota PowerShell 5 12 Jul 2006