Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Backup folder in Powershell

I am working on a script to create a daily backup ( task schedule )

First I copy the folder "source_folder" and rename all the files with timestamp inside the "bkp" folder , when a new files is added in "source_folder" need to copy only the last file and also renamed ( I tried with LastModified or LastAccessTime but when I run the script again( next day) the last file is duplicated if no other file is create in soruce_folder Any advice ?

$sourceFiles = Get-ChildItem -Path $source -Recurse
$bkpFiles    = Get-ChildItem -Path $bkp -Recurse
$syncMode    = 1 

if(!(Test-Path $bkp)) {
   Copy-Item -Path $source -Destination $bkp -Force -Recurse
   Write-Host "created new folder"  
   $files = get-ChildItem -File -Recurse -Path $bkp
   foreach($file in $files){
       # Copy  files to the backup directory
       $newfilename = $file.FullName +"_"+ (Get-Date -Format yyyy-MM-dd-hhmmss)
   
       Rename-Item -path $file.FullName -NewName $newfilename
   }
}
elseif ((Test-Path $bkp ) -eq 1) {
    $timestamp1 = (Get-Date -Format yyyy-MM-dd-hhmmss)
    $timestamp = "_" + $timestamp1
    @(Get-ChildItem $source -Filter *.*| Sort LastAccessTime -Descending)[0] | % { 
        Copy-Item -path $_.FullName -destination $("$bkp$_$timestamp") -force
    }  
    Write-Host  "most recent files added" 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Based on this "(Get-ChildItem $source -Filter .| Sort LastAccessTime -Descending)[0]" you only ever expect to copy 1 file per day. And it sounds like the problem is that the script copies a file over even when no new files have been added to $source. Hopefully I have that right.

Maybe you can add a filter like below, assuming your file is added on a regular schedule

(Get-ChildItem $source -Filter .| Sort LastAccessTime -Descending | ? {$_.LastAccessTime -gt $(get-date).AddDays(-1))[0] #may want to use LastWriteTime or CreationTime in place of LastAccessTime. Can also fiddle with .AddDays - .AddMinutes, .AddHours, etc.

Alternatively you can check your $bkp folder to see if the file exists before copying it:

@(Get-ChildItem $source -Filter *.*| Sort LastAccessTime -Descending)[0] | % { 
#check if file exists in $bkp before copying from $source
#"$($_.name)*" part tries to account for the file in $bkp having a timestamp appended to the name
$x = get-childitem $bkp -recurse | ? {$_.name -like "$($_.name)*"}
if(!$x){
    Copy-Item -path $_.FullName -destination $("$bkp$_$timestamp") -force
}        

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...