PowerShell: Backup files and folders

From ICO wiki
Jump to navigationJump to search

<Source lang="powershell">

  1. Name: Backup.ps1
  2. Author: Kalju H6bem2e
  3. Version: 0.1a
  4. Date: 26.05.2012
  5. Comment: Teeb varu koopiad antud folderist
  6. Main all muuta $strSourceFolder = Kust
  7. $strDestinationFolder = Kuhu
  8. Idee tuli sellest, et kasutan PlayStation Portable consooli, ja mul on erinevad m2lukaardid kus on erinevad salvestatud failid (Save Folderid).
  9. Script, mis v6tab vastavast kaustas ja teeb kuup2evalise koopia m22ratud kausta.

function DoSubFolders {

   param([object] $objSrcFolder, [object] $objDstFolder) 

   $colSrcSubFolders = $objSrcFolder.SubFolders 

   foreach($objSrcSubFolder in $colSrcSubFolders) 
   { 
       $strSrcSubFolder = $objSrcSubFolder.Path 
       $strDstSubFolder = $strSrcSubFolder.Replace($strSourceFolder, $strDestinationFolder) 
       if( -not $FSO.FolderExists($strDstSubFolder)) 
       { 
           $FSO.CreateFolder($strDstSubFolder) 
           Write-Host "L6ime folderi $strDstSubFolder" 
       } 
       $objDstSubFolder = $FSO.GetFolder($strDstSubFolder) 
       BackupFolder $objSrcSubFolder $objDstSubFolder 
       DoSubFolders $objSrcSubFolder $objDstSubFolder 
   } 

}

function BackupFolder {

   param( [object] $objSrcFolder, [object] $objDstFolder) 

   $strSrcFolder = $objSrcFolder.Path 
   $strDstFolder = $objDstFolder.Path 

   $colSrcFiles = $objSrcFolder.Files 
   $colDstFiles = $objDstFolder.Files 

   foreach ($objSrcFile in $colSrcFiles) 
   { 
       $strSrcFile = $strSrcFolder + "\" + $objSrcFile.Name 
       $strDstFile = $strDstFolder + "\" + $objSrcFile.Name 
       if($FSO.FileExists($strDstFile)) 
       { 
           $objDstFile = $FSO.GetFile($strDstFile) 
           $dtSrcFile = $objSrcFile.DateLastModified 
           $dtDstFile = $objDstFile.DateLastModified 
           $iSec = (New-TimeSpan -Start $dtDstFile -End $dtSrcFile).TotalSeconds 
           if($iSec -gt 2) # lubab 2 sekundilise vahe kui on CD-lt v6i muult v2liselt kettalt
           { 
               $FSO.CopyFile($strSrcFile, $strDstFile, $true) 
               Write-Host "Kopeerisin faili $strSrcFile to $strDstFile" 
               $script:iCopyCount++ 
           } 
       } 
       else 
       { 
           $FSO.CopyFile($strSrcFile, $strDstFile, $true) 
           Write-Host "Kopeerisin faili $strSrcFile to $strDstFile" 
           $script:iCopyCount++ 
       } 
   } 
#Kui failid olemas siis kututab olemasoleva 2ra ja asendab uuega
   foreach($objDstFile In $colDstFiles) 
   { 
       $strDstFile = $objDstFile.Path 
       $strSrcFile = $strSrcFolder + "\" + $objDstFile.Name 

       if( -not $FSO.FileExists($strSrcFile)) 
       { 
           $FSO.DeleteFile($strDstFile) 
           Write-Host "Kustutasin faili $strDstFile" 
           $script:iDestDeletedCount++ 
       } 
   } 

   DoSubFolders $objSrcFolder $objDstFolder 

}


function WaitKey {

   param( [String] $strPrompt = "Vajuta suvalist nuppu ... ") 
   Write-Host 
   Write-Host $strPrompt -NoNewline 
   $key = [Console]::ReadKey($true)
   Write-Host 

}


  1. main

$date = Get-Date -Format d.MMMM.yyyy

  1. Folder kust v6tta

$strSourceFolder = "C:\DOS\" #siia pane oma kaust

  1. Kuhu kopeerib

$strDestinationFolder = "C:\Backup\$date\" #siia pane oma kaust

$iCopyCount = 0 $iDestDeletedCount = 0

Write-Host Write-Host "Backing up " -NoNewline -ForegroundColor "White" Write-Host $strSourceFolder -ForegroundColor "Cyan" -NoNewline Write-Host " to " -NoNewline -ForegroundColor "White" Write-Host $strDestinationFolder -ForegroundColor "Cyan" Write-Host

$FSO = New-Object -COM Scripting.FileSystemObject

#testib kas Folder kus v6tta on olemas, kui ei ole l6petab scripti

if( -not $FSO.FolderExists($strSourceFolder) ) {

   Write-Host "Viga: l2hte kasuta pole!" -ForegroundColor "Red" 
   Write-Host 
   Write-Host "V2ljume skriptist" 
   WaitKey "Vajuta suvalist nuppu ... " 
   exit 

}

#testib kas kausta kuhu kopeeritakse backup andmed on olemas, kui mitte siis kysib kas võib luua kausta

if( -not $FSO.FolderExists($strDestinationFolder) ) {

   Write-Host "Hoiatus: Sihtkoha kausta ei ole" 
   $p = Read-Host "Kas loome selle ja j2tkame? (Y / N) " 
   Write-Host 
   if( $p.Substring(0, 1).ToUpper() -eq "Y" ) 
   { 
       $FSO.CreateFolder($strDestinationFolder) 
   } 
   else 
   { 
       Write-Host "V2ljume skriptist" 
       WaitKey "Vajuta suvalist nuppu ... " 
       exit 
   } 

}

$objSourceFolder = $FSO.GetFolder($strSourceFolder) $objDestinationFolder = $FSO.GetFolder($strDestinationFolder)

BackupFolder $objSourceFolder $objDestinationFolder

if( ($iCopyCount -eq 0) -and ($iDestDeletedCount -eq 0) ) {

   Write-Host 
   Write-Host "Folders are synchronized" -ForegroundColor "magenta" 

} else {

   Write-Host 
   Write-Host $iCopyCount "Failid ja kaustad kopeeritud allikast sihtkohta" -ForegroundColor "magenta" 
   Write-Host $iDestDeletedCount "Yleliigset kustutatud" -ForegroundColor "magenta" 

}

WaitKey "Vajuta suvalist nuppu ... "