Created
July 20, 2018 18:43
-
-
Save bolivarbutzke/3889f853cd36a5bfd930602d0fdc6b4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Copia diretórios e arquivos de um local (Origem) para o outro (Destino) | |
## No caso do arquivo já existir, copia apenas se o arquivo foi modificado, criando um novo arquivo com a data da cópia no início | |
## Criado em 2018-07-20 por Bolívar Butzke | |
## dicasdobolivar.blogspot.com | |
$data_atual = ("{0:yyyy-MM-dd}" -f (Get-Date)) | |
$Source = "F:\testescript" | |
$Destination = "F:\bkp_testescript" | |
# Getting Files/Folders from Source | |
# Obtendo arquivos e pastas da origem | |
$SrcEntries = Get-ChildItem $Source -Recurse | |
# Parsing the folders and Files | |
# Separando os Diretórios e Arquivos | |
$Srcfolders = $SrcEntries | Where-Object{$_.PSIsContainer} | |
$SrcFiles = $SrcEntries | Where-Object{!$_.PSIsContainer} | |
# Formas de pegar apenas os arquivos modificados em um período, deixei de exemplo mas não uso mais porque arquivos copiados mantém data de modificação e não entravam no backup | |
#$SrcFilesUpdatedYesterday = $SrcFiles | Where{$_.LastWriteTime -ge (get-date).AddDays(-1)} | |
#$SrcFilesUpdated1MinuteAgo = $SrcFiles | Where{$_.LastWriteTime -ge (get-date).AddMinutes(-1)} | |
# Checking for Folders that are in Source, but not in Destination | |
# Cria diretórios no Destindo igual a Origem | |
foreach($folder in $Srcfolders) | |
{ | |
$SrcFolderPath = $source -replace "\\","\\" -replace "\:","\:" | |
$DesFolder = $folder.Fullname -replace $SrcFolderPath,$Destination | |
if(!(test-path $DesFolder)) | |
{ | |
# Diretório ausente, cria ele | |
Write-Host "Diretório $DesFolder ausente. Criando ele!" | |
new-Item $DesFolder -type Directory | out-Null | |
} | |
} | |
# Copia os arquivos novos e modificados, adicionando a data atual no início do nome do arquivo modificado | |
foreach($arquivo in $SrcFiles) | |
{ | |
$SrcFullname = $arquivo.fullname | |
$SrcFilePath = $Source -replace "\\","\\" -replace "\:","\:" | |
$DesFile = $SrcFullname -replace $SrcFilePath,$Destination | |
Write-Host "Testando se o arquivo $Desfile existe..." | |
# Verifica se o arquivo existe no destino | |
if(test-Path $Desfile) | |
{ | |
#existe, compara data de modificação | |
$SrcDate = get-Item $SrcFullname | foreach-Object{$_.LastWriteTimeUTC} | |
$DesDate = get-Item $DesFile | foreach-Object{$_.LastWriteTimeUTC} | |
# -gt = Greater than | |
if($SrcDate -gt $DesDate) | |
{ | |
# muda nome do arquivo que foi modificado / add data no início | |
$DesDirectory = $arquivo.Directory -replace $SrcFilePath,$Destination | |
$DesFileModified = $DesDirectory +"\"+ $data_atual + "_" + $arquivo.Name | |
#copia o arquivo com novo nome (versionamento) | |
Write-Host "Arquivo $arquivo.Name foi modificado, copiando para $DesFileModified" | |
copy-Item -path $SrcFullName -dest $DesFileModified -force | |
} | |
} | |
else | |
{ | |
# arquivo não existe, copia o arquivo para o destino | |
Write-Host "$SrcFullname é novo, copiando para $DesFile" | |
copy-Item -path $SrcFullName -dest $DesFile -force | |
} | |
} | |
# Fontes de Inspiração: | |
# - https://ss64.com/ps/syntax-compare.html | |
# - https://stackoverflow.com/questions/677789/powershell-copy-item-but-only-copy-changed-files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment