找回密码
 注册

QQ登录

只需一步,快速开始

查看: 417|回复: 1

Backup your data from multiple places (PowerShell)

[复制链接]
发表于 2011-11-21 16:27:01 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2011-11-21 16:33 编辑

Some people may need backup their data files from time to time, and they also want keep the data in an order of date avoiding mess them up.

This powershell script maybe a good way to take care of it under Windows.

  1. #  Script name:    Multi_Backup.ps1
  2. #  Created on:     2011-11-21
  3. #  Author:         John Z
  4. #  Purpose:        Create Backup Files according date


  5. #new folder name can be custmized as you want, or you can just remove it as we already have sub folder created by date and hour.
  6. $NewFolderName = "" #Stores the name of the folder

  7. #Create array of Servers
  8. $Servers = @("Source","Dest","Merges")
  9. $ServerName = @("Source Demo folder copying","Dest Demo folder copying","Merges Demo folder copying")

  10. #Prompt user to enter the new folder name
  11. $NewFolderName = read-Host "Enter the name of the backup folder(Like your name of date string)"

  12. #Function to process the files for each server in the array list.
  13. Function Backup {

  14. #Loop throug each of the servers
  15. for ($i = 0; $i -le $Servers.length -1; $i++)
  16.     {
  17.         write-Host $Servers[$i]
  18.         $BasePath = "C:\Test" + $Servers[$i] + ""
  19.         $DestPath = "C:\Test\Backup" + $NewFolderName + ""
  20.         #[IO.Directory]::CreateDirectory($DestPath)
  21.         $DonePath = $DestPath + $Servers[$i] +""
  22.         #$DonePath = $DestPath + $i +""

  23.         write-Host "Copying files for" $ServerName[$i]
  24.         write-Host "Copying xls files"
  25.         $FilePath = $BasePath + "*.xls"
  26.         #Copy-Item $FilePath $DonePath
  27.         $Files = Get-ChildItem $FilePath
  28.         #echo $Files
  29.         
  30.         #'if folder already exists

  31.          foreach ($file in $Files)
  32.          {
  33.            $date    = $file.LastWriteTime;
  34.            $datepath= [string]$date.Year+"-"+[string]$date.month+"-"+[string]$date.day+"_"+[string]$date.hour;
  35.            $dstpath = $DonePath + $datepath + ""
  36.            $dstfile = $DonePath + $datepath + ""+$file.name
  37.            $srcfile = $file.Fullname

  38.            #$msg = "From '" + $srcfile +"' to '" + $dstfile +"'"
  39.            #Write-Host $msg

  40.            if (test-Path $dstpath)
  41.            {
  42.               #Move-Item -path $srcfile -destination $dstfile -Force
  43.               Copy-Item -path $srcfile -destination $dstfile
  44.            }
  45.            Else
  46.            {
  47.               New-Item $DstPath -type directory -force
  48.               Write-Host "-- new folder"
  49.               #Move-Item -path $srcfile -destination $dstfile -Force
  50.               Copy-Item $srcfile -destination $dstfile
  51.            }
  52.            #echo $srcfile;
  53.            #echo $dstfile
  54.            #$pstmp = read-Host "Any key to continue..."
  55.   
  56.          }
  57.            
  58.          write-Host $BasePath " Copied"
  59.          
  60.     }
  61. }
  62.   
  63. #Run the function   
  64. Backup
复制代码
Another way to keep backup is just move the data from different places to one central point. You can uncomment those lines to make it work, but you do better have your other stepps ready to manipulate the data before or after.

---This codes originately released on BC Morning. if you are interested in it, please keep this in sign.


