|
|
本帖最后由 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.
- # Script name: Multi_Backup.ps1
- # Created on: 2011-11-21
- # Author: John Z
- # Purpose: Create Backup Files according date
-
- #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.
- $NewFolderName = "" #Stores the name of the folder
- #Create array of Servers
- $Servers = @("Source","Dest","Merges")
- $ServerName = @("Source Demo folder copying","Dest Demo folder copying","Merges Demo folder copying")
- #Prompt user to enter the new folder name
- $NewFolderName = read-Host "Enter the name of the backup folder(Like your name of date string)"
- #Function to process the files for each server in the array list.
- Function Backup {
-
- #Loop throug each of the servers
- for ($i = 0; $i -le $Servers.length -1; $i++)
- {
- write-Host $Servers[$i]
- $BasePath = "C:\Test" + $Servers[$i] + ""
- $DestPath = "C:\Test\Backup" + $NewFolderName + ""
- #[IO.Directory]::CreateDirectory($DestPath)
- $DonePath = $DestPath + $Servers[$i] +""
- #$DonePath = $DestPath + $i +""
- write-Host "Copying files for" $ServerName[$i]
- write-Host "Copying xls files"
- $FilePath = $BasePath + "*.xls"
- #Copy-Item $FilePath $DonePath
- $Files = Get-ChildItem $FilePath
- #echo $Files
-
- #'if folder already exists
- foreach ($file in $Files)
- {
- $date = $file.LastWriteTime;
- $datepath= [string]$date.Year+"-"+[string]$date.month+"-"+[string]$date.day+"_"+[string]$date.hour;
- $dstpath = $DonePath + $datepath + ""
- $dstfile = $DonePath + $datepath + ""+$file.name
- $srcfile = $file.Fullname
- #$msg = "From '" + $srcfile +"' to '" + $dstfile +"'"
- #Write-Host $msg
- if (test-Path $dstpath)
- {
- #Move-Item -path $srcfile -destination $dstfile -Force
- Copy-Item -path $srcfile -destination $dstfile
- }
- Else
- {
- New-Item $DstPath -type directory -force
- Write-Host "-- new folder"
- #Move-Item -path $srcfile -destination $dstfile -Force
- Copy-Item $srcfile -destination $dstfile
- }
- #echo $srcfile;
- #echo $dstfile
- #$pstmp = read-Host "Any key to continue..."
-
- }
-
- write-Host $BasePath " Copied"
-
- }
- }
-
- #Run the function
- 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.
|
|