Home > Software engineering >  backup www directory from local to network drive powershell
backup www directory from local to network drive powershell

Time:12-03

I built a script that copies files from the local C drive to a Network Drive. I have a folder in C:\www\root called "images" or "C:\www\root\images" The script creates a folder called "images" in the "F:\powershell_wwwroot\root_" $TimeStamp" however the "images" directory is empty in the destination folder. The script copies everything from the C:\www\root\images\ folder to base destination folder "F:\powershell_wwwroot\root_" $TimeStamp".

So I see everything from C:\www\root\images\ in F:\powershell_wwwroot\root_02_12_2021 however the F:\powershell_wwwroot\root_02_12_2021\images is empty. I would like to see everytying from "C:\www\root\images" in "F:\powershell_wwwroot \root_02_12_2021\images"

$TimeStamp = get-date -f dd_MM_yyyy
$Destination = "F:\powershell_wwwroot\root_"   $TimeStamp
New-Item -ItemType directory -Path $Destination -Force

gci "C:\www\root\" -recurse | %{ 

    $file = $_.Fullname
    Write-Host $file
    Copy-Item -Path $file -Destination $Destination -Force 


}

CodePudding user response:

A ForEach-Loop for this shouldn't be needed, let Copy-Item handle it. The following should leave the Folder / File hierarchy intact.

$TimeStamp = Get-Date -Format dd_MM_yyyy
$Destination = "F:\powershell_wwwroot\root_"   $TimeStamp
New-Item -ItemType directory -Path $Destination -Force
$Source = "C:\www\root\"

Copy-Item -LiteralPath $source -Destination $destination -Recurse -Force

CodePudding user response:

you can use either copy-item or bit-transfer. I wrote a script that uses the bits transfer cmdlet. If you update my script, to create the destination folder your home dry. If you have any issues regarding the script holler at me :o)
enter image description here

Import-Module BitsTransfer

#Get Script Location Path 
$ScriptPath = Get-Location
$ScriptPath = $ScriptPath.ToString()
$dirName=[System.IO.Path]::GetDirectoryName($ScriptPath)

#Build Source Server Location Path
#Source Server
$Sourceserver = $env:COMPUTERNAME
#Source Server Folder Path
$Sourcefolderpath = "C:\Temp"
#Switch Colon for Dollar Sign
$Sourcepath = $Sourcefolderpath.Replace(':','$')
#Build Target Folder Path
$ServerSourceFolderpathTest = "\\"   $SourceServer   "\"   $Sourcepath
#Print to screen to view path
#Write-Host $ServerSourceFolderpathTest
#Test Path
$testsourcefolderpath = Test-Path -LiteralPath $ServerSourceFolderpathTest

IF ($testsourcefolderpath -eq $False) {
    Write-Host -Fore Red "Source Folder Path Cannot Be Established. 
    Path is - $testsourcefolderpath"
    Write-Host "Script Will Exit"
    Exit
} ELSE {
    Write-Host -Fore Green "Source Folder Path Established. 
    Path Is - $testsourcefolderpath"
}

#Build Target Server Location Path
#Target Server
$Targetserver = $env:COMPUTERNAME
#Target Server Folder Path
$Targetfolderpath = "C:\Test"
#Switch Colon for Dollar Sign
$Targetpath = $Targetfolderpath.Replace(':','$')
#Build Target Folder Path
$ServerTargetFolderpathTest = "\\"   $TargetServer   "\"   $Targetpath
#Print to screen to view path
#Write-Host $ServerTargetFolderpathTest
#Test Path
$testTargetfolderpath = Test-Path -LiteralPath $ServerTargetFolderpathTest

IF ($testTargetfolderpath -eq $False) {
    Write-Host -Fore Red "Target Folder Path Cannot Be Established. 
    Path is - $testTargetfolderpath"
    Write-Host "Script Will Exit"
    Exit
} ELSE {
    Write-Host -Fore Green "Target Folder Path Established. 
    Path Is - $testTargetfolderpath"
}

#Build Source Target Paths for Bit Transfer
$SourceFolder = $ServerSourceFolderpathTest

$PreTargetFolder = $ServerTargetFolderpathTest
$Slash = "\"
$TargetFolder = $PreTargetFolder   $Slash
#Test Target Folder Path
#$TargetFolder

#Get All Text Files From Source Folder
$GetFiles = (gci $SourceFolder -Filter *.txt)
$GetFilesFileFullName = $GetFiles.FullName
#Test View All Full File Names
#$GetFilesFileFullName



ForEach($File in $GetFilesFileFullName) {

    $Discript = "Backing Up $File from $Sourceserver to $Targetserver -- "   (Get-Date)
    $Discript
    Start-BitsTransfer -Source $File -Destination $TargetFolder -Display $Discript -Priority 'Low' -Asynchronous -WhatIf

}
  • Related