发表于 2011-11-29 13:52:47 | 显示全部楼层
This is a evolution version depends on codes above. All the codes are most self descriptive.

  1. #  Script name:    Forecast_BK.ps1
  2. #  Created on:     2011-11-23
  3. #  Author:         John Z
  4. #  Purpose:        Classfy Backup Files according Categories and date


  5. #new folder name can be custmized as you want
  6. $NewFolderName = "" #Stores the name of the folder

  7. #Create array of Servers
  8. $Servers = @("Forecast","Queue")
  9. $ServerName = @("Forecast Source folder","Miscellaneous backup folder")


  10. #Function to process the files for each server in the array list.
  11. Function Backup {

  12. $LogFile  = "Y:\DataLib\Backup_log"
  13. Out-File -FilePath $LogFile -InputObject "..........`n" -append -encoding unicode
  14. #Loop throug each of the servers
  15. for ($i = 0; $i -le $Servers.length -1; $i++)
  16.     {
  17.         #Sample folder used to hold sample categories
  18.         $SamPath  = "C:\Test\Sample\*"
  19.         $BasePath = "Y:\DataLib" + $Servers[$i] + ""
  20.         $DestPath = "Y:\archive"
  21.         $DonePath = $DestPath + $Servers[$i] +""
  22.         #$DonePath = $DestPath + $i +""

  23.         write-Host "Now copying files from $($ServerName[$i]) ..."
  24.         write-Host "Copying xls files"
  25.         Out-File -FilePath $LogFile -InputObject "`n`nStart processing `"$($ServerName[$i])`" ... $(date)`n" -append -encoding unicode
  26.         $FilePath = $BasePath + "*.xls"

  27.         $Files = Get-ChildItem $FilePath

  28.         
  29.         #'if folder already exists
  30. If ($(Get-Item $BasePath).GetFiles().Count -gt 0)
  31. {
  32.          foreach ($file in $Files)
  33.          {
  34.            $date    = $file.LastWriteTime
  35.            $datepath= [string]$date.Year+"-"+[string]$date.month+"-"+[string]$date.day
  36.            #+"_"+[string]$date.hour;
  37.            #Write-Host $datepath
  38.            $dstpath = $DonePath + $datepath + ""
  39.            $dstfile = $DonePath + $datepath + ""+$file.name
  40.            Switch ($file.name.substring(0,6))
  41.            {
  42.               "Cat 1 " { Write-Host "Class 1"; $dstfile = $DonePath + $datepath + "\Class 1" + $file.name }
  43.               "Cat 2 " { Write-Host "Class 2"; $dstfile = $DonePath + $datepath + "\Class 2" + $file.name }
  44.               "Cat 3 " { Write-Host "Class 3"; $dstfile = $DonePath + $datepath + "\Class 3" + $file.name }
  45.               "Cat 4 " { Write-Host "Class 4"; $dstfile = $DonePath + $datepath + "\Class 4" + $file.name }
  46.               "Cat 5 " { Write-Host "Class 5"; $dstfile = $DonePath + $datepath + "\Class 5" + $file.name }
  47.               "Cat 6 " { Write-Host "Class 6"; $dstfile = $DonePath + $datepath + "\Class 6" + $file.name }
  48.               "Cat 7 " { Write-Host "Class 7"; $dstfile = $DonePath + $datepath + "\Class 7" + $file.name }
  49.               "Cat 8 " { Write-Host "Class 8"; $dstfile = $DonePath + $datepath + "\Class 8" + $file.name }
  50.               "Cat 9 " { Write-Host "Class 9"; $dstfile = $DonePath + $datepath + "\Class 9" + $file.name }
  51.            }
  52.            $srcfile = $file.Fullname
  53.            if (test-Path $dstpath)
  54.            {
  55.               Move-Item -path $srcfile -destination $dstfile -Force
  56.            }
  57.            Else
  58.            {
  59.               New-Item $DstPath -type directory -force
  60.               Write-Host "-- Create Group folders"
  61.               Copy-Item $SamPath -Destination $DstPath -Force
  62.               Move-Item -path $srcfile -destination $dstfile -Force
  63.            }
  64.   
  65.          }
  66.          $msg = "Files under " + $BasePath + " Copied."
  67.       }
  68.       Else
  69.       {
  70.          $msg = "===> Folder `"" + $BasePath + "`" is empty. Skipped."
  71.       }
  72.       $tm = date
  73.       $msg = "$($tm) " + $msg
  74.       Write-Host $msg
  75.       $msg | out-file $LogFile -append
  76.          
  77.     }
  78. }
  79.   
  80. #Run the function   
  81. Backup
复制代码

The log file function added as well.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-16 21:45 , Processed in 0.018886 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表